如何设置android camera2预览和捕获大小?

时间:2017-07-25 15:07:56

标签: android android-camera2

我使用SurfaceView来显示我捕获的预览。我想使用width = 1080,height = 1920进行预览。我在哪里可以设置预览的大小?

我用谷歌搜索了一个答案,但它们都是用于相机版本的。我使用的是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");
    }
}

2 个答案:

答案 0 :(得分:5)

createCaptureSession的引用中所述:

  

绘制到SurfaceView时:创建SurfaceView的Surface后,将setFixedSize(int, int)的Surface大小设置为getOutputSizes(SurfaceHolder.class返回的大小之一,然后获取通过调用getSurface()来表面。如果应用程序未设置尺寸,相机设备将将其四舍五入到最接近的支持尺寸小于1080p。

答案 1 :(得分:1)

查看Google在GitHub上提供的Camera2Basic示例:https://github.com/googlesamples/android-Camera2Basic

主片段中有一种方法可以选择给定设备的可选预览大小。如果你想让你的应用程序更灵活,而不是硬编码大小,这可能是一种更好的方法,但即使你仍然坚持设置尺寸,你也可以看到它们如何使用结果。

摘要是您只需将TextureView的大小设置为您想要的任何大小的预览。

方法名称是' chooseOptimalSize'它包括这个评论/解释:

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