How to set android camera2 preview and capture size?

天大地大妈咪最大 提交于 2020-01-13 01:29:07

问题


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");
    }
}

回答1:


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.




回答2:


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 optional 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
     */


来源:https://stackoverflow.com/questions/45307191/how-to-set-android-camera2-preview-and-capture-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!