File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes byte or int array into BufferedImage Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "byte or int array into BufferedImage" Watch "byte or int array into BufferedImage" New topic
Author

byte or int array into BufferedImage

Bruno Danis
Greenhorn

Joined: Jun 20, 2008
Posts: 5
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

Joined: Oct 27, 2005
Posts: 19216

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


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Bruno Danis
Greenhorn

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,
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

Joined: Oct 27, 2005
Posts: 19216

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: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: byte or int array into BufferedImage
 
Similar Threads
BufferedImage from byte[]
Graphics/Sprites Protection
problem with ImageIO.read
convert java.awt.Image to com.lowagie.text.Image
how to create an image through bufferedimage,image and other helper class using byte array