I'm using Camera project (https://github.com/duanhong169/Camera) as a reference.
I need to take a picture of a document, for example, a passport, it is necessary that
the photo is exactly only the document that is inserted inside the rectangle.
I start the camera (using androidx) showing a rectangle in the draw method :
new CanvasDrawer () {...})
canvas.drawRect (100, 75, 600, 700, paint);
Then, in the captureStillPicture method, the calculateZoomRect method is called,
which creates a rectangle, I imagined
that if the construction of that same rectangle was passed in this method, this would be possible.
I looked at the Android documentation and found the line below
captureRequestBuilder.set (CaptureRequest.SCALER_CROP_REGION, calculateZoomRect ()), would be to crop the rectangle region.
These methods are in class Camera2Photographer (https://github.com/duanhong169/Camera), i changed the original method calculateZoomRect and the construction of CanvasDrawer.
private void captureStillPicture() {
try {
CaptureRequest.Builder captureRequestBuilder = camera.createCaptureRequest(
CameraDevice.TEMPLATE_STILL_CAPTURE);
captureRequestBuilder.addTarget(imageReader.getSurface());
captureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
previewRequestBuilder.get(CaptureRequest.CONTROL_AF_MODE));
captureRequestBuilder.set(CaptureRequest.JPEG_ORIENTATION,
Utils.getOrientation(sensorOrientation, currentDeviceRotation));
captureRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, calculateZoomRect());
captureSession.stopRepeating();
captureSession.capture(captureRequestBuilder.build(),
new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(@NonNull CameraCaptureSession session,
@NonNull CaptureRequest request,
@NonNull TotalCaptureResult result) {
unlockFocus();
callbackHandler.onShotFinished(nextImageAbsolutePath);
Intent data = new Intent();
data.putExtra("fileImagePhotoPath", nextImageAbsolutePath);
activityContext.setResult(activityContext.RESULT_OK, data);
activityContext.finish();
}
@Override
public void onCaptureFailed(@NonNull CameraCaptureSession session,
@NonNull CaptureRequest request,
@NonNull CaptureFailure failure) {
unlockFocus();
callbackHandler.onError(new Error(Error.ERROR_CAMERA));
}
}, null);
} catch (CameraAccessException e) {
callbackHandler.onError(new Error(Error.ERROR_CAMERA, "Cannot capture a still picture.", e));
}
}
Then I did the following:
private Rect calculateZoomRect () {
return new Rect (100, 75, 600, 700);
}
But it didn't work.
I tried to crop the image exactly at those coordinates that I created for the rectangle, or else create a new bitmap with these coordinates, but it didn't work either.
Please, would you have any idea how I can do this?
Regards,
Alessandro
question from:
https://stackoverflow.com/questions/65649330/take-photo-inserted-inside-the-rectanglecanvas-in-androidx