下载 Xamarin Camera2Basic 项目
在 Visual Studio 2017 模拟器中开始测试时,最初一切正常。
注释掉“CameraCaptureStillPictureSessionCallback.cs”中的代码,如下所示,以允许在单击“拍照”按钮后捕获的静止图像显示在模拟器屏幕上:
public override void OnCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result)
{
//Owner.ShowToast("Saved: " + Owner.mFile);
//Log.Debug(TAG, Owner.mFile.ToString());
//Owner.UnlockFocus();
}
单击“拍照”按钮会隐藏自身,并在屏幕上显示另一个标记为“重拍照片”的按钮(这是我在事后添加的,代码位于 Camera2BasicFragment.cs 文件中,但代码对问题)。
单击现在可见的“重拍照片”按钮会执行以下操作:
- 隐藏自己
- 显示“拍照”按钮
- 调用 UnlockFocus()
UnlockFocus() 允许相机的流连续显示在屏幕上,而不是之前捕获的静止图像。
现在,当我再次点击“拍照”按钮(尝试拍摄新的静止图像)时,应用程序崩溃了。
Visual Studio 不提供任何有意义的错误消息。最接近有用的信息是设备日志中显示的错误消息:
07-26 23:29:03.201 10.1" Marshmallow (6.0.0) XHDPI Tablet Error 6987 BufferQueueProducer [ImageReader-640x480f100m2-6987-0] dequeueBuffer: can't dequeue multiple buffers without setting the buffer count
07-26 23:29:07.174 10.1" Marshmallow (6.0.0) XHDPI Tablet Error 6987 RequestThread-0 Hit timeout for jpeg callback!
07-26 23:29:03.201 10.1" Marshmallow (6.0.0) XHDPI Tablet Error 6987 Legacy-CameraDevice-JNI LegacyCameraDevice_nativeProduceFrame: Error while producing frame Function not implemented (-38).
我不确定如何处理这些错误,或者要更改哪些设置/代码以允许“重拍图片”功能在不导致应用程序崩溃的情况下运行。
有什么建议吗?
编辑 1: 根据请求,我目前拥有的项目链接在哪里。
https://drive.google.com/file/d/0B7OSuA_ybXcFb081T210UlQzZkE/view?usp=sharing
这里是一些其他看似相关的信息:
此代码使用:
一个。 Windows 10 专业版、Visual Studio 2017 社区、Android 模拟器 对于 Visual Studio、Hyper-v 虚拟管理器、Android 6.0 (Marshamallow SDK 23),平板电脑大小的模板
2013 Macbook Pro,Visual Studio For Mac(最新版本),默认 emaulator,Android 6.0 (Marshmallow SDK 23),平板电脑大小的模板。
在两种环境中都观察到在第二次“LockFocus”调用后未能拍摄快照。
Mac 让我们更容易找到一些更有意义的错误:
一个。我看到的错误发生在方法“produceFrame”里面 LegacyCameraDevice.java
最佳答案
我也有这个演示的问题,这背后的真正问题是关于 IOnImageAvailableListener,它不会触发 OnImageAvailable 方法来保存图片...
阅读代码后,我发现该演示已损坏,缺少一些部分。 在 Camera2BasicFragment.cs 的 OnCreateMethod 中,您需要添加这一行
mCaptureCallback = new CameraCaptureListener() { Owner = this};
整个方法应该是这样的:
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
mStateCallback = new CameraStateListener() { owner = this };
mSurfaceTextureListener = new Camera2BasicSurfaceTextureListener(this);
// fill ORIENTATIONS list
ORIENTATIONS.Append((int)SurfaceOrientation.Rotation0, 90);
ORIENTATIONS.Append((int)SurfaceOrientation.Rotation90, 0);
ORIENTATIONS.Append((int)SurfaceOrientation.Rotation180, 270);
ORIENTATIONS.Append((int)SurfaceOrientation.Rotation270, 180);
}
有趣的是,如果你在模拟器上运行它,什么也不会发生,因为在 CameraCaptureListener.cs 文件中,它总是返回 0,而不是 ControlAFState.FocusedLocked 或 ControlAFState.InActivate
Integer afState = (Integer)result.Get(CaptureResult.ControlAfState);
事件如果我破解 If 方法能够运行跳转到下一步,ImageAvailableListener.cs 的 OnImageAvaiable 方法将永远不会被模拟器触发。但如果我在真实设备上运行,它运行良好?!
所以解决方法是: 1. 添加上面的代码,就像我在 onCreate 函数中解释的那样。 2. 不要用模拟器运行Camera2,它有bug。
希望对你有帮助:)
关于android - Xamarin Camera2Basic 示例在 UnlockFocus 调用后引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45343217/