BufferImage 转化成InputStream

场景:需要采集视频,拿到这个视频之后,一帧一帧的图片转成base64的数据,调用python写的算法处理,然后返回给java程序,之后处理下面的业务代码

通过V4L2,使用java可以拿到BufferImage对象,然后转换成InputStream

使用java拿到摄像头驱动采集的视频,加入v4l2依赖的jar包


    com.github.sarxos
    v4l4j
    0.9.1-r507

方法中的得到BufferImage,然后转化成InputStream

BufferImage转成InputStream
final BufferedImage input = videoFrame.getBufferedImage();
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
   ImageIO.write(input,"jpg",os);
} catch (IOException e) {
   e.printStackTrace();
}
InputStream is = new ByteArrayInputStream(os.toByteArray());

 

 

 

 

 

 

 

拿到InputStream,需求是调用Base64码的方法,然后调用python脚本

String streamImage = ImageToBase64(is)
private static String ImageToBase64(InputStream is) {
   byte[] data = null;
   try {
      data = new byte[is.available()];
      is.read(data);
      is.close();
   } catch (IOException e) {
      e.printStackTrace();
   }
   String encode = Base64.encode(data);
   return encode;
}

 

 

 

 

 

 

 

 

 

 

 

 

完事之后,调用python脚本

 

本地图片,先得到BufferImage,然后转成InputStream


//URL url = new URL("http://www.google.com/intl/en_ALL/images/logo.gif");

File file = new File("G:\\video\\img_01.jpg");

ImageIO.read(file);
BufferedImage image = ImageIO.read(url);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(image, "gif", os);
InputStream is = new ByteArrayInputStream(os.toByteArray());

 

你可能感兴趣的:(问题解决,V4L2,BufferImage,InputStream,java调用python)