Sunday, December 11, 2011

Read and display image - Java Swing


package com.shyarmal.image.display;

import java.awt.Color;
import java.awt.Image;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.border.LineBorder;

/**
 *
 * @author  shyarmal
 * An external image file is read, re-sized and rendered in a
 * JLabel.
 */
public class ImageForm extends extends javax.swing.JFrame {

    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;


/**
This is the default constructor of the JFrame. This can not be used to render an image. Basically the initialization of components are done here. 
*/
    public ImageForm() {
        initComponents();
        this.setLocation(100, 150);
        jLabel1.setBorder(new LineBorder(Color.DARK_GRAY, 5));
        this.setResizable(false);
    }


/**
This is the constructor to be used to set an image. Path of the image file is passed as an argument, which is used to get an image icon from the method, createImageIcon(path) below. The image icon is set to the JLabel. Further the tile of the JFrame is set here. The default constructor is called first to initialize the components of the JFrame.
*/
    public ImageForm(String path) {
        this();
        this.setTitle("Image: " + path.substring(
                           path.lastIndexOf("\\") + 1));
        jLabel1.setIcon(createImageIcon(path));
//        jLabel1.setIcon(new ImageIcon(readFile(path)));
        getContentPane().add(jLabel1);
    }
/**
CREATING AND RESCALING OF AN IMAGE
An image is created from the byte data obtained by reading the image file (refer to the method, readImageFile(String path)). The image created is rescaled to the dimensions of the label in which it is to be rendered. An image icon is created from the rescaled image and returned.
*/
    private ImageIcon createImageIcon(String path) {
        Image image = new ImageIcon(
                readImageFile(path)).getImage();
        Image rescaledImage = image.getScaledInstance(
               jLabel1.getWidth(), jLabel1.getHeight(),
                      Image.SCALE_DEFAULT);
        return new ImageIcon(rescaledImage);
    }


/**
READING IMAGE DATA FROM (EXTERNAL) FILE
File path is passed into this method and a file input stream is created using the same. Length of byte data available in the input stream is obtained [fis.available()]. A byte array of the length, determined by this means is subsequently created. All the data is read into the byte array from the input stream [while ((i = fis.read(data)) != -1);] and returned.
*/
    private byte[] readImageFile(String path) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(path);
            int length = fis.available();
            byte[] data = new byte[length];
            int i = 0;
            while ((i = fis.read(data)) != -1);
            return data;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }               
   
    //  auto generated codes for the JFrame are not shown.              
}




thanks,
Shyarmal.

No comments:

Post a Comment