Android Camera2 API 预览有时会失真

标签 android preview android-camera2 distortion camera2

我正在使用 Camera2 API 构建自定义相机。 到目前为止,相机工作得很好,除了预览有时会失真。假设我连续打开相机 7 次。所有尝试都成功,第 8 次相机预览失真。看起来它使用宽度作为高度,反之亦然。

我的代码基于可以找到的 camera2 的 Google 示例实现 here . 有趣的是,即使是 Google 示例实现有时也会出现这种扭曲的预览。我试图修改 AutoFitTextureView 但没有成功。我目前正在使用 Google 再次提供的 AutoFitTextureView.java。

与此类似的帖子 can be found here . 然而,建议的修复并没有解决问题。

我可以通过更改 setUpCameraOutputs 方法中的以下内容来重现该问题:

mTextureView.setAspectRatio(mPreviewSize.getHeight(), mPreviewSize.getWidth());

到:

mTextureView.setAspectRatio(mPreviewSize.getWidth(), mPreviewSize.getHeight());

另一个奇怪的事情是,每当出现扭曲的预览时,您只需按下主页按钮,应用程序就会进入 onPause() 并再次打开应用程序,以便调用 onResume(),每次预览都是完美的。

这里有没有人遇到过这个问题并找到了解决办法?

提前致谢

最佳答案

我在 Sony Xperia Z3 Tablet Compact 上遇到了同样的问题。

Alex 指出的拉取请求似乎对我不起作用。它会导致相机预览大于 View 区域(预览被裁剪)。

虽然我无法具体跟踪问题,但我找到了解决方法。似乎在打开相机的过程中 mTextureView 的大小发生变化时会发生失真。延迟相机打开过程可以解决此问题。

修改openCamera方法:

/**
 * Opens the camera specified by {@link StepFragmentCamera#mCameraId}.
 */
private void openCamera(int width, int height) {
    startBackgroundThread();

    if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED) {
        requestCameraPermission();
        return;
    }
    setUpCameraOutputs(width, height);
    configureTransform(width, height);

    /*
     * Delay the opening of the camera until (hopefully) layout has been fully settled.
     * Otherwise there is a chance the preview will be distorted (tested on Sony Xperia Z3 Tablet Compact).
     */
    mTextureView.post(new Runnable() {
        @Override
        public void run() {
            /*
             * Carry out the camera opening in a background thread so it does not cause lag
             * to any potential running animation.
             */
            mBackgroundHandler.post(new Runnable() {
                @Override
                public void run() {
                    Activity activity = getActivity();
                    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
                    try {
                        if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
                            throw new RuntimeException("Time out waiting to lock camera opening.");
                        }
                        manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
                    } catch (CameraAccessException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
                    }
                }
            });
        }
    });
}

关于Android Camera2 API 预览有时会失真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43319318/

相关文章:

android 网络服务响应 "parse xml to pojo exception"

preview - 强制特定的测试变体作为谷歌优化中的预览

java - android应用程序的内存分配问题

android - 使用Android的相机拍摄灰度照片2

android - 使用 Google 的 Volley 设置缓存的过期策略

android - 使用 android 媒体播放器进行渐进式视频下载

jquery - 为什么jquery中的图像预览无法显示任何图像?

android - 在间隔拍摄 session 中拍摄多张照片?

java - 在模拟器/手机中运行应用程序时出现 NoClassDefFoundError

html - 如何使用 Visual Studio Code 在浏览器中查看 HTML 文件