如何NV21图像格式转换成位图? [英] How to convert NV21 image format in bitmap?

查看:2878
本文介绍了如何NV21图像格式转换成位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从相机获得preVIEW数据。它是在NV21格式。我想保存preVIEW到SD卡即位图。我的code越来越图像并保存,但在画廊它不是捕捉preVIEW。这仅仅是黑色矩形,这里是code。

 公共无效processImage来(){    位图位图= NULL;    如果(标志==真){        标志= FALSE;        如果(mCamera!= NULL){            Camera.Parameters参数= mCamera.getParameters();            INT的imageformat = parameters.get previewFormat();            如果(==的imageformat ImageFormat.NV21){
                Toast.makeText(mContext,格式:NV21,Toast.LENGTH_SHORT)
                        。显示();
                。INT W = parameters.get previewSize()宽度;
                。INT H = parameters.get previewSize()高度;                YuvImage yuvImage =新YuvImage(MDATA,的imageformat,W,H,
                        空值);                矩形矩形=新的Rect(0,0,W,H);                ByteArrayOutputStream BAOS =新ByteArrayOutputStream();                yuvImage.com pressToJpeg(RECT,100,BAOS);                字节[] = jData baos.toByteArray();                位= BitmapFactory.de codeByteArray的(jData,0,
                        jData.length);
            }            否则,如果(==的imageformat ImageFormat.JPEG
                    || ==的imageformat ImageFormat.RGB_565){                Toast.makeText(mContext,格式:JPEG || RGB_565
                        Toast.LENGTH_SHORT).show();
                位= BitmapFactory.de codeByteArray的(MDATA,0,
                        mData.length);
            }
        }        如果(位图!= NULL){
            saveImage(位图);
            Toast.makeText(mContext,图像保存,Toast.LENGTH_SHORT)
                    。显示();
        }其他
            Toast.makeText(mContext,位图NULL,Toast.LENGTH_SHORT)
                    。显示();
    }
}


解决方案

如果您想保存NV21 preVIEW图片查看画廊中的最简单方法是创建一个从NV21字节数组,然后COM的YuvImage $ p $它PSS为JPEG的文件输出流,如下面的code:

 的FileOutputStream FOS =新的FileOutputStream(Environment.getExternalStorageDirectory()+/imagename.jpg);
YuvImage yuvImage =新YuvImage(nv21bytearray,ImageFormat.NV21,宽度,高度,NULL);
yuvImage.com pressToJpeg(新的Rect(0,0,宽度,高度),100,FOS);
fos.close();

请注意,您可能要更改路径保存图像。

I am getting preview data from camera. It is in NV21 format. I want to save the preview to SD Card i.e in bitmap. My code is getting image and saving it, but in the gallery it is not captured preview. It is just black rectangle, Here is the code.

    public void processImage() {

    Bitmap bitmap = null;

    if (flag == true) {

        flag = false;

        if (mCamera != null) {

            Camera.Parameters parameters = mCamera.getParameters();

            int imageFormat = parameters.getPreviewFormat();

            if (imageFormat == ImageFormat.NV21) {
                Toast.makeText(mContext, "Format: NV21", Toast.LENGTH_SHORT)
                        .show();
                int w = parameters.getPreviewSize().width;
                int h = parameters.getPreviewSize().height;

                YuvImage yuvImage = new YuvImage(mData, imageFormat, w, h,
                        null);

                Rect rect = new Rect(0, 0, w, h);

                ByteArrayOutputStream baos = new ByteArrayOutputStream();

                yuvImage.compressToJpeg(rect, 100, baos);

                byte[] jData = baos.toByteArray();

                bitmap = BitmapFactory.decodeByteArray(jData, 0,
                        jData.length);
            }

            else if (imageFormat == ImageFormat.JPEG
                    || imageFormat == ImageFormat.RGB_565) {

                Toast.makeText(mContext, "Format: JPEG||RGB_565",
                        Toast.LENGTH_SHORT).show();
                bitmap = BitmapFactory.decodeByteArray(mData, 0,
                        mData.length);
            }
        }

        if (bitmap != null) {
            saveImage(bitmap);
            Toast.makeText(mContext, "Image Saved", Toast.LENGTH_SHORT)
                    .show();
        } else
            Toast.makeText(mContext, "Bitmap Null", Toast.LENGTH_SHORT)
                    .show();
    }
}

解决方案

If you want to save the NV21 preview image to view in the gallery the easiest way is to create an YuvImage from the NV21 byte array and then compress it to a JPEG in a file output stream, like the code below:

FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory() + "/imagename.jpg");
YuvImage yuvImage = new YuvImage(nv21bytearray, ImageFormat.NV21, width, height, null);
yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, fos);
fos.close();

Please note that you probably want to change the path to save the image.

这篇关于如何NV21图像格式转换成位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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