Copying BufferedImage

E

Eel

Should the following work? Is there any trick to using getRGB /
setRGB in BufferedImage? I'm just trying to copy one BufferedImage to
another. The ultimate goal is to resize, but I just want to copy for
now.

BufferedInputStream in = new BufferedInputStream(new
FileInputStream(input));

JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);

BufferedImage bi = decoder.decodeAsBufferedImage();

int imageWidth = bi.getWidth();
int imageHeight = bi.getHeight();

BufferedOutputStream out = new BufferedOutputStream(new
FileOutputStream(output));

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

BufferedImage bi1 =
new BufferedImage(imageWidth, imageHeight, bi.getType());
for (int y = 0; y < imageHeight; y++)
{
for (int x = 0; x < imageWidth; x++)
{
int rgb = bi.getRGB(x, y);
bi1.setRGB(x, y, rgb);
}
}
encoder.encode(bi1);
 
M

Marco Schmidt

Eel:
Should the following work?

Why don't you try? The setRGB/getRGB combination loses some precision
if the BufferedImage in question contains more than eight bits per
component, or uses some color model that cannot be perfectly mapped
from or to RGB.

[...]

Regards,
Marco
 
E

Eel

Thanks for responding. Actually the code I posted worked. The reason
I thought it didn't was that I was using ascii ftp to get test images
to view. Sorry for the silly error.

Thanks.

Marco Schmidt said:
Eel:
Should the following work?

Why don't you try? The setRGB/getRGB combination loses some precision
if the BufferedImage in question contains more than eight bits per
component, or uses some color model that cannot be perfectly mapped
from or to RGB.

[...]

Regards,
Marco
 

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,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top