1. 程式人生 > >yuv與rgb資料流寫成jpeg檔案模板

yuv與rgb資料流寫成jpeg檔案模板

YUV轉圖片如果偏藍色,則證明是nv12,nv12 與nv21的資料反了生成的圖片與rgb與bgr反了效果一樣。

YUV生成檔案圖片

    private void toJPG(byte[] bytes, int nWidth, int nHeigh) {
        Rect rect = new Rect(0, 0, nWidth, nHeigh);
        LogUtils.e(TAG, "###width = " + nWidth + "height = " + nHeigh);
//        YuvImage yuvImage = new YuvImage(bytes, ImageFormat.YUV_420_888, nWidth, nHeigh, null);
        YuvImage yuvImage = new YuvImage(bytes, ImageFormat.NV21, nWidth, nHeigh, null);
        Bitmap rawbitmap = null;
        ByteArrayOutputStream outputstream = null;
        FileOutputStream fileOutputStream = null;
        File file = new File(Environment.getExternalStorageDirectory() + File.separator + "zgzq/yuvPic");
        boolean mkdirs = file.mkdirs();
        String pigPigPath = Environment.getExternalStorageDirectory() + File.separator + "zgzq/yuvPic" + File.separator + System.currentTimeMillis() + "yv.jpg";
        try {
            outputstream = new ByteArrayOutputStream();

            yuvImage.compressToJpeg(rect, 100, outputstream);
            rawbitmap = BitmapFactory.decodeByteArray(outputstream.toByteArray(), 0, outputstream.size());
            //旋轉90度
//            rawbitmap = Bitmap.createBitmap(rawbitmap, 0, 0, rawbitmap.getWidth(), rawbitmap.getHeight(), null, true);
            fileOutputStream = new FileOutputStream(pigPigPath);
            boolean compress = rawbitmap.compress(Bitmap.CompressFormat.JPEG, 60, fileOutputStream);
//            boolean compress = rawbitmap.compress(Bitmap.CompressFormat.JPEG, 60, fileOutputStream);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (outputstream != null) outputstream.close();
                if (fileOutputStream != null)
                    fileOutputStream.close();
                if (rawbitmap != null && !rawbitmap.isRecycled()) {
                    rawbitmap.recycle();
                    rawbitmap = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

RGB:生成圖片檔案

private void writeImgToFileBmp(byte[] bytes, int width, int height) {
        if (bytes.length != 0){
            BitmapFactory.Options options= new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.RGB_565;
            Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length,options);
            File file = new File(Environment.getExternalStorageDirectory() + File.separator + "images_test");
            boolean mkdirs = file.mkdirs();

            String imgPath = Environment.getExternalStorageDirectory() + File.separator + "images_test" + File.separator + System.currentTimeMillis() + "rgb.jpeg";

            try {
                FileOutputStream fos = new FileOutputStream(imgPath);
                bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }