NV21字节格式从at转换mp4

时间:2018-12-14 06:51:54

标签: android h.264 mediacodec android-mediacodec

enter image description here我正在从h264视频格式中获得此黑帧,我从具有NV21字节格式的相机中获得了数据流,我可以将其转换为位图,并以png格式显示,但是我想将其保存为.mp4格式我该怎么办,如何转换成mp4格式呢?

private CameraProxy.CameraDataCallBack callBack = new CameraProxy.CameraDataCallBack() {

    @Override
    public void onDataBack(byte[] data, long length) {
        // TODO Auto-generated method stub
        Log.i(TAG, "length . " + length);

      //NV21 -> yuvImage -> jpeg -> bitmap
        YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21, realPreviewSize.getWidth(), realPreviewSize.getHeight(), null);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        yuvImage.compressToJpeg(new Rect(0, 0, realPreviewSize.getWidth(), realPreviewSize.getHeight()), 100, os);
        byte[] jpegByteArray = os.toByteArray();
        Bitmap bitmap = BitmapFactory.decodeByteArray(jpegByteArray, 0, jpegByteArray.length);
        try {
            fos = new FileOutputStream(photo);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        try {
            fos.close();
           Bitmap img = BitmapFactory.decodeFile(photo.getAbsolutePath());
           runOnUiThread(new Runnable() {
               @Override
               public void run() {
                   im.setImageBitmap(img);
               }
           });

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
};

0 个答案:

没有答案