How to get an array of bytes from an Image object

B

Bilbo Baggins

Hi all.

I've an array of bytes.

ImageIcon imageIcon = new ImageIcon(byteArray);

I've to do an image resize (i.e. 100x100)

Image image = imageIcon.getImage();
Image newImage = image.getScaledInstance(100, 100,
Image.SCALE_DEFAULT);

Now I need to get an array of bytes about the new image.
How can I do it?

Thanks in advance for your support.
 
A

Alex Hunsley

Bilbo said:
Hi all.

I've an array of bytes.

ImageIcon imageIcon = new ImageIcon(byteArray);

I've to do an image resize (i.e. 100x100)

Image image = imageIcon.getImage();
Image newImage = image.getScaledInstance(100, 100,
Image.SCALE_DEFAULT);

Now I need to get an array of bytes about the new image.
How can I do it?

Thanks in advance for your support.

Sun's Java Advanced Imaging (JAI) may be of interest....

alex
 
C

Chris Berg

Now I need to get an array of bytes about the new image.
How can I do it?

Thanks in advance for your support.

I assume you want to get an array of pixels:

First, make sure the new Image is fully loaded:

MediaTracker mt = new MediaTracker(myComponent);
mt.addImage(image, 0);
mt.waitForID(0, 5000);
// must have time-out, 'cause animated gif's may loop forever
int r = mt.statusID(0, false);
if ( (r&MediaTracker.COMPLETE)!=0) {
// IMAGE ERROR
}

Then, grab the pixels: (w,h is new Image dimension)

int[]pixels = new int[w*h];
PixelGrabber pxg = new PixelGrabber(image, 0, 0, w, h, pixels,0,w);
pxg.grabPixels();

- plus some try-catch surroundings.

Now you've got int's, not bytes. I assume that's what you want.

But if you want to write a .jpg file, it's a completely different
story; in that case you shall use Java2 classes. Look for:

java.awt.image.BufferedImage
com.sun.image.codec.jpeg.JPEGImageEncoder


Chris
 
C

Cyril Mrazek

The problem is that I need bytes, not int's, because I have to store
them into db.
I tried to convert int's into bytes using this example:
http://forum.java.sun.com/thread.jsp?forum=31&thread=353331
but it didn't work.
The final array of bytes is not an image, unfortunately.

Hi,
I have exactely the same problem. I've found a workaround, I use a
file as an intermediate stage, it works fine but it is a heresy in
efficiency terms.

Cyril Mrazek


Connection con;
BufferedImage bimg;

//... get con
//... get bimg

File fiDummy = new File("dummy.jpg");
FileImageOutputStream fios = new FileImageOutputStream(fiDummy);
ImageWriter iw = ImageIO.getImageWriterBySuffix("jpg");
iw.setOutput(fios);
iw.write(bimg);
fios.close();

// the column "image" int the table "album" is a Blob
PreparedStatement pstm = con.prepareStatement("UPDATE album SET
image=? WHERE name=?");
FileInputStream fis = new FileInputStream(fiDummy);
pstm.setBinaryStream(1, fis, (int) fiDummy.length());
pstm.setString(2, _name);
pstm.executeUpdate();
fis.close();
rs.close();
 
C

Cyril Mrazek

without the rs, sorry, I copied one line too much.
CM


Connection con;
BufferedImage bimg;

//... get con
//... get bimg

File fiDummy = new File("dummy.jpg");
FileImageOutputStream fios = new FileImageOutputStream(fiDummy);
ImageWriter iw = ImageIO.getImageWriterBySuffix("jpg");
iw.setOutput(fios);
iw.write(bimg);
fios.close();

// the column "image" int the table "album" is a Blob
PreparedStatement pstm = con.prepareStatement("UPDATE album SET
image=? WHERE name=?");
FileInputStream fis = new FileInputStream(fiDummy);
pstm.setBinaryStream(1, fis, (int) fiDummy.length());
pstm.setString(2, _name);
pstm.executeUpdate();
fis.close();
 
B

Bilbo Baggins

Cyril Mrazek said:
Hi,
I have exactely the same problem. I've found a workaround, I use a
file as an intermediate stage, it works fine but it is a heresy in
efficiency terms.

I solved the problem:

// iconData is the original array of bytes
ImageIcon imageIcon = new ImageIcon(iconData);
Image img = imageIcon.getImage();

Image imageResize = img.getScaledInstance(100, 100, 0);

ImageIcon imageIconResize = new ImageIcon (imageResize);

int resizeWidth = imageIconResize.getIconWidth();
int resizeHeight = imageIconResize.getIconHeight();

Panel p = new Panel();
BufferedImage bi = new BufferedImage(resizeWidth, resizeHeight,
BufferedImage.TYPE_INT_RGB);

Graphics2D big = bi.createGraphics();
big.drawImage(imageResize, 0, 0, p);

ByteArrayOutputStream os = new ByteArrayOutputStream();

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
encoder.encode(bi);
byte[] byteArray = os.toByteArray();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top