Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAVA : How to create .PNG image from a byte[]?

I have seen some code source, but I do not understand...

I use Java 7

Please, how to convert a RGB (Red,Green,Blue) Byte Array (or something similar) to a .PNG file format ?

Example from an array that could represent "a RGB pixel" :

byte[] aByteArray={0xa,0x2,0xf};

Important Aspect :

I try to generate a .PNG file only from a byte[] "not from a previous existing file"

is it possible with an existing API? ;)

Here my first code :

byte[] aByteArray={0xa,0x2,0xf}; 
ByteArrayInputStream bais = new ByteArrayInputStream(aByteArray); 
File outputfile = new File("image.png"); 
ImageIO.write(bais, "png", outputfile); 

....Error : No suitable Method Found

Here the other version modified from Jeremy but look similar :

byte[] aByteArray={0xa,0x2,0xf};
ByteArrayInputStream bais = new ByteArrayInputStream(aByteArray); 
final BufferedImage bufferedImage = ImageIO.read(newByteArrayInputStream(aByteArray));
ImageIO.write(bufferedImage, "png", new File("image.png")); 

....multiple Errors : image == null! ...... Sure ? Note : I do not search to use a source file

like image 371
MadMonkey Avatar asked Mar 13 '14 02:03

MadMonkey


People also ask

How do I save a byte array as a picture?

Create a ByteArrayInputStream object by passing the byte array (that is to be converted) to its constructor. Read the image using the read() method of the ImageIO class (by passing the ByteArrayInputStream objects to it as a parameter). Finally, Write the image to using the write() method of the ImageIo class.

Can you print [] byte?

You can simply iterate the byte array and print the byte using System. out. println() method.

What is byte [] in Java?

The byte keyword is a data type that can store whole numbers from -128 to 127.

Can we convert byte array to file in Java?

Convert byte[] array to File using Java In order to convert a byte array to a file, we will be using a method named the getBytes() method of String class. Implementation: Convert a String into a byte array and write it in a file.


1 Answers

The Image I/O API deals with images, so you need to make an image from your byte array first before you write it out.

byte[] aByteArray = {0xa,0x2,0xf,(byte)0xff,(byte)0xff,(byte)0xff};
int width = 1;
int height = 2;

DataBuffer buffer = new DataBufferByte(aByteArray, aByteArray.length);

//3 bytes per pixel: red, green, blue
WritableRaster raster = Raster.createInterleavedRaster(buffer, width, height, 3 * width, 3, new int[] {0, 1, 2}, (Point)null);
ColorModel cm = new ComponentColorModel(ColorModel.getRGBdefault().getColorSpace(), false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE); 
BufferedImage image = new BufferedImage(cm, raster, true, null);

ImageIO.write(image, "png", new File("image.png"));

This assumes the byte array has three bytes per pixel (red, green then blue) and the range of values is 0-255.

like image 60
prunge Avatar answered Oct 27 '22 19:10

prunge