Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2005

Tip: Looking for answers? Try searching our database.

How to get an array of bytes from an Image object

Thread view: 
Bilbo Baggins - 17 Feb 2004 11:45 GMT
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.
Alex Hunsley - 17 Feb 2004 15:58 GMT
> Hi all.
>
[quoted text clipped - 12 lines]
>
> Thanks in advance for your support.

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

alex
nos - 17 Feb 2004 20:22 GMT
> > Hi all.
> >
[quoted text clipped - 16 lines]
>
> alex

Also, look into "affene (sp?) transform"
Chris Berg - 17 Feb 2004 22:42 GMT
>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
Bilbo Baggins - 18 Feb 2004 08:28 GMT
>Now you've got int's, not bytes. I assume that's what you want.

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.
Cyril Mrazek - 19 Feb 2004 08:59 GMT
>>Now you've got int's, not bytes. I assume that's what you want.
>
[quoted text clipped - 4 lines]
>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();
Cyril Mrazek - 19 Feb 2004 09:02 GMT
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();
Bilbo Baggins - 19 Feb 2004 12:13 GMT
>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();
Cyril Mrazek - 19 Feb 2004 12:42 GMT
Great! Next step is to make it generic for all supported graphics
types.
CM

>I solved the problem:
>
[quoted text clipped - 21 lines]
>encoder.encode(bi);
>byte[] byteArray = os.toByteArray();
Bilbo Baggins - 19 Feb 2004 12:47 GMT
>Great! Next step is to make it generic for all supported graphics
>types.

For BMP images take a look at:
http://sourceforge.net/projects/javaexplorer/
Renato Gondim - 25 Mar 2005 11:43 GMT
It's not working, just saves a black window in the database.
What's wrong?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.