关于java ByteArrayOutputStream类
BufferedImage bufferedImage = ImageIO.read(new File("/...icon.jpg"));
// this writes the bufferedImage into a byte array called resultingBytes
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "jpg", byteArrayOut);
byte[] resultingBytes = byteArrayOut.toByteArray();
我使用上面的代码获取 JEPG 图像作为字节数组。我想知道这个字节数组到底是什么。该数组是否包含任何文件头信息或仅包含像素值?例如,如果我想反转该图像的颜色,有什么好方法呢? 非常感谢!
BufferedImage bufferedImage = ImageIO.read(new File("/...icon.jpg"));
// this writes the bufferedImage into a byte array called resultingBytes
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "jpg", byteArrayOut);
byte[] resultingBytes = byteArrayOut.toByteArray();
I use the above code to get a JEPG image as a byte array. I want to know what exactly is in this byte array. Does this array contain any file header information or just pixel values? And for example, if I want to reverse this image's color, what is a good way to do so?
Thanks so much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是内存中的完整 JPEG 文件。
编辑:如果您想将像素数据作为数组进行操作,您可能会发现
Raster
更有帮助:例如:
然后您可以调用
Raster.getPixels
方法之一。It's a complete JPEG file, in memory.
EDIT: If you want to manipulate pixel data as an array, you may find
Raster
more helpful:E.g.:
You can then call one of the
Raster.getPixels
methods.ByteArrayOutputStream 包含您写入的所有内容。不多不少。所以你的问题实际上是关于 ImageIO.write() 的。它根据您提供的编码类型写出图像的编码。这是 JPEG。
The ByteArrayOutputStream contains whatever you wrote to it. Nothing more, nothing less. So your question is really about ImageIO.write(). Which writes out an encoding of an image according to the encoding type you supply. Which was JPEG.
以下是读取真实像素值的方法。 JPEG 信息更难处理!
Here is how you read real pixel values. The JPEG information is much harder to do anything with!