int pixels array to bmp in java

試著忘記壹切 提交于 2019-12-12 02:25:49

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!