1

I'm not trying to configurate the custom camera, I only need to take a picture with it. I have a SurfaceView in my xml and a button to take photos. I find this method to take a picture: mCamera.takePicture(null, null, mPicture);

mPicture defined like this:

Camera.PictureCallback mPicture = new Camera.PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        pictureFile = getOutputMediaFile();
        if (pictureFile == null) {
            return;
        }
        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();


        } catch (FileNotFoundException e) {

        } catch (IOException e) {
        }
    }
};

What I need for is a bitmap of the taken image in order to use it in another Activity. Thanks in advance!

  • Why showing all that code! Irrelevant. You have a jpg file in your device and you want a bitmap of it is what you want. Thats all. What ecactly is the problem? Tell the other avtivity the path to the file. – greenapps Mar 14 '17 at 18:20

2 Answers2

0
> I asked my instructor about this question and he told me that the
> code below should work fine for you.

public class Custom_Camera
{
private Camera a;
private ab1 ab2;

/** Called when the activity is first created. */
@Override
public void onCreate(Total State) {
    super.onCreate(Saved State);
    setContentView(R.layout.main);
    a = getCameraInstance();
    ab1 = new ab2(this, a);
    FrameLayout= (FrameLayout) findViewById(R.id.a_b2);
    preview.addView(ab2);

    Button captureButton = (Button) findViewById(R.id.button_capture);
    captureButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCamera.takePicture(null, null, mPicture);
        }
    });
}
private Camera a getCameraInstance() {
    Camera a = null;
    try {
        camera = Camera.open();
    } catch (Exception e) {
    }
    return camera a;
}

Picture = new Picture() {
    public void taken(byte[] data, Camera camera) {
        File = getOutputMediaFile();
        if (pictureFile == null) {
            return;
        }
        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        } catch (FileNotFoundException e) {

        } catch (IOException e) {
        }
    }
};

private static File getOutputMediaFile() {
    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            "MyCameraApp");
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }
    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
            .format(new Date());
    File mediaFile;
    mediaFile = new File(mediaStorageDir.getPath() + File.separator
            + "IMG_" + timeStamp + ".jpg");

    return mediaFile;
   }
  • Another person had asked this question here: [link](http://stackoverflow.com/questions/10913682/how-to-capture-and-save-an-image-using-custom-camera-in-android) – BritenGGrosi Mar 14 '17 at 18:23
0

What I need for is a bitmap of the taken image in order to use it in another Activity. Thanks in advance!

All you need is decode byte array received from camera, and then rotate it for right orientation.

Get camera display orientation:

private static int getCameraDisplayOrientation(int cameraId, Activity activity) {
    int rotation = ((WindowManager)activity.getSystemService(Context.WINDOW_SERVICE))
            .getDefaultDisplay().getRotation();
    android.hardware.Camera.CameraInfo info =
            new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    int result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = (info.orientation + rotation) % 360;
        result = (360 - result) % 360;  // compensate the mirror
    } else {  // back-facing
        result = (info.orientation - rotation + 360) % 360;
    }
    return result;
}

Decode byte array to bitmap:

public static Bitmap decodeByteArray(byte[] data, float rotationDegree) {
    try {
        Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
        if (rotationDegree != 0) {
            bm = createRotatedBitmap(bm, rotationDegree);
        }
        return  bm;
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        return null;
    }
}

Rotate bitmap:

public static Bitmap createRotatedBitmap(Bitmap bm, float rotation) {
    Matrix matrix = new Matrix();
    matrix.postRotate(rotation, bm.getWidth()/2, bm.getHeight()/2);
    try {
        Bitmap result =  Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
        return result;
    } catch (OutOfMemoryError e) {
        return null;
    }
}
Duy Pham
  • 1,179
  • 1
  • 14
  • 19