如何在java中将图像转换为字节数组? [英] how to convert image to byte array in java?

查看:227
本文介绍了如何在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& ColorModel的的。 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.

这篇关于如何在java中将图像转换为字节数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆