OpenCV/Java byte[] 转 Mat

    /**
     * Mat转换成byte数组
     *
     * @param matrix        要转换的Mat
     * @param fileExtension 格式为 ".jpg", ".png", etc
     * @return
     */
    public static byte[] mat2Byte(Mat matrix, String fileExtension) {
        MatOfByte mob = new MatOfByte();
        Imgcodecs.imencode(fileExtension, matrix, mob);
        byte[] byteArray = mob.toArray();
        return byteArray;
    }
byte[] byteString = mat2Byte(mat, ".jpg");
Mat mat = Imgcodecs.imdecode(new MatOfByte(byteString), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
Imgcodecs.imwrite(failName, mat);

 

你可能感兴趣的:(OpenCV)