how to convert image to byte array in java? [duplicate]

匿名 (未验证) 提交于 2019-12-03 01:39:01

问题:

This question already has an answer here:

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.

回答1:

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.



回答2:

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()) 


回答3:

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(); 


回答4:

Try this code snippet

BufferedImage image = ImageIO.read(new File("filename.jpg"));  // Process image  ImageIO.write(image, "jpg", new File("output.jpg")); 


回答5:

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.



回答6:

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"));     } } 

Reference link



回答7:

Try this code:

public class ImageTest {  public static void main(String[] args) {      try {          byte[] imageInByte;         BufferedImage originalImage = ImageIO.read(new File(                 "c:/darksouls.jpg"));          // convert BufferedImage to byte array         ByteArrayOutputStream baos = new ByteArrayOutputStream();         ImageIO.write(originalImage, "jpg", baos);         baos.flush();         imageInByte = baos.toByteArray();         baos.close();          // convert byte array back to BufferedImage         InputStream in = new ByteArrayInputStream(imageInByte);         BufferedImage bImageFromConvert = ImageIO.read(in);          ImageIO.write(bImageFromConvert, "jpg", new File(                 "c:/new-darksouls.jpg"));      } catch (IOException e) {         System.out.println(e.getMessage());     } } } 


回答8:

java.io.FileInputStream is what you're looking for :-)



回答9:

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()



回答10:

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(); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!