This discussion is archived
1 Reply Latest reply: Jul 10, 2010 5:34 AM by 843802 RSS

How to show the Resized Tiff Image in JPanel?

843802 Newbie
Currently Being Moderated
I want to show the the resized TIFF image in JFrame. I can see the TIFF iamge in JFrame but I want to have
resized TIFF in my JFrame. Please anyone help in the following code I dont know whats going wrong their.
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import javax.media.jai.NullOpImage;
import javax.media.jai.OpImage;
import javax.media.jai.widget.ImageCanvas;
import javax.swing.JPanel;
import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.SeekableStream;
import com.sun.media.jai.codec.TIFFDecodeParam;

public class TiffReader extends Frame {

    public TiffReader(final String filename) throws IOException {

        setLayout( null );
        setTitle( " TIFF Reader" );

        final File file = new File( filename );
        final SeekableStream s = new FileSeekableStream( file );

        final TIFFDecodeParam param = null;

        final ImageDecoder dec = ImageCodec.createImageDecoder( "tiff", s, param );

        // Which of the multiple images in the TIFF file do we want to load
        // 0 refers to the first, 1 to the second and so on.
        final int imageToLoad = 0;

        final RenderedImage op =
            new NullOpImage( dec.decodeAsRenderedImage( imageToLoad ), null, OpImage.OP_IO_BOUND, null );

        final BufferedImage img = new BufferedImage( op.getWidth(), op.getHeight(), BufferedImage.TYPE_INT_ARGB );

        final JPanel addPanel = new JPanel();
        addPanel.setLayout( null );
        addPanel.setBounds( 100, 100, 800, 600 );
        add( addPanel );

        final ImageCanvas canvas = new ImageCanvas( img );
        canvas.setBounds( 0, 0, 800, 600 );

        final Graphics g = img.createGraphics();
        final int imageW = addPanel.getWidth();
        final int imageH = addPanel.getHeight();
        g.drawImage( img, 0, 0, imageW, imageH, null, null );

        addPanel.add( canvas );
        setSize( 1024, 768 );
        setUndecorated( true );
    }

    public static void main( final String[] args ) {

        final String filename = "C://pictures.tiff";

        try {
            final TiffReader window = new TiffReader( filename );
            // window.pack();
            window.show();

        } catch (final java.io.IOException ioe) {
            System.out.println( ioe );
        }
    }
}
  • 1. Re: How to show the Resized Tiff Image in JPanel?
    843802 Newbie
    Currently Being Moderated
    hi I got the solution to the above problem. But now I want to flush/remove the current image and add the new one
    to the JFrame Dynamically but I don't get it.
    This is the modified code that may be helpful to others
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.awt.image.ColorModel;
    import java.awt.image.RenderedImage;
    import java.awt.image.WritableRaster;
    import java.io.File;
    import java.io.IOException;
    import java.util.Hashtable;
    import javax.media.jai.NullOpImage;
    import javax.media.jai.OpImage;
    import javax.media.jai.widget.ImageCanvas;
    import javax.swing.JPanel;
    import com.sun.media.jai.codec.FileSeekableStream;
    import com.sun.media.jai.codec.ImageCodec;
    import com.sun.media.jai.codec.ImageDecoder;
    import com.sun.media.jai.codec.SeekableStream;
    import com.sun.media.jai.codec.TIFFDecodeParam;
    
    @SuppressWarnings("restriction")
    public class TiffReader extends Frame {
    
        private static final long serialVersionUID = 1L;
    
        public TiffReader(final String filename) throws IOException {
    
            setLayout( null );
            setTitle( " TIFF Reader" );
    
            final File file = new File( filename );
            final SeekableStream stream = new FileSeekableStream( file );
    
            final TIFFDecodeParam param = null;
    
            final ImageDecoder dec = ImageCodec.createImageDecoder( "tiff", stream, param );
    
            // Which of the multiple images in the TIFF file do we want to load
            // 0 refers to the first, 1 to the second and so on.
            final int imageToLoad = 0;
    
            final RenderedImage renderedTiffChqImg =
                new NullOpImage( dec.decodeAsRenderedImage( imageToLoad ), null, OpImage.OP_IO_BOUND, null );
    
            BufferedImage chqBufferedImg =
                new BufferedImage( renderedTiffChqImg.getWidth(), renderedTiffChqImg.getHeight(),
                    BufferedImage.TYPE_BYTE_GRAY );
            chqBufferedImg = convertRenderedImgToBuffImg( renderedTiffChqImg );
            final JPanel addPanel = new JPanel();
            addPanel.setLayout( null );
            addPanel.setBounds( 100, 100, 800, 400 );
            add( addPanel );
    
            final ImageCanvas chqImgCanvas = new ImageCanvas( chqBufferedImg );
            chqImgCanvas.setBounds( 0, 0, 800, 600 );
    
            final Graphics g = chqBufferedImg.createGraphics();
            final int imageW = addPanel.getWidth();
            final int imageH = addPanel.getHeight();
            g.drawImage( chqBufferedImg, 0, 0, imageW, imageH, null, null );
    
            addPanel.add( chqImgCanvas );
            setSize( 1024, 768 );
            setUndecorated( true );
        }
    
        public BufferedImage convertRenderedImgToBuffImg( final RenderedImage chqBufferedImg ) {
    
            if (chqBufferedImg instanceof BufferedImage) {
                return (BufferedImage) chqBufferedImg;
            }
            final ColorModel cm = chqBufferedImg.getColorModel();
            final int width = chqBufferedImg.getWidth();
            final int height = chqBufferedImg.getHeight();
            final WritableRaster raster = cm.createCompatibleWritableRaster( width, height );
            final boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
            final Hashtable<String, Object> properties = new Hashtable<String, Object>();
            final String[] keys = chqBufferedImg.getPropertyNames();
            if (keys != null) {
                for (int i = 0; i < keys.length; i++) {
                    properties.put( keys, chqBufferedImg.getProperty( keys[i] ) );
    }
    }
    final BufferedImage result = new BufferedImage( cm, raster, isAlphaPremultiplied, properties );
    chqBufferedImg.copyData( raster );
    return result;
    }

    public static void main( final String[] args ) {

    final String filename = "C://pictures.tiff";

    try {
    final TiffReader window = new TiffReader( filename );
    // window.pack();
    window.show();

    } catch (final java.io.IOException ioe) {
    System.out.println( ioe );
    }
    }
    }
    In above code I am trying to reload the new Image generated by the other class this is what i am doing but it wont work.
    chqBufferedImg.flush(); // BufferedImage Object
    result.flush(); // BufferedImage Object
    stream.reset(); // SeekableStream Object
    but I think this is not sufficient to flush out all the resources held by the different objects.