关于java ByteArrayOutputStream类

关于java ByteArrayOutputStream类,java,arrays,image,processing,Java,Arrays,Image,Processing,我使用上面的代码获取一个JEPG图像作为字节数组。我想知道这个字节数组中到底是什么。此数组是否包含任何文件头信息或仅包含像素值?例如,如果我想反转图像的颜色,有什么好方法? 非常感谢 这是一个完整的JPEG文件,在内存中 编辑:如果要将像素数据作为数组进行操作,您可能会发现光栅更有用: 例如: 然后,您可以调用其中一个光栅.getPixels方法。ByteArrayOutputStream包含您写入的内容。不多不少。所以你的问题实际上是关于ImageIO.write()。它根据您提供的编码类型写

我使用上面的代码获取一个JEPG图像作为字节数组。我想知道这个字节数组中到底是什么。此数组是否包含任何文件头信息或仅包含像素值?例如,如果我想反转图像的颜色,有什么好方法?
非常感谢

这是一个完整的JPEG文件,在内存中

编辑:如果要将像素数据作为数组进行操作,您可能会发现
光栅
更有用:

例如:


然后,您可以调用其中一个
光栅.getPixels
方法。

ByteArrayOutputStream包含您写入的内容。不多不少。所以你的问题实际上是关于ImageIO.write()。它根据您提供的编码类型写出图像的编码。这是JPEG

以下是读取实际像素值的方法。JPEG信息更难处理

Raster raster = bufferedImage.getData();
publicstaticvoidmain(String…args)抛出IOException{
字符串u=”http://blog.stackoverflow.com/wp-content/uploads/stackoverflow-logo-300.png";
BuffereImage old=ImageIO.read(新URL(u));
BuffereImage Inversed=新的BuffereImage(old.getWidth(),
old.getHeight(),
BuffereImage.TYPE_INT_RGB);
对于(int y=0;y
Raster raster = bufferedImage.getData();
public static void main(String... args) throws IOException {
    String u = "http://blog.stackoverflow.com/wp-content/uploads/stackoverflow-logo-300.png";

    BufferedImage old = ImageIO.read(new URL(u));
    BufferedImage inverted = new BufferedImage(old.getWidth(),
                                               old.getHeight(),
                                               BufferedImage.TYPE_INT_RGB);


    for (int y = 0; y < old.getHeight(); y++) {
        for (int x = 0; x < old.getWidth(); x++) {
            Color oldColor = new Color(old.getRGB(x, y));

            // reverse all but the alpha channel
            Color invertedColor = new Color(255 - oldColor.getRed(),
                                            255 - oldColor.getGreen(),
                                            255 - oldColor.getBlue());

            inverted.setRGB(x, y, invertedColor.getRGB());
        }
    }

    ImageIO.write(inverted, "png", new File("test.png"));
}