Java 如何将字节数组转换为位图并通过web服务器传输图像?
我正在做的项目是在联合国办事处和联合国秘书处的帮助下,从一个安全摄像机中拍摄一帧。然后我通过串行端口以字节数组的形式发送帧。我想问,我如何使用Java将这个字节数组转换回一个图像,然后通过web服务器流式传输这个图像,或者甚至将这个图像变成视频,然后再流式传输 我使用的代码是:Java 如何将字节数组转换为位图并通过web服务器传输图像?,java,Java,我正在做的项目是在联合国办事处和联合国秘书处的帮助下,从一个安全摄像机中拍摄一帧。然后我通过串行端口以字节数组的形式发送帧。我想问,我如何使用Java将这个字节数组转换回一个图像,然后通过web服务器流式传输这个图像,或者甚至将这个图像变成视频,然后再流式传输 我使用的代码是: //Handle an event on the serial port. Read the data and save the image. public synchronized void serialEvent(
//Handle an event on the serial port. Read the data and save the image.
public synchronized void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
System.out.println("Got it!");
int available = input.available();
byte[] chunk = new byte[available];
input.read(chunk, 0, available);
InputStream in = new ByteArrayInputStream(chunk);
BufferedImage image = ImageIO.read(in);
ImageIO.write(image, "BMP", new File ("/home/zuss/images/image.BMP"));
} catch (Exception e) {
System.err.println(e.toString());
}
}
}
返回到我的终端窗口:java.lang.IllegalArgumentException:image==null代码>
只要arduino向串行端口发送数据,就可以连续发送。您的代码应该如下所示:
InputStream in = new ByteArrayInputStream(chunk);
OutputStream out = new FileOutputStream(new File ("/home/zuss/images/image.BMP"));
byte[] buffer = new byte[4096];
int read = in.read(buffer);
while(read >= 0 ) {
out.write(buffer, 0, read);
read = in.read(buffer);
}
视频实验者盾这是什么?理论上这应该是可能的是的,只要你知道使用了什么编码。只要将所有原始字节都提取出来,就可以在Java层对其进行解码。首先,祝你们好运!这需要stacktrace。也许您没有一次获取所有字节?当您输入时,读取(块,0,可用);你可能还没有得到所有的图像。在接收到所有字节之前,您可能需要在循环中读取流。只有将其转换为图像,您才拥有字节数组。流式传输。您无需将其转换为图像即可发送。您需要将其转换为图像以显示它。而且您误用了available()
,其Javadoc中特别警告您不要这样做。