19

我正在使用新的 Android camera2 API 进行测试,我想控制来自相机的每一帧。我为此做的是创建一个ImageReader并设置分辨率和图像格式。

ImageReader imageReader = ImageReader.newInstance(1280,720,ImageFormat.YUV_420_888,1);
imageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
    @Override
    public void onImageAvailable(ImageReader reader) {
        Image image = reader.acquireLatestImage();
        Log.i(MainActivity.LOG_TAG,"imageReader: "+System.currentTimeMillis());
        image.close();
    }
},null);

之后,我使用参数TEMPLATE_PREVIEW创建新的CaptureRequest并将目标添加到他。对于cameraCaptureSession ,我使用此请求创建新的setRepeatingRequestimageReader

 try {
    final CaptureRequest.Builder builder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);

    builder.addTarget(imageReader.getSurface());

    mCameraDevice.createCaptureSession(
            Arrays.asList(imageReader.getSurface()),
            new CameraCaptureSession.StateCallback() {
                @Override
                public void onConfigured(CameraCaptureSession session) {
                    mSession = session;
                    try {
                        mSession.setRepeatingRequest(builder.build(),null,null);
                    } catch (CameraAccessException e) {
                        e.printStackTrace();
                    }
                }
                @Override
                public void onConfigureFailed(CameraCaptureSession session) {

                }
            },
            null
    );
} catch (CameraAccessException e) {
    e.printStackTrace();
}

当我打开相机并开始查看我所拥有的预览时,我得到了异常。但在异常之前我得到了一些预览图像,之后我有异常

10-30 16:00:32.850    1390-1894/.camera2tutorial E/BufferQueueProducer﹕ [unnamed-1390-1] dequeueBuffer: BufferQueue has been abandoned
10-30 16:00:32.850    1390-1894/.camera2tutorial E/Legacy-CameraDevice-JNI﹕ LegacyCameraDevice_nativeProduceFrame: Error while producing frame No such device (-19).
10-30 16:00:32.850    1390-1894/.camera2tutorial W/SurfaceTextureRenderer﹕ Surface abandoned, dropping frame.
    android.hardware.camera2.legacy.LegacyExceptionUtils$BufferQueueAbandonedException
            at android.hardware.camera2.legacy.LegacyExceptionUtils.throwOnError(LegacyExceptionUtils.java:64)
            at android.hardware.camera2.legacy.LegacyCameraDevice.produceFrame(LegacyCameraDevice.java:516)
            at android.hardware.camera2.legacy.SurfaceTextureRenderer.drawIntoSurfaces(SurfaceTextureRenderer.java:699)
            at android.hardware.camera2.legacy.GLThreadManager$1.handleMessage(GLThreadManager.java:103)
            at android.os.Handler.dispatchMessage(Handler.java:98)
            at android.os.Looper.loop(Looper.java:145)
            at android.os.HandlerThread.run(HandlerThread.java:61)

我怎样才能解决这个问题??

我正在使用三星 Galaxy S5 和 Android API 21

4

3 回答 3

32

确保您持有对您创建的 ImageReader 的引用,可能在您定义了 mSession 的任何地方。

从 ImageReader 获得的 Surface 大致相当于一个弱指针——它不会阻止 ImageReader 收集垃圾。所以很可能(根据您的命名) ImageReader 被破坏,然后发生放弃错误。

于 2015-11-02T00:19:18.977 回答
2

在我的应用程序中的活动之间切换时我遇到了同样的问题,那是在调用onSurfaceTextureDestroyed()它只返回 false 之后,但我所做的是我将它更改为

public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
            Log.e(TAG, "onSurfaceTextureDestroyed");
            if(cameraDevice != null){
                closeCamera();

                cameraDevice = null;
            }
            return false;
        }

这对我有用。

于 2017-01-28T06:55:06.417 回答
0

我找到了解决方案,这对我有用,感染错误在于选择正确的尺寸,因此在使用 MediaRecorder.setVideoSize() 时使用此方法选择最佳尺寸

private static Size chooseOptimalSize(Size[] choices, int width, int height) {
        Size bigEnough = null;
        int minAreaDiff = Integer.MAX_VALUE;
        for (Size option : choices) {
            int diff = (width*height)-(option.getWidth()*option.getHeight()) ;
            if (diff >=0 && diff < minAreaDiff &&
                    option.getWidth() <= width &&
                    option.getHeight() <= height) {
                minAreaDiff = diff;
                bigEnough = option;
            }
        }
        if (bigEnough != null) {
            return bigEnough;
        } else {
            Arrays.sort(choices,new CompareSizeByArea());
            return choices[0];
        }

    }
于 2019-01-21T17:22:10.447 回答