【发布时间】:2010-12-07 11:50:02
【问题描述】:
我在 Java 中得到一个 byte[] 数组,其中包含图像的字节,我需要将其输出到图像中。我该怎么做呢?
非常感谢
【问题讨论】:
我在 Java 中得到一个 byte[] 数组,其中包含图像的字节,我需要将其输出到图像中。我该怎么做呢?
非常感谢
【问题讨论】:
BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytes));
【讨论】:
如果您知道图像的类型并且只想生成文件,则无需获取 BufferedImage 实例。只需将字节写入具有正确扩展名的文件即可。
try (OutputStream out = new BufferedOutputStream(new FileOutputStream(path))) {
out.write(bytes);
}
【讨论】:
From Database.
Blob blob = resultSet.getBlob("pictureBlob");
byte [] data = blob.getBytes( 1, ( int ) blob.length() );
BufferedImage img = null;
try {
img = ImageIO.read(new ByteArrayInputStream(data));
} catch (IOException e) {
e.printStackTrace();
}
drawPicture(img); // void drawPicture(Image img);
【讨论】:
因为听起来您已经知道 byte[] 数组的格式(例如 RGB、ARGB、BGR 等),您可能可以使用BufferedImage.setRGB(...),或BufferedImage.getRaster() 和WritableRaster.setPixels(...) 的组合或WritableRaster.setSamples(...)。不幸的是,这两种方法都需要您将 byte[] 转换为 int[]、float[] 或 double[] 之一,具体取决于图像格式。
【讨论】:
根据 Java 文档,看起来您需要使用 the MemoryImageSource Class 将字节数组放入内存中的对象中,然后接下来使用 Component.createImage(ImageProducer) (传入实现 ImageProducer 的 MemoryImageSource) .
【讨论】: