• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

byte or int array into BufferedImage

 
Bruno Danis
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
can you help me please with converting byte array into BufferedImage
in case of BMP and TYP_INT_RGB of an image?

thank you
Bruno
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried so far? At the ranch we do not provide complete answers like this - we are NotACodeMill.
 
Bruno Danis
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Transparency.OPAQUE,
DataBuffer.TYPE_BYTE);

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) /
Math.log(2));
IndexColorModel model = new IndexColorModel(bits,
colors,
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
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic