2017-04-10 66 views
1

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

我已经将我的代码基于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()); 

另一个奇怪的是,每当发生扭曲预览,您只需按home按钮,所以应用程序进入onPause()并再次打开应用程序,所以onResume()被调用,预览每次都是完美的。

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

在此先感谢

+1

你有没有找到任何解决办法?我也面临这个问题。 –

+0

我也面临同样的问题 – FaisalAhmed

回答

0

的谷歌Camera2Basic样品was finally fixed。原始代码有一个微小的< >错误。 2年来一直是错误的。

0

我在Sony Xperia Z3 Tablet Compact上面临同样的问题。

亚历克斯指出的拉请求似乎不适用于我。它导致相机预览大于视图区域(预览被裁剪)。

虽然我无法专门跟踪此问题,但我找到了解决方法。似乎在打开相机的过程中,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); 
        } 
       } 
      }); 
     } 
    }); 
} 
相关问题