【问题标题】:How to set android camera2 preview and capture size?如何设置android camera2预览和捕获大小?
【发布时间】:2017-07-25 15:07:56
【问题描述】:

我正在使用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");
    }
}

【问题讨论】:

    标签: android android-camera2


    【解决方案1】:

    如对createCaptureSession 的引用中所述:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 嗨@Mick,我已经阅读了代码。示例使用 (TextureView) texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight()) 告诉相机使用正确的分辨率。但是SurfaceView没有那个方法。
      猜你喜欢
      • 2017-02-23
      • 1970-01-01
      • 2020-04-06
      • 1970-01-01
      • 1970-01-01
      • 2022-11-16
      • 2017-06-18
      • 2015-02-26
      • 2017-09-09
      相关资源
      最近更新 更多