关于java ByteArrayOutputStream类

发布于 2024-10-03 22:54:26 字数 441 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

坏尐絯 2024-10-10 22:54:26

这是内存中的完整 JPEG 文件。

编辑:如果您想将像素数据作为数组进行操作,您可能会发现Raster更有帮助:

例如:

Raster raster = bufferedImage.getData();

然后您可以调用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.:

Raster raster = bufferedImage.getData();

You can then call one of the Raster.getPixels methods.

[浮城] 2024-10-10 22:54:26

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.

屌丝范 2024-10-10 22:54:26

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

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"));
}

Here is how you read real pixel values. The JPEG information is much harder to do anything with!

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"));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文