Showing posts with label images. Show all posts
Showing posts with label images. Show all posts

Monday, August 4, 2008

Java: component to display images from a byte array

In some situation there might be the need of displaying images received through a byte array (e.g. the image is sent using a socket). The solution is pretty simple. With the following code you can easily create a Component which can display images (e.g. JPEG, GIF) from a byte array. Then you just need to add this component to your GUI.
class ImageComponent extends JPanel
{
    public ImageComponent() {}
        
    public ImageComponent (byte[] image)
    {
        ByteArrayInputStream bis = new ByteArrayInputStream (image);
        try {
            _image = ImageIO.read (bis);
        }
        catch (IOException ex){}
    }
        
    //This method replaces the image displayed by the component
    //with a new one passed in the image byte array.
    public void updateImage (byte[] image)
    {
        ByteArrayInputStream bis = new ByteArrayInputStream (image);
        try {
            _image = ImageIO.read (bis);
        }
        catch (IOException ex){}
        repaint();
    }
        
    public void paint(Graphics g)
    {
        g.drawImage(_image, 0, 0, null);
    }
        
    public Dimension getPreferredSize()
    {
        if (_image == null) {
            return new Dimension (320, 240);
        } else {
            return new Dimension (_image.getWidth (null), _image.getHeight (null));
        }
    }
        
    BufferedImage _image;
}

Wednesday, July 30, 2008

Tutorial: grab images from a network camera using Java

Requirements: a network camera with a web interface. The web interface should make available somehow a dynamically generated JPEG image. For the implementation we will use the Apache commons, available here. In particular we need the http-commons-client, the commons-logging and the commons-codec. The http-commons-client is what we really need, the other two packages are dependencies for the commons-client.

To run the program you need to have the JAR files included in the tar.gz archives in the Java classpath.

First of all we need to know the URL to get the image from the camera. In my case it was something like http://xxx.yyy.www.zzz/cgi-bin/video.jpg?size=3. Now we are ready to write the code.
//NetworkCamera class

import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

public class NetworkCamera
{
    public NetworkCamera (String url)
    {
        log ("Registered network camera. Image url = " + url);
        _method = new GetMethod(url);
        //Create the HTTP client and set the parameters
        _client = new HttpClient();
        _client.getParams().setParameter ("http.useragent", "CustomizedJavaClient");
        _client.getParams().setParameter ("http.connection.timeout", new Integer(5000));
        _method.setFollowRedirects (true);
    }
    
    
    public byte[] getImage ()
    {
        log ("Getting image...");
        byte[] image = null;
        try {
            int retCode = _client.executeMethod (_method);
            if ( retCode != -1){
                log ("getMethod succeed [HTTP-RESPONSE-CODE" + retCode + "]. Content lenght = " + _method.getResponseContentLength());
                //The HTTP response body contain the bytes of the image
                image = _method.getResponseBody();
                System.out.println ("Bytes read: " + image.length);
            }
        }
        catch (IOException ex) {
            //Report error
            System.out.println (ex.getMessage ());
        }  
        return image;
    }
    
    private void log (String message)
    {
        if (DEBUG) {
            System.out.println (message);
        }
    }
    

    private GetMethod _method;
    private HttpClient _client;
    
    private static final boolean DEBUG = true;
}

//Main class (for test purpose)

import java.io.FileOutputStream;

public class Main
{
    public static void main (String[] args) throws Exception
    {
        Camera cam = new NetworkCamera (CAMERA_IMAGE_URL);
        byte[] image = cam.getImage();
        
        FileOutputStream fos = new FileOutputStream (IMAGE_FILE_NAME);
        fos.write(image);
        fos.close();
    }
    
    private static final String CAMERA_IMAGE_URL = "http://xxx.yyy.www.zzz/cgi-bin/video.jpg?size=3";
    private static final String IMAGE_FILE_NAME = "test.jpg";
}

The code gets a frame from the camera as a JPEG file and saves it as test.jpg.
Of course you may use exactly the same code to grab an image from a web site. You can try, for instance, to assign
private static final String CAMERA_IMAGE_URL = "http://www.google.com/intl/en_ALL/images/logo.gif"
and see what happens.

Tuesday, July 22, 2008

Extract EXIF Metadata from JPEG files using Java

I was looking into this problem for a project I am working on, and I found this cool library.


Here some usage examples.