Android camera2 API-实时显示处理过的帧 [英] Android camera2 API - Display processed frame in real time

查看:773
本文介绍了Android camera2 API-实时显示处理过的帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个实时处理相机图像并将其显示在屏幕上的应用程序.我正在使用camera2 API.我创建了一个本地库来使用OpenCV处理图像.

I'm trying to create an app that processes camera images in real time and displays them on screen. I'm using the camera2 API. I have created a native library to process the images using OpenCV.

到目前为止,我已经设法设置了一个ImageReader来接收类似YUV_420_888格式的图像.

So far I have managed to set up an ImageReader that receives images in YUV_420_888 format like this.

        mImageReader = ImageReader.newInstance(
                mPreviewSize.getWidth(),
                mPreviewSize.getHeight(),
                ImageFormat.YUV_420_888,
                4);
        mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, mImageReaderHandler);

从那里,我可以获取图像平面(Y,U和V),获取它们的ByteBuffer对象,并将它们传递给我的本机函数.这发生在mOnImageAvailableListener:

From there I'm able to get the image planes (Y, U and V), get their ByteBuffer objects and pass them to my native function. This happens in the mOnImageAvailableListener:

        Image image = reader.acquireLatestImage();

        Image.Plane[] planes = image.getPlanes();
        Image.Plane YPlane = planes[0];
        Image.Plane UPlane = planes[1];
        Image.Plane VPlane = planes[2];

        ByteBuffer YPlaneBuffer = YPlane.getBuffer();
        ByteBuffer UPlaneBuffer = UPlane.getBuffer();
        ByteBuffer VPlaneBuffer = VPlane.getBuffer();

        myNativeMethod(YPlaneBuffer, UPlaneBuffer, VPlaneBuffer, w, h);

        image.close();

在本机端,我能够从缓冲区获取数据指针,从数据创建cv::Mat并执行图像处理.

On the native side I'm able to get the data pointers from the buffers, create a cv::Mat from the data and perform the image processing.

现在,下一步将是显示经过处理的输出,但是我不确定如何显示经过处理的图像.任何帮助将不胜感激.

Now the next step would be to show my processed output, but I'm unsure how to show my processed image. Any help would be greatly appreciated.

推荐答案

我尝试了ANativeWindow方法,但是设置起来很痛苦,而且我没有正确地做到这一点.最后,我只是放弃并导入了OpenCV4Android库,该库通过将摄像机数据转换为幕后的RGBA Mat简化了事情.

I've tried the ANativeWindow approach, but it's a pain to set up and I haven't managed to do it correctly. In the end I just gave up and imported OpenCV4Android library which simplifies things by converting camera data to a RGBA Mat behind the scenes.

这篇关于Android camera2 API-实时显示处理过的帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆