convert BufferedImage to a byte array

C

cyberco

I want to get the bytes from a BufferedImage to send it over the line,
how can I achieve that? I seems to be trickier than I thought.
 
T

Timo Stamm

cyberco said:
I want to get the bytes from a BufferedImage to send it over the line,
how can I achieve that? I seems to be trickier than I thought.


See ImageIO
 
R

Roedy Green

I want to get the bytes from a BufferedImage to send it over the line,
how can I achieve that? I seems to be trickier than I thought.

you can't serialise it. The actual form is platform dependent, so it
might not mean anything when you got to the other end. What you want
to do is extract an array of ints or shorts or bytes and send that by
serialisation or DataOutputStream.
 
R

Roedy Green

you can't serialise it. The actual form is platform dependent, so it
might not mean anything when you got to the other end. What you want
to do is extract an array of ints or shorts or bytes and send that by
serialisation or DataOutputStream.

you can convert it to png or jpg format and send that.
 
C

cyberco

Yes, getting the bytes is what I'm trying to do. Unfortunately ImageIO
does not have any convenience methods for that purpose. Any other
suggestions?
 
T

Timo Stamm

cyberco said:
Yes, getting the bytes is what I'm trying to do.

Roedy pointed out why BufferedImage itself isn't serializable.

You could access the matrices and send that data over the line, but you
will most likely need some meta data for different color models etc. and
will reinvent the wheel, because there are already dozens of image file
formats available through ImageIO.

Unfortunately ImageIO does not have any convenience methods for that
purpose.


What could be more convenient than:

ImageIO.write(RenderedImage im, String formatName, OutputStream output);

?


Timo
 
C

cyberco

Oops! Sorry, Timo, you're right. My bad. I overlook ImageIO.write(). I
succeeded getting the byte[] in the following way.

####################
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufImage, formatName, baos);
byte[] bytesOut = baos.toByteArray();
 
E

E. Naubauer

cyberco said:
I want to get the bytes from a BufferedImage to send it over the line,
how can I achieve that? I seems to be trickier than I thought.

If you know the type of the data buffer of the BufferedImage its easy:

int[] t =
((DataBufferInt)(myBufferedImage).getRaster().getDataBuffer()).getData();

with DataBufferByte it works the same.
 
Joined
Jun 27, 2008
Messages
1
Reaction score
0
Similar Issue

When I create my cropped image it is showing as a black square. The width and the height are correct but it is not "cropping" from the original image. Any help would be appreciated thanks!

String sp = "C:\\testin.jpg";
Image image = new ImageIcon(sp).getImage();
//image = (Image) request.getSession().getAttribute("fixed_image");

BufferedImage resizedImage = new BufferedImage(200, 200,BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = resizedImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics2D.drawImage(image, 0,0, 80, 80, null, null);

BufferedImage croppedImage = resizedImage.getSubimage(1, 90, 90, 90);
File tempout = new File("C:\\testout.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
//FileOutputStream outImg = new FileOutputStream(tempimg);
ImageIO.write(croppedImage, "jpg", baos);
byte[] b = baos.toByteArray();

FileOutputStream outImg = new FileOutputStream(tempout);
outImg.write(b);
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top