Powered By Blogger
Showing posts with label image. Show all posts
Showing posts with label image. Show all posts

Saturday, December 22, 2012

Flex: Displayable Image from an Image Resource

  Following is a class which can be used to convert and an image resource (here a png file) to a displayable image (mx.controls.Image) instance in Flex. Here a static function 'displayableImageFromClass(Image, int, int): void ' does the work. The function's height and width parameters are optional and they specify the height and width of the displayable image that is returned.

================================================================

package com.shyarmal.drawing.utils {
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.utils.ByteArray;
   
    import mx.controls.Image;
    import mx.graphics.codec.PNGEncoder;
   

    public class ImageUtils {
       
        /**
         * png image encoder.
         */
        private static var encoder:PNGEncoder = new PNGEncoder();
       
        public function ImageUtils() {
        }
       
        /**
         * Given a class (with an embeded png image), 
         * creates a corresponding displayable image,
         * if imageClass is not null.
         *
         * @param imageClass - The image class from 
         *            which the image has to be created.
         * @param height - Height of the final image (optional).
         * @param width - Width of the final image (optional).
         * @return - mx.controls.Image produced.
         */
        public static function displayableImageFromClass( 
                   imageClass: Class, height: int = 25, 
                                   width: int = 25): Image {
            var i:Image = new Image();
            if (imageClass != null) {
                var bitMap: Bitmap = new imageClass();
                var bitmapData: BitmapData = bitMap.bitmapData;
                bitmapData.draw(bitMap);
                var imageToDisplay:ByteArray = 
                         encoder.encode(bitmapData);
                i.source = imageToDisplay;
                i.height = height;
                i.width = width;
            }
            return i;
        }
    }
}

================================================================

  If the 'imageClass' parameter is null, simply a new instance of mx.controls.Image is formed and returned. If not a 'Bitmap' is formed by instantiating the 'imageClass' passed in. Then its 'BitmapData' is derived. A byte array is constructed with the help of PNGEncoder and assigned to the source of the image instance. Image is returned after setting its dimensions.

Invocation of the method can be done as below;

[Embed (source="/assets/images/your_png.png" )] 
var resource: Class; // define a class with the actual resource.

// height and width can be passed if necessary.
var i:Image = ImageUtils.displayableImageFromClas(resource);



thanks,
Shyarmal.

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.