Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set android camera2 preview and capture size?

I am using a SurfaceView to show the preview I capture. I want to use width=1080,height=1920 for the preview. Where can I set the size of the preview?

I googled for an answer, but they are all for camera version one. I am using the 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");
    }
}
like image 216
John Zhang Avatar asked Jul 25 '17 15:07

John Zhang


People also ask

How to set camera preview orientation in Android?

The image must be rotated 270 degrees counterclockwise so that the preview's orientation matches the device orientation: A back-facing camera would produce an image buffer with the same orientation as the buffer above, but SENSOR_ORIENTATION is 90 degrees. As a result, the buffer is rotated 90 degrees clockwise.

What is camera preview in Android?

PreviewView is a subclass of FrameLayout . To display the camera feed, it uses either a SurfaceView or TextureView , provides a preview surface to the camera when it's ready, tries to keep it valid as long as the camera is using it, and when released prematurely, provides a new surface if the camera is still in use.

What is camera api2?

Both CameraX and Camera2 support Android 5.0 (API level 21) and higher. Camera2 is the latest low-level Android camera package and replaces the deprecated Camera class. Camera2 provides in-depth controls for complex use cases, but requires you to manage device-specific configurations.

Should I use CameraX?

For new apps, we recommend starting with CameraX. It provides a consistent, easy-to-use API that works across the vast majority of Android devices, with backward-compatibility to Android 5.0 (API level 21).


2 Answers

As documented in the reference to createCaptureSession:

For drawing to a SurfaceView: Once the SurfaceView's Surface is created, set the size of the Surface with setFixedSize(int, int) to be one of the sizes returned by getOutputSizes(SurfaceHolder.class) and then obtain 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.

like image 159
Eddy Talvala Avatar answered Oct 21 '22 16:10

Eddy Talvala


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 fragment which chooses the optimal preview size for a given device. This may be a better approach if you want to make your app more flexible, rather than hardcoding the size, but even if you would still rather stick with set sizes you can see how they use the results.

The summary is that you simply set the size of the, in their case, TextureView to whatever size preview you want.

The method name is 'chooseOptimalSize' 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
     */
like image 37
Mick Avatar answered Oct 21 '22 16:10

Mick