2012-04-14 152 views
5

基本上我有一個通過RS232鏈接連接到PC的攝像頭芯片(攝像頭模塊:C3038,使用OmniVision的CMOS圖像傳感器OV6630)。我想讀在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(); 
      } 
     } 
    } 
} 
} 
+1

相機模塊:C3038,使用OmniVision的CMOS圖像傳感器OV6630。 – stud91 2012-04-14 07:51:03

+0

謝謝,編輯了這個信息。納入問題中,使其更加突出。不知道答案,但有趣的問題。 +1 – 2012-04-14 07:54:39

回答

0

的問題是一種廣泛的,所以我不知道你有多少經驗,但對於RS-232,你將需要使用的SerialPort。 Here是讓你開始從COM端口讀取的簡單示例。

3

不幸的是,Java不支持自己的串行端口 - 您需要一個外部庫。我建議看看the RXTX library,這些日子似乎有些事實上的標準。

視頻傳感器芯片通常自身具有相對簡單的通信接口(即沒有橋接芯片)。通常歸結爲設置圖像參數,啓動實際的圖像數據傳輸,然後將一些字節讀入緩衝區。有時候可能會涉及圖像數據的開始或結束簽名,但就是這樣。

它不應該是太如果你把所有的文檔,你湊錢在手 - 我偶爾會做用C類似的東西沒有任何文檔 ...

編輯:

將圖像讀取到字節數組後,您可能可以使用the BufferedImage class使其可用於Java。也就是說,我一直無法確定Java是否支持除ARGB以外的其他任何東西 - 如果您想使用非空格,您可能必須自行(或通過第三方庫)進行色彩空間轉換-RGB模式在您的傳感器。

+0

謝謝我知道如何使用RXTX庫從COM端口讀取數據,但我需要的是將數據從COM端口處理成Java中的圖像 – stud91 2012-04-14 08:00:48

+0

代碼已編輯並上傳..但是,有沒有辦法檢測圖像的結束以停止while循環while(inputStream.available()> 0) – stud91 2012-04-15 01:21:02

+0

@ stud91:好的,顯而易見的方法是計算字節數。原始圖像具有固定的字節大小,具體取決於分辨率和字節/像素。請注意以格式填充比特,例如12位/像素。一些傳感器也具有分隔符字節序列。您應該檢查您的傳感器文檔以瞭解其支持的輸出格式的詳細信息... – thkala 2012-04-15 09:46:10