I want to convert an image to byte array and vice versa. Here, the user will enter the name of the image (.jpg
) and program will read it from the file and will convert it to a byte array.
9 Answers
If you are using JDK 7 you can use the following code..
import java.nio.file.Files;
import java.io.File;
File fi = new File("myfile.jpg");
byte[] fileContent = Files.readAllBytes(fi.toPath())
BufferedImage consists of two main classes: Raster & ColorModel. Raster itself consists of two classes, DataBufferByte for image content while the other for pixel color.
if you want the data from DataBufferByte, use:
public byte[] extractBytes (String ImageName) throws IOException {
// open image
File imgPath = new File(ImageName);
BufferedImage bufferedImage = ImageIO.read(imgPath);
// get DataBufferBytes from Raster
WritableRaster raster = bufferedImage .getRaster();
DataBufferByte data = (DataBufferByte) raster.getDataBuffer();
return ( data.getData() );
}
now you can process these bytes by hiding text in lsb for example, or process it the way you want.
-
if the image is backen by a short array (which was the case for me and a tif image) it will give you a DataBufferShort resulting in a cast exception Commented Jun 21, 2014 at 8:55
-
-
-
12Sorry, but this answer is nonsense. Getting bytes from an image file is not different from getting bytes from an arbitrary file. You don't need Java 2D API for this at all. This approach is clumsy and would only be necessary when you're actually interested in manipulating the image in some way (resizing, cropping, colorizing, etc). Just use
Files#readAllBytes()
or any other sane way you'd usually use on any arbitrary file.– BalusCCommented May 10, 2015 at 17:21 -
5
File fnew=new File("/tmp/rose.jpg");
BufferedImage originalImage=ImageIO.read(fnew);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ImageIO.write(originalImage, "jpg", baos );
byte[] imageInByte=baos.toByteArray();
Try this code snippet
BufferedImage image = ImageIO.read(new File("filename.jpg"));
// Process image
ImageIO.write(image, "jpg", new File("output.jpg"));
Here is a complete version of code for doing this. I have tested it. The BufferedImage
and Base64
class do the trick mainly. Also some parameter needs to be set correctly.
public class SimpleConvertImage {
public static void main(String[] args) throws IOException{
String dirName="C:\\";
ByteArrayOutputStream baos=new ByteArrayOutputStream(1000);
BufferedImage img=ImageIO.read(new File(dirName,"rose.jpg"));
ImageIO.write(img, "jpg", baos);
baos.flush();
String base64String=Base64.encode(baos.toByteArray());
baos.close();
byte[] bytearray = Base64.decode(base64String);
BufferedImage imag=ImageIO.read(new ByteArrayInputStream(bytearray));
ImageIO.write(imag, "jpg", new File(dirName,"snap.jpg"));
}
}
Check out javax.imageio
, especially ImageReader
and ImageWriter
as an abstraction for reading and writing image files.
BufferedImage.getRGB(int x, int y)
than allows you to get RGB values on the given pixel, which can be chunked into bytes.
Note: I think you don't want to read the raw bytes, because then you have to deal with all the compression/decompression.
Using RandomAccessFile
would be simple and handy.
RandomAccessFile f = new RandomAccessFile(filepath, "r");
byte[] bytes = new byte[(int) f.length()];
f.read(bytes);
f.close();
I think the best way to do that is to first read the file into a byte array, then convert the array to an image with ImageIO.read()
File
is very different fromImage
.