Link to home
Start Free TrialLog in
Avatar of yongsing
yongsing

asked on

Load image, scale it, & convert to byte[]

I use the following code to read an image from the file system and display it on the GUI:

File file = new File("image.gif");
FileInputStream in = new FileInputStream(file);
BufferedInputStream reader = new BufferedInputStream(in);
int length = reader.available();
image = new byte[length];
reader.read(image, 0, length);
reader.close();
JLabel imageLabel = new JLabel();
imageLabel.setIcon(new ImageIcon(image));

I need to allow the user to scale the image freely, and then convert the scaled image into a byte array so that it can be saved into a database.

How do I do the scaling, and how do I convert the scaled image into a byte[]?

Avatar of daitt
daitt
Flag of Viet Nam image

java.awt.Graphics allow you to stretch the Image object with DrawImage method.
You can create a BufferedImage, then create a Graphics object to draw on that image, and use DrawImage to stretch the image on your BufferedImage.
Then you use the encoder to encode your bufferedimage to a file. I suppose it's a Jpeg file, then you can use com.sun.image.codec.jpeg.JPEGCodec.
http://java.sun.com/products/jdk/1.2/docs/guide/2d/api-jpeg/com/sun/image/codec/jpeg/JPEGCodec.html

Ask me if you have problem with any step.
Good luck
daitt
Avatar of SreedharSanni
SreedharSanni

hi..

 For scaling the ImageIcon "AffineTransform" from Graphics2D can be used.See java api..

cheers...




ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you can JAI to this too.