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[]?
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[]?
hi..
For scaling the ImageIcon "AffineTransform" from Graphics2D can be used.See java api..
cheers...
For scaling the ImageIcon "AffineTransform" from Graphics2D can be used.See java api..
cheers...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you can JAI to this too.
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.J
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