Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Convert int[] to BufferedImage

800306Aug 1 2007 — edited Aug 2 2007
Hi all,

This is related to my other thread found here, although the Exception from that other thread is irrelevant now: http://forum.java.sun.com/thread.jspa?threadID=5200579.

Basically, I have an int array, and I want to convert it to a BufferedImage so I can save it to disk, and other fun stuff like that. I have a small idea of how to do it based on some code from DrLaszloJamf from awhile back.

I've been beating my head against the wall for about three days. Everywhere I search for information always comes up with crap like "convert bufferedimage to byte[]!!!!!". The API docs have about ten different classes for this, none of which seem to work, and all of which lead to two-three more classes.

The closest I've gotten is using createBandedRaster to create a WritableRaster. This unfortunately displays a completely black image, which is not what I want.

I am trying to take the int[], create a grayscale image out of it, add RGB values, and save it to disk.

I am absolutely stuck. I've tried using createPackedRaster; this gave me some crazy exceptions. I've tried using createInterleavedRaster; this is not compatible with DataBuffer.TYPE_INT. The closest I've gotten is outputting a completely black image with createBandedRaster.

Here's what I have at the moment:
// w = width, h = height, yes I need better variable names
private static BufferedImage toImage(byte[] data, int w, int h, int bitsPerPixel) {
        DataBuffer buffer;
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        ColorModel cm;
        WritableRaster raster;
        // convertBytesToInts works fine. I have verified this multiple times. 
	buffer = new DataBufferInt(convertBytesToInts(data), data.length/2);
       	cm = new ComponentColorModel(cs, false, true, Transparency.OPAQUE, DataBuffer.TYPE_INT);
	raster = Raster.createBandedRaster(buffer, w, h, w, new int[] {0}, new int[] {0}, null);
        return new BufferedImage(cm, raster, false, null);
}

public static void createAndSaveImage(byte[] data, int width, int height, int bitsPerPixel) {
        File file = new File("/u/dgresh/pic.jpg");
	BufferedImage im = toImage(data, width, height, bitsPerPixel);
        BufferedImage out = createRGBImage(im);
        try {
                ImageIO.write(out, "jpeg", file);
        } catch (IOException e) {
                e.printStackTrace();
        }
        System.out.println("Image saved.");
}

private static BufferedImage createRGBImage(BufferedImage in) {
        BufferedImage output = new BufferedImage(in.getWidth(), in.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g = output.createGraphics();
        g.drawRenderedImage(in, null);
        g.dispose();
        return output;
}
Any help would be VERY, VERY much appreciated.

Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 30 2007
Added on Aug 1 2007
26 comments
4,008 views