Results 1 to 6 of 6
Thread: Camera image processing in Java
- 04-14-2012, 01:28 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 2
- Rep Power
- 0
Camera image processing in Java
Basically I have a camera chip (Camera module: C3038, uses OmniVision’s CMOS image sensor OV6630) connected to a PC through a RS232 link. I want to read image data in a Java program which is in this format (according to camera specification):
My implementation is as follows...However the program doesn't proceed to the GUI stage after the whole image is transferred to the PC from the camera chip...i.e. the while loop at line 58 doesn't break...
Java Code:public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier portId; static Enumeration portList; InputStream inputStream; SerialPort serialPort; Thread readThread; byte [] readBuffer; static byte [] storeBuffer; public SimpleRead() { 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[20]; ByteArrayOutputStream bias = new ByteArrayOutputStream(); String a = new String(); String b = new String(); try { while (inputStream.available() > 0) { b += readBuffer; int numBytes = inputStream.read(readBuffer); a += readBuffer; System.out.print(new String(readBuffer)); storeBuffer = new byte[numBytes]; inputStream.read(storeBuffer); if(b.equals(a)){// to indicate end of image break; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } InputStream in = new ByteArrayInputStream(storeBuffer); BufferedImage image = null; try { image = ImageIO.read(in); } catch (IOException e) { // TODO Auto-generated catch block 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")) { SimpleRead reader = new SimpleRead(); } } } }
- 04-14-2012, 02:24 PM #2
Re: Camera image processing in Java
You have two reads, on line 60 and 64. That's going to read twice, not read the same bytes twice.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-14-2012, 02:30 PM #3
Re: Camera image processing in Java
Also note that it's woefully inefficient to (1) use a String assignment to a newly constructed empty String -- just assign to the empty String literal
and (2) to use Strings at all when repeated concatenation is the order of the day. Use a StringBuilder / StringBuffer instead.Java Code:String a = ""; // new String();
It also looks like you want to process binary data, so adding the data to a String isn't going to help. And thisgets only the data of the last read (i.e. the data read by the second read the last time through the loop) which certainly isn't going to give you all the bytes of the image.Java Code:InputStream in = new ByteArrayInputStream(storeBuffer);
What format?I want to read image data in a Java program which is in this format (according to camera specification)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-14-2012, 02:39 PM #4
Re: Camera image processing in Java
Looks like this is where the original code came from:
http://java.sun.com/developer/releas...impleRead.java
You might do better to make less changes in the original code, except for adding e.printStackTrace() in the empty catch block (very bad of Sun/Oracle to publish empty catch blocks at all). And instead of constructing a String from the bytes, you would probably want to aggregate and return the byte array read.
AFAIK javax.comm is less maintained than RXTX (google it) but I could be wrong.
dbLast edited by DarrylBurke; 04-14-2012 at 04:32 PM.
Why do they call it rush hour when nothing moves? - Robin Williams
- 04-14-2012, 06:11 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 04-14-2012, 07:20 PM #6
Re: Camera image processing in Java
Cross posted
java - Camera image processing - Stack Overflow
Quoting from that thread:
Locking this waste of our time.
Originally Posted by stud91
db
THREAD CLOSEDWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Capturing image from stream of usb video camera
By a_p2011 in forum Advanced JavaReplies: 1Last Post: 03-28-2011, 12:07 AM -
Image Processing in Java
By ankurshanbhag in forum Advanced JavaReplies: 2Last Post: 01-12-2011, 02:26 PM -
Image Processing in Java
By ankurshanbhag in forum Java SoftwareReplies: 7Last Post: 12-29-2010, 02:13 PM -
How To Receive And Display Image From Network Camera
By sri_reddy523 in forum NetworkingReplies: 11Last Post: 03-03-2009, 09:22 AM -
Capturing a video camera and save a snapshot as image
By happyday in forum AWT / SwingReplies: 0Last Post: 11-20-2008, 11:03 AM


LinkBack URL
About LinkBacks


Bookmarks