Read image data from COM7 port in Java

2020-08-09 04:40发布

问题:

I am doing a project in which I have to display the image captured from a camera chip on to the PC.

The image data is being sent through the COM7 port to the computer. (I have checked it using a dumb terminal program). Basically what I want to do is to develop a Java program that will read the COM port, get the image data, process it and display it on the screen.

What I want to know is how to read this image data from COM7 port in my Java program.

回答1:

For the serial port portion of the problem, read the serial port like any other file.

  • On 'nix, serial ports are named /dev/ttySn where n 0-3 for COM1 to COM4. I'm guessing that if more serial ports exist, appropriately named files will exist on your system to access these too.
  • on Windows you can just use the special file names COM1, COM2, etc.

As for actually reading the image data, as mentioned by others, you will need to know what format it is in. You can try using ImageIO.read() if it is in one of Java's known file formats. If it is in a raw bitmap format, you could try constructing a buffered image from the raw data by creating a BufferedImage from a Raster, and a Raster from an appropriate SampleModel and DataBuffer. You will need to know the format of the bitmap data to do this, however.



回答2:

You probably want to start with reading the data from the port. For this, a Socket could be useful.

Since you 'think' the data will be bytes... try the following:

BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytes));

You could also check out a related post



标签: java port