MTK 设备中setPreviewTexture 失败

问题描述 投票:0回答:3

最近我正在学习grafika的android Camera和OpenglES(感谢fadden)。它在大多数设备上都很好,但我在某些设备上遇到错误,特别是MTK设备(例如MT6580,MT8163...)。

例如,当“CameraCaptureActivity”在 MTK 中运行时。我收到此错误:

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.hardware.Camera.setPreviewTexture(android.graphics.SurfaceTexture)”

所以我将“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();
}

然后错误更改为:

java.lang.RuntimeException:java.io.IOException:setPreviewTexture 失败 在jp.co.cyberagent.android.gpuimage.grafika.CameraCaptureActivity.handleSetSurfaceTexture(CameraCaptureActivity.java:1150)

我阅读了很多其他相机应用程序源代码,我猜想也许MTK设备中的相机和SurfaceRender存在同步问题。所以我像这样更改代码:

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)如何安全使用surfaceRender/Camera/SurfaceTexture?

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,支持Es2 = true

nGLVersion = 196608,支持Es2 = 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 failed”。 释放相机时我收到一些日志:

CameraClient native_window_api_disconnect 失败:管道损坏 (-32)

重新打开相机时:

CameraClient native_window_api_connect 失败:没有此类设备(-19)

这些设备可能有问题,但我还在这些设备中测试了其他一些相机应用程序,其中一些表现良好。所以它一定有更好的方式来使用Camera和glsurfaceview。

android android-camera glsurfaceview grafika
3个回答
2
投票

我在语句 mCamera.setPreviewDisplay(holder) 上遇到了同样的问题,通过放置语句解决了

camera.stopPreview();

之前

mCamera.setPreviewDisplay(holder)

1
投票

正如 fadden 所说,也许存在导致“setPreviewTexture 失败”的竞争条件。最后我从谷歌camera2示例代码中找到了一个解决方案:https://github.com/googlesamples/android-Camera2Basic/blob/master/Application/src/main/java/com/example/android/camera2basic/Camera2BasicFragment.java 。它使用“信号量”来解决这个问题。


0
投票

我最近遇到了同样的问题,这篇文章很有帮助。过去 6 年里,我一直在 Android 驱动的行车记录仪中使用经过高度修改的 grafika 连续捕获程序版本,效果非常出色。中国制造商最近发布了一款带有多个摄像头的设备,5个同时录制摄像头,我的程序因这个错误而失败。这是一个竞争条件,因为我正在使用多线程环境,最多有 5 个相机实例,一些 SurfaceTexture 未正确释放。

解决方案是,打开相机后,确保相机已打开,然后在开始预览之前设置

mCamera.setPreviewDisplay(null);

希望这对某人有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.