我正在使用 Camera2 API 构建自定义相机。
到目前为止,除了预览有时会失真之外,相机工作得很好。假设我连续打开相机 7 次。所有尝试均成功,但第 8 次相机预览失真。看起来它使用宽度作为高度,反之亦然。
我的代码基于camera2的Google示例实现,可以找到here https://github.com/googlesamples/android-Camera2Basic。
有趣的是,即使是 Google 示例实现有时也会出现这种扭曲的预览。我尝试修改 AutoFitTextureView 但没有成功。我目前正在使用 Google 再次提供的 AutoFitTextureView.java 。
与此类似的帖子可以在这里找到 https://stackoverflow.com/questions/30049293/camera-preview-stretched-on-few-android-devices。
然而,所提出的修复方案并没有解决问题。
我可以通过更改 setUpCameraOutputs 方法中的以下内容来重现该问题:
mTextureView.setAspectRatio(mPreviewSize.getHeight(), mPreviewSize.getWidth());
to:
mTextureView.setAspectRatio(mPreviewSize.getWidth(), mPreviewSize.getHeight());
另一个奇怪的事情是,每当预览失真时,您只需按主页按钮,应用程序就会进入 onPause() 并再次打开应用程序,以便调用 onResume() ,每次预览都是完美的。
这里有人遇到过这个问题并找到解决办法吗?
提前致谢
我在 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);
}
}
});
}
});
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)