Allie Fly
Allie Fly

Reputation: 55

I need to capture Image and display it in another activity

I have an activity that starts the camera API. I want to press the button (named by id "cptr_1") and take a picture and display it in another activity (PhotoPreview.class) where I can add photo effects. I just need the code for:

    ImageButton capture_1 = (ImageButton)findViewById(R.id.cptr_1);
    capture_1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

And then how to transfer that image to PhotoPreview.class

Upvotes: 0

Views: 639

Answers (2)

sajan
sajan

Reputation: 389

either you can use an Intent to take picture or use a custom camera class for camera intent for custom camera class

if u already have a custom camera class, you can use this code`Camera cam = Camera.open(cameraId);

capture_1.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        try {

            cam.takePicture(null, null, mPicture);


        } catch (Exception e) {

        }
    }
});


private PictureCallback mPicture = new PictureCallback() {

    public void onPictureTaken(final byte[] data, Camera camera) {

        try {

            File mediaFile = new File("file path/filename.jpg");

            FileOutputStream fos = new FileOutputStream(mediaFile);
            fos.write(data);
            fos.close();
            cam.startPreview();


        } catch (Exception e) {
        }
    }
};`

Upvotes: 0

BooDoo
BooDoo

Reputation: 635

You can take photo with the camera app of the device.

So when you click:

    static final int ImageValue= 1;

    ImageButton capture_1 = (ImageButton)findViewById(R.id.cptr_1);
        capture_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Intent takepic = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
               if (takepic.resolveActivity(getPackageManager()) != null) {
               startActivityForResult(takepic, ImageValue);
               }
            }
        });

Once capture is completed get the image back from the camera application

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ImageValue && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
       }
}

Then send Bitmap to another activity.

Inside the activity that starts the camera API write:

Intent intent = new Intent(this, PhotoPreview.class);
intent.putExtra("GetBitmap", bitmap);

Inside PhotoPreview.class write:

Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("GetBitmap");

Also you may need to add those permissions to Android Manifest

 <uses-feature android:name="android.hardware.camera"
                  android:required="true" />
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Upvotes: 1

Related Questions