Android Camera2 API预览有时会失真 [英] Android Camera2 API preview sometimes distorted

查看:596
本文介绍了Android Camera2 API预览有时会失真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am building a custom camera with the Camera2 API. So far the camera works very well except for the preview which is distorted sometimes. Let's say I open the camera 7 times in a row. All of the attempts are succesful and the 8th time the camera preview is distorted. It looks like it uses the width as the height and vice versa.

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

I have based my code on the Google sample implementation of the camera2 which can be found here. the interesting thing is that even the Google sample implementation has this distorted preview sometimes. I have tried to modify the AutoFitTextureView but nothing was successful. I am currently using the AutoFitTextureView.java Google provides again.

与此类似的帖子在此处可以找到. 但是,建议的修补程序无法解决问题.

A similar post to this one can be found here. However the proposed fixes didn't solve the problem.

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

I can reproduce the problem by changing the following in the setUpCameraOutputs method:

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

收件人:

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

另一个奇怪的事情是,每当发生扭曲的预览时,只要按一下主屏幕按钮,应用程序就会进入onPause()并再次打开该应用程序,以便调用onResume(),每次预览都是完美的.

Another weird thing is that whenever the distorted preview occurs and you just press the home button so the app goes in onPause() and open up the app again so onResume() gets called, the preview is perfect every time.

这里有没有人遇到此问题并找到解决方法的?

Has anyone here experienced this problem and found a fix for it?

预先感谢

推荐答案

我在Sony Xperia Z3 Tablet Compact上遇到相同的问题.

I am facing the same issue on Sony Xperia Z3 Tablet Compact.

Alex指出的拉动请求似乎对我不起作用.这样会导致相机预览大于视图的区域(裁剪的预览).

The pull request that Alex pointed out doesn't seem to work for me. It results in camera preview larger than the view's area (preview is cropped).

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

While I was not able to track the issue specifically, I was able to find a workaround. It seems the distortion happens while there are changes of mTextureView's size while in process of opening camera. Delaying the camera opening procedure fixes the issue.

修改后的openCamera方法:

Modified openCamera method:

/**
 * 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预览有时会失真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆