2

I try to implement camera2 to my app on Samsung Note 8, however, the image in preview is deformed.

Preview Size : 2220x1080 (ratio 18.5:9)
Camera Size : 1920x1080 (ratio 16:9)

It seem that to fit the size of image from 1920x1080 to 2220x1080

how to preview and capture image with full screen(2220x1080)?

public void createCameraPreviewSession() {
    try {
        SurfaceTexture texture = this.getSurfaceTexture();
        assert texture != null;
        texture.setDefaultBufferSize(previewSize.getWidth(), previewSize.getHeight());
        Surface surface = new Surface(texture);
        previewBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        previewBuilder.addTarget(surface);
        cameraDevice.createCaptureSession(Arrays.asList(surface, reader.getSurface()),
                new CameraCaptureSession.StateCallback() {
                    @Override
                    public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                        if (null == cameraDevice) {
                            return;
                        }
                        previewSession = cameraCaptureSession;
                        thread = new HandlerThread("CameraPreview");
                        thread.start();
                        backgroundHandler = new Handler(thread.getLooper());
                        try {
                      previewBuilder.set(CaptureRequest.CONTROL_AF_MODE,CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
              previewSession.setRepeatingRequest(previewBuilder.build(), null, backgroundHandler);
                        } catch (CameraAccessException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onConfigureFailed(
                            @NonNull CameraCaptureSession cameraCaptureSession) {
                    }
                }, null
        );
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
W.Mike
  • 45
  • 1
  • 7

1 Answers1

1

Galaxy Note 8 is one of the many devices where the camera doesn't support a frame size that would exactly fit the screen. The usual solution to that is 'boxing': prepare the screen layout that has some black margins and a rectangle surface that matches the desired preview frame aspect ratio. The camera2 API will automatically scale the preview frame to fit the surface size, but the result will be distorted if the aspect ratios don't match.

Another approach is to 'crop' the preview to fit the screen (again, we are speaking about the aspect ratio, not the number of pixels). It is easier to do this through TextureView, not through surface. In Full screen preview camera2Basic Example Project you can find how this can be coded.

See also Android Camera2 capture image skewed which specifically discusses some problems with camera2 frame sizes on some Samsung devices.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307