I am making a camera based application, where I put a rectangular view over the camera.
When I capture an image using new Camera.PictureCallback()
, I cropped th
For me, I take a image then crop according to dimension of the selected area of the image..
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap imageOriginal = BitmapFactory.decodeByteArray(data, 0, data.length, null);
int width = imageOriginal.getWidth();
int height = imageOriginal.getHeight(); // for width
int narrowSize = Math.min(width, height); // for height
int differ = (int) Math.abs((imageOriginal.getHeight() - imageOriginal.getWidth()) / 2.0f); // for dimension
width = (width == narrowSize) ? 0 : differ;
height = (width == 0) ? differ : 0;
Matrix rotationMatrix = new Matrix();
rotationMatrix.postRotate(90); // for orientation
Bitmap imageCropped = Bitmap.createBitmap(imageOriginal, width, height, narrowSize, narrowSize, rotationMatrix, false);
}
Capture Image
Output Image
Your rectangle is mCustomView.getWidth()*2/3 pixels wide, and mCustomView.getHeight()*2/3 pixels high, and its left corner is at 1/6 of mCustomView.getWidth(), if I understand your post correctly.
public void onPictureTaken(byte[] data, Camera camera) {
//.../
Bitmap imageOriginal = BitmapFactory.decodeByteArray(data, 0, data.length);
int[] customViewPosition;
mCustomView.getLocationInWindow(customViewPosition);
int[] surfacePosition;
mSurfaceView.getLocationInWindow(surfacePosition);
float scale = imageOriginal.getWidth()/(float)mSurfaceView.getWidth();
int left = (int) scale*(customViewPosition[0] + mCustomView.getWidth()/6F - surfacePosition[0]);
int top = (int) scale*(customViewPosition[1] + mCustomView.getHeight()/6F - surfacePosition[1]);
int width = (int) scale*mCustomView.getWidth()*2/3;
int height = (int) scale*mCustomView.getHeight()*2/3;
Bitmap imageCropped = Bitmap.createBitmap(imageOriginal, left, top, width, height, null, false);
//.../
}