• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

byte or int array into BufferedImage

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic