3

我正在从事的项目是在Arduino UNO 和Video Experimenter shield的帮助下从安全摄像头捕获帧。然后我通过串行端口将帧作为字节数组发送。我想问一下,我如何使用 Java 将这个字节数组转换回一个图像,然后通过 Web 服务器流式传输这个图像 - 或者甚至将这个图像制作成视频然后流式传输它?

我堆积的代码是这样的:

//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 正在将数据发送到串行端口,就会持续。

4

1 回答 1

0

您的代码应如下所示:

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);
}
于 2021-01-18T11:41:30.910 回答