YV12toI420 yuv420、NV12、YV12相互转换

在录像时设置了YV12要保存图片时

YUV420P、I420、NV12、YV12格式转NV12保存图片
GLSurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback {

Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewFormat(ImageFormat.YV12);
在使用camera设置为YV12或者NV12时要转换时所用的工具


   FileClientSender fileSender;
    UDPClientSender udpSender;
    private ExecutorService executorService;
    //如果是其他页面进来的则只显示
    public static boolean isAll = true;
    boolean isOpen = false;//false 为已打开如果是显示的则不用在设置Visibility

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

//        executorService.execute(new Runnable() {
//            @Override
//            public void run() {
//                YuvUtils.scaleAndRotateYV12ToI420(data, h264,
//                       PREVIEW_WIDTH,
//                       PREVIEW_HEIGHT, 0, 504, 896);
        //        // 获取到一帧预览数据
//        // 将yv12的帧转化为h254的帧
        final int ret = avcCodec.offerEncoder(data, h264);
        // 发送h264数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (MainActivity.client != null) {
                    udpSender.send(h264, ret);
                }
            }
        }).start();
        if (isAll) {
            if (isOpen) {
                customSurfaceView.setVisibility(VISIBLE);
                isOpen = false;
            }
            new Thread(new Runnable() {
                @Override
                public void run() {
                    fileSender.send(h264, ret);
                }
            }).start();

//            }
//        });
            new Thread(new Runnable() {
                @Override
                public void run() {
                    long l = System.currentTimeMillis();
                    if (keybs) {
                        uploodTask(data, l);
                    } else {
                        if (uploadInt == 20) {
                            uploodTask(data, l);
                            uploadInt = 0;
                        } else {
                            uploadInt++;
                        }
                    }
                }
            }).start();
        }
        camera.addCallbackBuffer(data);

    }

    int uploadInt = 0;
    public boolean keybs = false;


    @SuppressLint("NewApi")
    private void uploodTask(final byte[] data, final long l) {

        new AsyncTask() {
            @Override
            protected Boolean doInBackground(String... params) {  /*传递多个String参数*/
                // ?如果你输出格式是nv21 或者 yuy2 转图片  /**照相后的保存路径*/
                File file = new File(SavaFileNameUtil.getImageRoot() +
                        SavaFileNameUtil.getCaptureImageName(Config.USER_NUMBER));
                // ?如果你输出格式是nv21 或者 yuy2 转图片
                try {
                    byte[] NV21 = new byte[MAX_SEND_SIZE];
                    Yuv.YV12toNV21(data, NV21, PREVIEW_WIDTH, PREVIEW_HEIGHT);
                    Bitmap bmp = Yuv.NV21ToBitmap(NV21, PREVIEW_WIDTH, PREVIEW_HEIGHT);
                    //得到long类型当前时间

                    //new日期对象
                    Date date = new Date(l);
                    //转换提日期输出格式
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    bmp = createWaterMark(bmp, null, dateFormat.format(date));
                    FileOutputStream outStream = new FileOutputStream(file);
                    bmp.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
                    outStream.close();

                    LogUtils.e(TAG + "======================Ftp4jUtil", "图片写入完成----------------------------------");
                    if (keybs) {
                        //点击拍照上传
                        upload(file, ftpPath, null, false);
                        keybs = false;
                    } else {
                        //自动上传
                        upload(file, ftpPath + "_auto", null, false);

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    LogUtils.e(TAG, "Ftp4jUtil图片写入出现异常", e);
                }
                return true;
            }

//            @Override
//            protected void onPostExecute(Boolean isSuccess) {
//                if (isSuccess) {
//                    //耗时操作成功后的操作
//                } else {
//                    //打印错误
//                }
//            }
        }.execute(""); /*传递参数*/
    }

如果要保存图片,或着保存视频时使用的一个工具类
YUV420P、I420、NV12、YV12格式转NV12保存图片
// 将yv12的帧转化为h254的帧
?如果你输出格式是nv21 或者 yuy2 转图片

YUV420P、YV12、NV12、YV12格式转NV12保存图片

你可能感兴趣的:(工具类)