最近,我正在通过grafika学习安卓相机和OpenglES (谢谢fadden),这在大多数设备上都很好,但我在某些设备中遇到了错误,尤其是MTK设备(比如MT6580、MT8163.)。
例如,当"CameraCaptureActivity“在MTK中运行时。我得到了这个错误:
android.hardware.Camera.setPreviewTexture(android.graphics.SurfaceTexture)‘:尝试在空对象引用上调用虚拟方法的void java.lang.NullPointerException
因此,我将"handleSetSurfaceTexture“函数改为:
private void handleSetSurfaceTexture(SurfaceTexture st) {
if(mCamera == null)
{
Log.e(TAG, "mCamera return null");
return;
}
st.setOnFrameAvailableListener(this);
try {
mCamera.setPreviewTexture(st);
} catch (Exception ioe) {
Log.e(TAG, "camera failed handleSetSurfaceTexture");
throw new RuntimeException(ioe);
}
mCamera.startPreview();
}然后错误更改为:
jp.co.cyberagent.android.gpuimage.grafika.CameraCaptureActivity.handleSetSurfaceTexture(CameraCaptureActivity.java:1150) java.lang.RuntimeException: java.io.IOException: setPreviewTexture失败
我读了很多其他摄像头应用程序的源代码,我想可能是相机和SurfaceRender在MTK设备上的同步问题。所以我修改代码如下:
private void waitUntilSetup()
{
long l = System.currentTimeMillis();
while ((getMaxTextureSize() == 0) && (System.currentTimeMillis() - l < 3000L))
{
SystemClock.sleep(100L);
}
Log.e(TAG,"getMaxTextureSize() = " + getMaxTextureSize());
}
private int getMaxTextureSize() {
int[] maxTextureSize = new int[1];
GLES20.glGetIntegerv(GL10.GL_MAX_TEXTURE_SIZE, maxTextureSize, 0);
Log.e(TAG, "Max texture size = " + maxTextureSize[0]);
return maxTextureSize[0];
}
private void handleSetSurfaceTexture(SurfaceTexture st) {
//wait for gl
waitUntilSetup();
if(mCamera == null)
{
Log.e(TAG, "mCamera return null");
return;
}
st.setOnFrameAvailableListener(this);
try {
mCamera.setPreviewTexture(st);
} catch (Exception ioe) {
Log.e(TAG, "camera failed handleSetSurfaceTexture");
throw new RuntimeException(ioe);
}
mCamera.startPreview();
}不幸的是,"getMaxTextureSize()“在其他设备中返回一个有用的数字,但我在MTK设备中只得到了getMaxTextureSize()=0。
所以我有以下问题:
( 1)如何安全使用表面Camera/照相机/表面结构?
2)为什么MTK会出现这样的问题?
任何答案都将不胜感激。
我把这个加起来再测试一次
//get glVersion
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
int nGLVersion = configurationInfo.reqGlEsVersion;
final boolean supportsEs2 = (nGLVersion >= 0x20000);
Log.e(TAG, "nGLVersion = " + nGLVersion + ", supportsEs2 = " + supportsEs2);其中两个设备的结果是:
nGLVersion = 131072,supportsEs2 = true nGLVersion = 196608,supportsEs2 = true
我还得到设备信息:
String strDevice = Devices.getDeviceName(); //https://gist.github.com/jaredrummler/16ed4f1c14189375131d
String strModel = Build.MODEL;
int nVersion = Build.VERSION.SDK_INT;
Log.e(TAG, "strDeviceName = " + strDevice + ", strModel =" + strModel + ", nVersion =" + nVersion);结果:
strDevice =阿尔卑斯山k80_gmo,strModel =k80_gmo,nVersion =22 strDevice =阿尔卑斯山tb8163p3_64_sph,strModel =tb8163p3_64_sph,nVersion =22
顺便说一句,它可以在第一次打开相机和开始预览。但是当活动暂停或重新打开相机时,会遇到"setPreviewTexture失败“。我得到了一些日志,当释放相机时:
CameraClient native_window_api_disconnect失败:管道破裂(-32)
重新打开摄像机时:
CameraClient native_window_api_connect失败:没有这样的设备(-19)
这些设备可能有问题,但我也在这些设备上测试了一些其他摄像头应用程序,其中一些性能很好。因此,它必须有一个更好的方式使用相机和玻璃表面视图。
发布于 2015-11-26 10:41:51
正如法登所说,也许有一种种族条件导致了"setPreviewTexture失败“。最后,我从google camera2示例代码中找到了一个解决方案:https://github.com/googlesamples/android-Camera2Basic/blob/master/Application/src/main/java/com/example/android/camera2basic/Camera2BasicFragment.java。它使用“信号量”来解决这个问题。
发布于 2016-07-18 09:08:38
我在报表mCamera.setPreviewDisplay(持卡人)上得到了同样的问题,通过写声明解决了
camera.stopPreview();在此之前
mCamera.setPreviewDisplay(holder)https://stackoverflow.com/questions/31943963
复制相似问题