如何在java中将图像转换为字节数组?
我想将图像转换为字节数组,反之亦然.在这里,用户将输入图像的名称(.jpg
),程序将从文件中读取并将其转换为字节数组.
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.
BufferedImage 包含两个主要类:Raster &颜色模型.光栅本身由两个类组成,DataBufferByte 用于图像内容,另一个用于像素颜色.
BufferedImage consists of two main classes: Raster & ColorModel. Raster itself consists of two classes, DataBufferByte for image content while the other for pixel color.
如果你想要来自 DataBufferByte 的数据,使用:
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() );
}
现在您可以通过在 lsb 中隐藏文本来处理这些字节,或者按照您想要的方式进行处理.
now you can process these bytes by hiding text in lsb for example, or process it the way you want.