How to set Android2 camera preview and capture size?

I am using SurfaceView

to show preview. I want to use width = 1080, height = 1920 for preview. Where can I set the preview size?

I searched google for the answer, but they are all for camera version 1. I am using android.hardware.camera2.

private void takePreview() {
    try {
        final CaptureRequest.Builder previewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        previewRequestBuilder.addTarget(mSurfaceHolder.getSurface());
        mCameraDevice.createCaptureSession(Arrays.asList(mSurfaceHolder.getSurface(), mImageReader.getSurface()), new CameraCaptureSession.StateCallback() // โ‘ข
        {
            @Override
            public void onConfigured(CameraCaptureSession cameraCaptureSession) {
                if (null == mCameraDevice) return;
                mCameraCaptureSession = cameraCaptureSession;
                try {
                    previewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
                    previewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH);
                    previewRequestBuilder.set(CaptureRequest.JPEG_THUMBNAIL_SIZE, new Size(1080,1920));

                    CaptureRequest previewRequest = previewRequestBuilder.build();
                    mCameraCaptureSession.setRepeatingRequest(previewRequest, null, childHandler);
                } catch (CameraAccessException e) {
                    Log.e("takePreview","onConfigured(CameraCaptureSession cameraCaptureSession)",e);
                }
            }
            @Override
            public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
                Log.e("takePreview","onConfigureFailed");
            }
        }, childHandler);
    } catch (CameraAccessException e) {
        Log.e("takePreview","CameraAccessException");
    }
}

      

+3


source to share


2 answers


As described in the createCaptureSession link :



To paint the SurfaceView: After creating the SurfaceView Surface, set the surface size from setFixedSize (int, int) to one of the sizes returned by getOutputSizes (SurfaceHolder.class ), and then get the Surface by calling getSurface () . If the size is not set by the application, it will be rounded to the nearest supported size less than 1080p by the camera device.

+1


source


Take a look at the Camera2Basic example that Google provides on GitHub: https://github.com/googlesamples/android-Camera2Basic

There is a method in the main snippet that selects an additional preview size for a given device. This might be the best approach if you want to make your application more flexible rather than hard-coded the size, but even if you stick with the given sizes anyway, you can see how they use the results.

The recap is that you simply set the size in your case to the TextureView for any size preview.



The method name is "selectOptimalSize" and it includes this comment / explanation:

/**
     * Given {@code choices} of {@code Size}s supported by a camera, choose the smallest one that
     * is at least as large as the respective texture view size, and that is at most as large as the
     * respective max size, and whose aspect ratio matches with the specified value. If such size
     * doesn't exist, choose the largest one that is at most as large as the respective max size,
     * and whose aspect ratio matches with the specified value.
     *
     * @param choices           The list of sizes that the camera supports for the intended output
     *                          class
     * @param textureViewWidth  The width of the texture view relative to sensor coordinate
     * @param textureViewHeight The height of the texture view relative to sensor coordinate
     * @param maxWidth          The maximum width that can be chosen
     * @param maxHeight         The maximum height that can be chosen
     * @param aspectRatio       The aspect ratio
     * @return The optimal {@code Size}, or an arbitrary one if none were big enough
     */

      

0


source







All Articles