2

我正在开发一个必须能够基于 androids camera2 API 进行运动检测的应用程序。所以我正在使用 ImageReader 和相应的 OnImageAvailableListener。我有两个用于 CameraCaptureSession 框架的目标表面、显示相机预览的 UI SurfaceTexture 和 ImageReaders 表面。

出于与此问题中提到的相同原因,我有点“被迫”使用 ImageFormat.YUV_420_88 :

ImageReader 使相机滞后

我对这种格式有点陌生,我需要实现运动检测。

我的想法是循环遍历 Image.getPlanes()[i].getBuffer() 像素,并根据某个阈值比较两个图像。

我有两个问题:

  1. 这是一个好主意吗?有没有更好/更有效的方法?

  2. 格式 YUV_420_88 中的哪个平面最适合这种运动检测?

4

1 回答 1

-1
  1. 这是一个好主意吗?有没有更好/更有效的方法?

是的。没有其他方法可以比较连续的帧来检测运动事件。

  1. 格式 YUV_420_88 中的哪个平面最适合这种运动检测?

我认为是Planes()[0]。

我的示例代码:

mOnImageAvailableListener = new ImageReader.OnImageAvailableListener() {
        @Override
        public void onImageAvailable(ImageReader imageReader) {
            Image image = null;
            try {
                image = imageReader.acquireLatestImage();
                if (null == image) {
                    return;
                }
                ByteBuffer buffer = image.getPlanes()[0].getBuffer();
                byte[] bytes = new byte[buffer.capacity()];
                buffer.get(bytes);

                detectMotion(bytes);
            } finally {
                if (image != null) {
                    image.close();
                }
            }
        }

        private void detectMotion(byte[] frame) {
            if (null == frame) {
                return;
            }

            int ret = NativeUtil.detectMotion(frame);
            if (ret == 1) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mIvMotion.setVisibility(View.VISIBLE);
                    }
                });
                Log.d(TAG, "Detected motion event");
            } else {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mIvMotion.setVisibility(View.INVISIBLE);
                    }
                });
            }
        }
    };
于 2018-07-27T09:36:21.860 回答