Results 1 to 6 of 6
  1. #1
    stud91 is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default 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();
               }
            }
        }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default 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
    Java Code:
    String a = ""; // new String();
    and (2) to use Strings at all when repeated concatenation is the order of the day. Use a StringBuilder / StringBuffer instead.

    It also looks like you want to process binary data, so adding the data to a String isn't going to help. And this
    Java Code:
    InputStream in = new ByteArrayInputStream(storeBuffer);
    gets 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.

    I want to read image data in a Java program which is in this format (according to camera specification)
    What format?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default 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.

    db
    Last edited by DarrylBurke; 04-14-2012 at 04:32 PM.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,001
    Blog Entries
    7
    Rep Power
    20

    Default Re: Camera image processing in Java

    Quote Originally Posted by DarrylBurke View Post
    AFAIK javax.comm is less maintained than RXTX (google it) but I could be wrong.
    You're not wrong; on MS Windows platforms javax.comm is dead but RXTX is still supported (plus a few lesser known derivatives).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Camera image processing in Java

    Cross posted
    java - Camera image processing - Stack Overflow

    Quoting from that thread:
    Quote Originally Posted by stud91
    thanks I know how to read from a COM port using RXTX library but the what I need is to process that data from the COM port into an image in Java
    Locking this waste of our time.

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Capturing image from stream of usb video camera
    By a_p2011 in forum Advanced Java
    Replies: 1
    Last Post: 03-28-2011, 12:07 AM
  2. Image Processing in Java
    By ankurshanbhag in forum Advanced Java
    Replies: 2
    Last Post: 01-12-2011, 02:26 PM
  3. Image Processing in Java
    By ankurshanbhag in forum Java Software
    Replies: 7
    Last Post: 12-29-2010, 02:13 PM
  4. How To Receive And Display Image From Network Camera
    By sri_reddy523 in forum Networking
    Replies: 11
    Last Post: 03-03-2009, 09:22 AM
  5. Replies: 0
    Last Post: 11-20-2008, 11:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •