0

How do i create new bmp image in java from int array that contains pixels (from getPixel() of pixelGrabber class) ?

Thanks

Ha-eun Chung
  • 455
  • 1
  • 7
  • 20

2 Answers2

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.

MrAnonymous
  • 717
  • 3
  • 7
-1

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

csharpfolk
  • 4,124
  • 25
  • 31
  • OP searches for a **Java** solution. While **Kotlin** gets indeed converted to *JVM* compatible *Bytecode* its source code is surely not compatible with the *Java compiler* such OP pobably can only use the idea but not the code itself. – Zabuzard Jul 22 '17 at 12:12
  • @Zabuza In provided example differences between Java and Kotlin are small, just change `val` to full type name and add `new` here and there and you will get compiling java solution. Anyway I added this answer for *myself* since this is the first question that pop up when I googled for "saving int array to bmp". I added my code, it has nothing to do with OP problem but may save someone a few hours of research... – csharpfolk Jul 22 '17 at 13:11