Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

java - Take photo inserted inside the rectangle(canvas) in Androidx

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.

enter image description here

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...