如何在java中将图像转换为字节数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3211156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to convert image to byte array in java?
提问by userv
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 fileand will convert it to a byte array.
我想将图像转换为字节数组,反之亦然。在这里,用户将输入图像的名称 ( .jpg
),程序将从文件中读取它并将其转换为字节数组。
采纳答案by Wajdy Essam
BufferedImage consists of two main classes: Raster & ColorModel. Raster itself consists of two classes, DataBufferBytefor image content while the other for pixel color.
BufferedImage 包含两个主要类:Raster 和 ColorModel。Raster 本身由两个类组成,DataBufferByte用于图像内容,另一个用于像素颜色。
if you want the data from DataBufferByte, use:
如果您想要来自 DataBufferByte 的数据,请使用:
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.
现在您可以通过在 lsb 中隐藏文本来处理这些字节,或者按照您想要的方式处理它。
回答by Vanya
java.io.FileInputStream is what you're looking for :-)
java.io.FileInputStream 就是你要找的 :-)
回答by b_erb
Check out javax.imageio
, especially ImageReader
and ImageWriter
as an abstraction for reading and writing image files.
退房javax.imageio
,特别是ImageReader
和ImageWriter
用于读取和写入图像文件的抽象。
BufferedImage.getRGB(int x, int y)
than allows you to get RGB values on the given pixel, which can be chunked into bytes.
BufferedImage.getRGB(int x, int y)
than 允许您获取给定像素的 RGB 值,可以将其分块为字节。
Note: I think you don't want to read the raw bytes, because then you have to deal with all the compression/decompression.
注意:我认为您不想读取原始字节,因为那样您必须处理所有压缩/解压缩。
回答by Maurice Perry
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()
我认为最好的方法是首先将文件读入字节数组,然后使用 ImageIO.read() 将数组转换为图像
回答by McK
Try this code snippet
试试这个代码片段
BufferedImage image = ImageIO.read(new File("filename.jpg"));
// Process image
ImageIO.write(image, "jpg", new File("output.jpg"));
回答by user717325
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();
回答by Darryl Coward
If you are using JDK 7 you can use the following code..
如果您使用的是 JDK 7,则可以使用以下代码。
import java.nio.file.Files;
import java.io.File;
File fi = new File("myfile.jpg");
byte[] fileContent = Files.readAllBytes(fi.toPath())
回答by Ryan
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.
这是执行此操作的完整代码版本。我已经测试过了。TheBufferedImage
和Base64
class 主要是用来解决问题的。还有一些参数需要正确设置。
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"));
}
}
回答by Gowtham Gopalakrishnan
Using RandomAccessFile
would be simple and handy.
使用RandomAccessFile
将是简单和方便的。
RandomAccessFile f = new RandomAccessFile(filepath, "r");
byte[] bytes = new byte[(int) f.length()];
f.read(bytes);
f.close();