File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes byte or int array into BufferedImage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "byte or int array into BufferedImage" Watch "byte or int array into BufferedImage" New topic

byte or int array into BufferedImage

Bruno Danis

Joined: Jun 20, 2008
Posts: 5
can you help me please with converting byte array into BufferedImage
in case of BMP and TYP_INT_RGB of an image?

thank you
Rob Spoor

Joined: Oct 27, 2005
Posts: 19543

What have you tried so far? At the ranch we do not provide complete answers like this - we are NotACodeMill.

How To Ask Questions How To Answer Questions
Bruno Danis

Joined: Jun 20, 2008
Posts: 5
something like that, but it's wrong:SampleModel sm = new SinglePixelPackedSampleModel(DataBuffer.TYPE_BYTE,
w, h,
new int[] {0xFF});

DataBuffer db = new DataBufferByte( bdata, w*h, 0);
Point p = new Point(0,0);
WritableRaster raster = Raster.createWritableRaster(sm, db, p);
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);

int[] nBits = {24};

ColorModel cm = new ComponentColorModel(cs, nBits, false, true,

bufi =
new BufferedImage(cm, raster, false, null);

second version:

public BufferedImage getImagen(byte[] bdata)
final int colors = 32;
final int width = 200;
final int height = 200;
Image img;

// Create the color map
byte[] rbmap = new byte[colors];
byte[] gmap = new byte[colors];
for (int i = 0; i < colors; i++)
gmap[i] = (byte)((i * 255) / (colors - 1));
// Create the color model
int bits = (int)Math.ceil(Math.log(colors) /
IndexColorModel model = new IndexColorModel(bits,
rbmap, gmap, rbmap);
// Create the pixels
int pixels[] = new int[width * height];
int index = 0;
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
pixels[index++] = (x * colors) / width;
MemoryImageSource source = new MemoryImageSource(width, height,
model, pixels, 0, width);
// Create the image
Image = ? // i need some createImage
[ June 22, 2008: Message edited by: Bruno Danis ]
Rob Spoor

Joined: Oct 27, 2005
Posts: 19543

Where did you get the byte[] from? From a File / FileInputStream?

If so, check javax.imageio.ImageIO. It has methods to read a BufferedImage directly from a File / FileInputStream.
I agree. Here's the link:
subject: byte or int array into BufferedImage
Similar Threads
problem with
Graphics/Sprites Protection
BufferedImage from byte[]
convert java.awt.Image to com.lowagie.text.Image
how to create an image through bufferedimage,image and other helper class using byte array