问题
How do i create new bmp image in java from int array that contains pixels (from getPixel() of pixelGrabber class) ?
Thanks
回答1:
Make a BufferedImage and call setPixel.
BufferedImage: http://download.oracle.com/javase/1.4.2/docs/api/java/awt/image/BufferedImage.html
If you want BMP file then you can use ImageIO.write(bufferedImage,"BMP", new File("mybmp.bmp"));
I would give you the link to ImageIO class but Stack Overflow is preventing me from spamming.
回答2:
In 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))
There may be a problem with saving bitmap in ARGB format, see this: ImageIO.write bmp does not work
来源:https://stackoverflow.com/questions/3697471/int-pixels-array-to-bmp-in-java