简体   繁体   English

将原始相机字节 [] 数组用于增强现实

[英]Using the raw camera byte[] array for augmented reality

I'm developing an Augmented Reality app, so I need to capture the camera preview, add visual effects to it, and display it on screen.我正在开发一个增强现实应用程序,因此我需要捕获相机预览,为其添加视觉效果,并将其显示在屏幕上。 I would like to do this using the onPreviewFrame method of PreviewCallback .我想使用PreviewCallbackonPreviewFrame方法来做到这一点。 This gives me a byte[] variable containing raw image data (YUV420 encoded) to work with.这给了我一个byte[]变量,其中包含要使用的原始图像数据(YUV420 编码)。

Even though I searched for a solution for many hours, I cannot find a way to convert this byte[] variable to any image format I can work with or even draw on the screen.尽管我搜索了很多小时的解决方案,但我找不到将这个byte[]变量转换为我可以使用甚至在屏幕上绘制的任何图像格式的方法。

Preferably, I would convert the byte[] data to some RGB format that can be used both for computations and drawing.最好将byte[]数据转换为可用于计算和绘图的某种 RGB 格式。

Is there a proper way to do this?有没有合适的方法来做到这一点?

I stumbled upon the same issue a few months back when I had to do some edge detection on the camera frames.几个月前,当我不得不对相机框架进行边缘检测时,我偶然发现了同样的问题。 This works perfectly for me.这对我来说非常有效。 Try it out.试试看。

public void surfaceChanged(SurfaceHolder holder,int format, int width,int height) 
        {
            camera.setPreviewCallback(new PreviewCallback() {

                public void onPreviewFrame(byte[] data, Camera camera) {

                    Camera.Parameters parameters = camera.getParameters();

                    int width = parameters.getPreviewSize().width;
                    int height = parameters.getPreviewSize().height;

                    ByteArrayOutputStream outstr = new ByteArrayOutputStream();
                    Rect rect = new Rect(0, 0, width, height); 
                    YuvImage yuvimage=new YuvImage(data,ImageFormat.NV21,width,height,null);
                    yuvimage.compressToJpeg(rect, 100, outstr);
                    Bitmap bmp = BitmapFactory.decodeByteArray(outstr.toByteArray(), 0, outstr.size());
                }
}
}

You can use the bitmap for all your processing purposes now.您现在可以将 bitmap 用于所有处理目的。 Get the interested pixel and you can comfortably do your RGB or HSV stuff on it.获取感兴趣的像素,您可以轻松地在其上进行 RGB 或 HSV 处理。

Imran Nazar has writen a two part tutorial on augmented reality which you may find useful. Imran Nazar 编写了一个关于增强现实的两部分教程,您可能会发现它很有用。 Although he eventually uses the NDK, the first part and most of the second part detail what you need using just Java.尽管他最终使用了 NDK,但第一部分第二部分的大部分内容仅使用 Java 详细说明了您需要什么。

I believe Bitmap.createBitmap is the method you need.我相信Bitmap.createBitmap是您需要的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM