有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android ImageReader运动检测

我正在开发一个应用程序,它必须能够基于安卓s camera2 API进行运动检测。所以我正在使用一个图像阅读器和相应的OnImageAvailableListener。我有两个用于CameraCaptureSession帧的目标曲面,即显示相机预览的UI SurfaceTexture和ImageReaders曲面

我有点“被迫”使用ImageFormat。YUV_420_88与本问题中提到的原因相同:

ImageReader makes Camera lag

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

我的想法是通过图像循环。getPlanes()[i]。getBuffer()以像素为单位,根据特定阈值比较两幅图像

我有两个问题:

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

  2. YUV_420_88格式的哪个平面最适合此类运动检测


共 (1) 个答案

  1. # 1 楼答案

    1. Is this a good idea? Is there maybe a better / more efficient way?

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

    1. Which of the Plane(s) from the Format YUV_420_88 is best suited for such motion detection?

    我想是飞机[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);
                        }
                    });
                }
            }
        };