Question

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

Thanks

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top