user1459497
user1459497

Reputation: 657

Android Camera API how to show bitmap in ImageView?

I am trying to use Android Camera API to take a picture. In my Activity i have SurfaceView for camera preview, Camera obj and Button for take a snap. The following is my onCreate of my activity:

    @SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_myactivity2);
    button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            cameraObj.takePicture(shutterCallback, null, pictureCallback);  
        }
    });

    surfaceView = (SurfaceView) findViewById(R.id.camera_surfaceview);
    cameraObj = Camera.open(0);
    SurfaceHolder holder = surfaceView.getHolder();
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    holder.addCallback(new SurfaceHolder.Callback() {

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            //do nothing    for now
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            try {
                cameraObj.setPreviewDisplay(holder);
                cameraObj.startPreview();

            } catch (IOException e) {
                e.printStackTrace();
            }

            cameraObj.startPreview();

        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {

        }
    });
}//end of onCreate

The following is my Shutter Call back and Picture Call back method.

private Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback() {

    @Override
    public void onShutter() {


    }
};

private Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {

    String filename = "test_";

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        try {

            FileOutputStream fios = openFileOutput(filename, Context.MODE_PRIVATE);
            fios.write(data);
            fios.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
};

My Question is how will i display the bitmap image to imageView in another activity or in the same activity?

Upvotes: 0

Views: 326

Answers (1)

Karim Baidar
Karim Baidar

Reputation: 559

Try this

//CONVERTING IMAGE TO BITMAP

/*public static Bitmap getBitmapFromURL(String xxx) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}*/

    void create_bitmap(){
        //creating bitmap
        Bitmap source = BitmapFactory.decodeResource(getResources(),
        R.drawable.image1);
        //calling doGreyScale
        doGreyscale(source);
    }

    public static void doGreyscale(Bitmap src) {
        // constant factors
        final double GS_RED = 0.299;
        final double GS_GREEN = 0.587;
        final double GS_BLUE = 0.114;

        // create output bitmap
        Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
        // pixel information
        int A, R, G, B;
        int pixel;

        // get image size
        int width = src.getWidth();
        int height = src.getHeight();

        // scan through every single pixel
        for(int x = 0; x < width; ++x) {
            for(int y = 0; y < height; ++y) {
                // get one pixel color
                pixel = src.getPixel(x, y);
                // retrieve color of all channels
                A = Color.alpha(pixel);
                R = Color.red(pixel);
                G = Color.green(pixel);
                B = Color.blue(pixel);
                // take conversion up to one single value
                R = G = B = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B);
                // set new pixel color to output bitmap
                bmOut.setPixel(x, y, Color.argb(A, R, G, B));
            }
        }




         //converting bitmap object to show in imageview2
          ImageView mImg;
        mImg = (ImageView) findViewById(R.id.imageView2);
          mImg.setImageBitmap(bmOut);

    }

}

Upvotes: 0

Related Questions