本文介绍了如何从Android的程序设计摄像头应用程序捕捉preVIEW图像帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个应用程序来捕捉摄像头preVIEW帧,并将其转换在Android中为位图。这是我的code:

I am writing an app to capture the camera preview frames and convert it to bitmap in Android. Here is my code:

   Camera.PreviewCallback previewCallback = new Camera.PreviewCallback()  
    { 
            public void onPreviewFrame(byte[] data, Camera camera)  
            { 
                    try 
                    { 
                            BitmapFactory.Options opts = new BitmapFactory.Options(); 
                            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);//,opts); 
                    } 
                    catch(Exception e) 
                    {

                    } 
            } 

    }; 

    mCamera = Camera.open();
    mCamera.setPreviewCallback(previewCallback); 

在我开始preVIEW,回调得到调用数据,而是位为空。

After I start preview, the callback got called with data, but the bitmap is null.

我做了什么错了,当转换为字节数组位图?

What did I do wrong when convert the byte array to BitMap?

推荐答案

在previewFrame()函数,你应该先检查图像格式。
这在NV21的例子。

In the onPreviewFrame() function, you should check the image format first.
This the NV21 example.

public void onPreviewFrame(byte[] data, Camera camera) 
{
    Parameters parameters = camera.getParameters();
    imageFormat = parameters.getPreviewFormat();
    if (imageFormat == ImageFormat.NV21)
    {
        Rect rect = new Rect(0, 0, PreviewSizeWidth, PreviewSizeHeight); 
        YuvImage img = new YuvImage(data, ImageFormat.NV21, PreviewSizeWidth, PreviewSizeHeight, null);
        OutputStream outStream = null;
        File file = new File(NowPictureFileName);
        try 
        {
            outStream = new FileOutputStream(file);
            img.compressToJpeg(rect, 100, outStream);
            outStream.flush();
            outStream.close();
        } 
        catch (FileNotFoundException e) 
        {
            e.printStackTrace();
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
}

另取照片的方式,检查本文如何使用相机安卓

这篇关于如何从Android的程序设计摄像头应用程序捕捉preVIEW图像帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 00:49