簡體   English   中英

相機圖像處理

[英]Camera image processing

基本上我有一個相機芯片(相機模塊:C3038,使用OmniVision的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,你需要使用SerialPort。 是一個簡單的例子,讓您開始從COM端口讀取。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM