tags:

views:

4782

answers:

5

hi i want to read an image and convert it to byte[] for my use. what should I do?

+10  A: 

You need to read each byte from your InputStream and write it to a ByteArrayOutputStream. You can then retrieve the underlying byte array by calling toByteArray(); e.g.

InputStream is = ...
ByteArrayOutputStream buffer = new ByteArrayOutputStream();

int nRead;
byte[] data = new byte[16384];

while ((nRead = is.read(data, 0, data.length)) != -1) {
  buffer.write(data, 0, nRead);
}

buffer.flush();

return buffer.toByteArray();
Adamski
+12  A: 

You can use Apache commons-io to handle this and similar tasks.

The IOUtils type has a static method to read an InputStream and return a byte[].

InputStream is;
byte[] bytes = IOUtils.toByteArray(is);

Internally this creates a ByteArrayOutputStream and copies the bytes to the output, then calls to ByteArray(). It handles large files by copying the bytes in blocks of 4MB.

Rich Seller
For the want of writing 4 lines of code, you think that importing a 3rd-party dependency is worthwhile?
oxbow_lakes
If there is a library that handles the requirement, and deals with processing for large files, and is well tested, surely the question is why would I write it myself? The jar is only 107KB and if you have need for one method from it, you are likely to use others too
Rich Seller
@oxbow_lakes: considering the staggering amount of **wrong** implementations of this feature I've seen in my developer life, I feel that **yes** it's *very* much worth the external dependency to get it right.
Joachim Sauer
oxbow_lakes
A: 

Below Codes

public static byte[] serializeObj(Object obj) throws IOException {
  ByteArrayOutputStream baOStream = new ByteArrayOutputStream();
  ObjectOutputStream objOStream = new ObjectOutputStream(baOStream);

  objOStream.writeObject(obj); 
  objOStream.flush();
  objOStream.close();
  return baOStream.toByteArray(); 
}

OR

BufferedImage img = ...
ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
ImageIO.write(img, "jpeg", baos);
baos.flush();
byte[] result = baos.toByteArray();
baos.close();
Firstthumb
+2  A: 

Do you really need the image as a byte[]? What exactly do you expect in the byte[] - the complete content of an image file, encoded in whatever format the image file is in, or RGB pixel values?

Other answers here show you how to read a file into a byte[]. Your byte[] will contain the exact contents of the file, and you'd need to decode that to do anything with the image data.

Java's standard API for reading (and writing) images is the ImageIO API, which you can find in the package javax.imageio. You can read in an image from a file with just a single line of code:

BufferedImage image = ImageIO.read(new File("image.jpg"));

This will give you a BufferedImage, not a byte[]. To get at the image data, you can call getRaster() on the BufferedImage. This will give you a Raster object, which has methods to access the pixel data (it has several getPixel() / getPixels() methods).

Lookup the API documentation for javax.imageio.ImageIO, java.awt.image.BufferedImage, java.awt.image.Raster etc.

ImageIO supports a number of image formats by default: JPEG, PNG, BMP, WBMP and GIF. It's possible to add support for more formats (you'd need a plug-in that implements the ImageIO service provider interface).

See also the following tutorial: Working with Images

Jesper
A: 
`Input Stream is ...
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int next = in.read();
while (next > -1) {
  bos.write(next);
  next = in.read();
}
bos.flush();
byte[] result = bos.toByteArray();`
Aturio