【发布时间】:2010-09-13 01:45:19
【问题描述】:
如何在 java 中从包含像素的 int 数组创建新的 bmp 图像(来自 pixelGrabber 类的 getPixel())?
谢谢
【问题讨论】:
如何在 java 中从包含像素的 int 数组创建新的 bmp 图像(来自 pixelGrabber 类的 getPixel())?
谢谢
【问题讨论】:
制作一个 BufferedImage 并调用 setPixel。
缓冲图像:http://download.oracle.com/javase/1.4.2/docs/api/java/awt/image/BufferedImage.html
如果你想要 BMP 文件,那么你可以使用 ImageIO.write(bufferedImage,"BMP", new File("mybmp.bmp"));
我会给你 ImageIO 类的链接,但 Stack Overflow 阻止我发送垃圾邮件。
【讨论】:
在 Kotlin 中:
// As it happens default color model has AARRGGBB format
// in other words alpha + RBG
val colorModel = ColorModel.getRGBdefault()
val raster = colorModel.createCompatibleWritableRaster(
horizontalRes, verticalRes)
val bufferedImage = BufferedImage(
colorModel, raster, colorModel.isAlphaPremultiplied, null)
// rawArgbData = array of int's.
// every int has format = 0xFF|R|G|B (MSB is alpha)
raster.setDataElements(
0, 0, horizontalRes, verticalRes,
rawArgbData)
// finally save
ImageIO.write(bufferedImage, "PNG", File(filePath))
将位图保存为 ARGB 格式可能会出现问题,请参见:ImageIO.write bmp does not work
【讨论】:
val 更改为完整类型名称并在此处和此处添加 new,您将获得编译 java 解决方案。无论如何,我为 myself 添加了这个答案,因为这是我搜索“将 int 数组保存到 bmp”时弹出的第一个问题。我添加了我的代码,它与 OP 问题无关,但可能会为某人节省几个小时的研究时间......