如何在Java中将byte []转换为BufferedImage? [英] How to convert a byte[] to a BufferedImage in Java?

查看:714
本文介绍了如何在Java中将byte []转换为BufferedImage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发布此线程是因为我在处理Java图片时遇到一些困难.我希望能够将图片转换为byte []数组,然后进行反向操作,因此可以更改每个像素的RGB,然后制作一张新图片.我想使用此解决方案,因为BufferedImage的setRGB()和getRGB()可能对于大型图片来说太慢了(如果我错了,请纠正我).

I'm posting this thread because I have some difficulties to deal with pictures in Java. I would like to be able to convert a picture into a byte[] array, and then to be able to do the reverse operation, so I can change the RGB of each pixel, then make a new picture. I want to use this solution because setRGB() and getRGB() of BufferedImage may be too slow for huge pictures (correct me if I'm wrong).

我在这里阅读了一些文章,以获得一个byte []数组(例如此处),以便每个像素都由3表示或包含红色,绿色和蓝色值的数组中的4个单元格(当有4个单元格时带有附加的alpha值),这对我来说非常有用并且易于使用.这是我用来获取该数组的代码(存储在我创建的PixelArray类中):

I read some posts here to obtain a byte[] array (such as here) so that each pixel is represented by 3 or 4 cells of the array containing the red, the green and the blue values (with the additional alpha value, when there are 4 cells), which is quite useful and easy to use for me. Here's the code I use to obtain this array (stored in a PixelArray class I've created) :

public PixelArray(BufferedImage image)
{
    width = image.getWidth();
    height = image.getHeight();
    DataBuffer toArray = image.getRaster().getDataBuffer();
    array = ((DataBufferByte) toArray).getData();
    hasAlphaChannel = image.getAlphaRaster() != null;
}

最大的麻烦是,如果要转换图片(例如,删除蓝色/绿色值而仅保留红色),则没有找到任何有效的方法将此byte []数组转换为新图像.一).我尝试过这些解决方案:

My big trouble is that I haven't found any efficient method to convert this byte[] array to a new image, if I wanted to transform the picture (for example, remove the blue/green values and only keeping the red one). I tried those solutions :

1)制作一个DataBuffer对象,然后制作一个SampleModel,以最终创建一个WritableRaster然后是BufferedImage(带有附加的ColorModel和Hashtable对象).它没有用,因为我显然没有我需要的所有信息(我不知道BufferedImage()构造函数的哈希表是什么).

1) Making a DataBuffer object, then make a SampleModel, to finally create a WritableRaster and then BufferedImage (with additional ColorModel and Hashtable objects). It didn't work because I apparently don't have all the information I need (I have no idea what's the Hashtable for BufferedImage() constructor).

2)使用ByteArrayInputStream.这没有用,因为ByteArrayInputStream期望的byte []数组与我的无关:它表示文件的每个字节,而不是每个像素的每个组成部分(每个像素3-4个字节)...

2) Using a ByteArrayInputStream. This didn't work because the byte[] array expected with ByteArrayInputStream has nothing to do with mine : it represents each byte of the file, and not each component of each pixel (with 3-4 bytes for each pixel)...

有人可以帮我吗?

推荐答案

尝试一下:

private BufferedImage createImageFromBytes(byte[] imageData) {
    ByteArrayInputStream bais = new ByteArrayInputStream(imageData);
    try {
        return ImageIO.read(bais);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

这篇关于如何在Java中将byte []转换为BufferedImage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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