基本上我有一个相机芯片(相机模块:C3038,使用豪威的CMOS图像传感器OV6630)通过RS232链路连接到PC。 我想在这种格式的 Java 程序中读取图像数据(根据相机规范):
数据格式 - YCrCb 4:2:2、GRB 4:2:2、RGB 原始数据
关于如何做到这一点的任何提示?
我的实现:
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.imageio.*;
public class SimpleRead1 implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
byte [] readBuffer;
static byte [] storeBuffer;
public SimpleRead1() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
}catch (PortInUseException e) {System.out.println(e);}
try {
inputStream = serialPort.getInputStream();
}catch (IOException e) {System.out.println(e);}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {System.out.println(e);}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {System.out.println(e);}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {System.out.println(e);}
}
@Override
public void serialEvent(SerialPortEvent event){
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
readBuffer = new byte[Integer.MAX_VALUE];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
System.out.print(new String(readBuffer));
}
} catch (IOException e) {e.printStackTrace();}
InputStream in = new ByteArrayInputStream(readBuffer);
BufferedImage image = null;
try {
image = ImageIO.read(in);
} catch (IOException e) {e.printStackTrace();}
//GUI for displaying image
ImageIcon imageIcon = new ImageIcon(image);
JLabel label = new JLabel();
label.setIcon(imageIcon);
JFrame frame = new JFrame("image display");
frame.getContentPane().add(label,BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
break;
}
}
public static void main(String[] args) throws Exception {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM7")) {
// if (portId.getName().equals("/dev/term/a")) {
SimpleRead1 reader = new SimpleRead1();
}
}
}
}
}
Java本身不支持串行端口 - 你需要一个外部库。我建议看看 RXTX 库,它现在似乎有点事实上的标准。
视频传感器芯片通常具有相对简单的通信接口(即没有桥接芯片)。通常,它归结为设置图像参数,启动实际的图像数据传输,然后将多个字节读取到缓冲区中。有时可能会涉及图像数据的开始或结束签名,但仅此而已。
如果您手头有芯片的所有文档,这应该不会太难 - 我偶尔会在没有任何文档的情况下用 C 做类似的事情......
编辑:
将图像读入字节数组后,可以使用 BufferedImage 类使其可用于 Java。也就是说,我无法确定Java是否支持ARGB变体以外的任何内容 - 如果您想在传感器中使用非RGB模式,则可能必须自己(或通过第三方库,我想)进行颜色空间转换。
这个问题有点宽泛,所以我不知道你有多少经验,但对于RS-232,你将需要使用串行端口。这是一个简单的示例,可帮助您开始从COM端口读取。