先将 mat 写入 MatOfByte
再将 MatOfByte 转换 byte[]
再将 byte[] 写入文件
public static boolean SaveMatToJpg(String imgPath,Mat dstImage) {
MatOfByte mob = new MatOfByte();
Imgcodecs.imencode(".jpg",dstImage,mob);
byte[] imageByte = mob.toArray();
return writeFileByBytes(imgPath,imageByte,false);
}
public static boolean writeFileByBytes(String fileName, byte[] bytes, boolean append) {
try(OutputStream out = new BufferedOutputStream(new FileOutputStream(fileName, append))){
out.write(bytes);
return true;
}catch (Exception e){
e.printStackTrace();
}
return false;
}