//输入流转mat
public static Mat inputStream2Mat(InputStream inputStream) throws IOException {
BufferedInputStream bis = new BufferedInputStream(inputStream);
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.flush();
os.close();
is.close();
Mat encoded = new Mat(1, os.size(), 0);
encoded.put(0, 0, os.toByteArray());
Mat decoded = Imgcodecs.imdecode(encoded, -1);
encoded.release();
}
//mat转输入流
public static InputStream mat2InputStream(Mat mat)
{
MatOfByte mob = new MatOfByte();
imencode(".jpg", mat, mob);
byte[] byteArray = mob.toArray();
return new ByteArrayInputStream(byteArray);
}
Mat和输入流之间的转换
最新推荐文章于 2022-08-22 18:25:56 发布