The project I am working on is capturing a frame from a security camera, with the help of Arduino UNO and Video Experimenter shield . Then I am sending the frame as byte arrays through a serial port. I would like to ask, how could I, with Java, convert back this byte arrays to an image, and stream this image - or even make this images a video and then stream it - through a web server?
The code I have stacked with is this:
//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());
}
}
}
That returns to my terminal window: java.lang.IllegalArgumentException: image == null!
continuously as long as arduino is sending data to the serial port.
Your code should look something like this:
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.