简体   繁体   中英

Convert Skija Image to BufferedImage?

How to convert Skija Image to BufferedImage ?

Skija Image can get me a list of bytes encoded in some format, but that's not what I need.

This little snipped worked for me!

fun Image.toBufferedImage(): BufferedImage {
    val storage = Bitmap()
    storage.allocPixelsFlags(ImageInfo.makeS32(this.width, this.height, ColorAlphaType.PREMUL), false)
    Canvas(storage).drawImage(this, 0f, 0f)

    val bytes = storage.readPixels(storage.imageInfo, (this.width * 4L), 0, 0)!!
    val buffer = DataBufferByte(bytes, bytes.size)
    val raster = Raster.createInterleavedRaster(
        buffer,
        this.width,
        this.height,
        this.width * 4, 4,
        intArrayOf(2, 1, 0, 3),     // BGRA order
        null
    )
    val colorModel = ComponentColorModel(
        ColorSpace.getInstance(ColorSpace.CS_sRGB),
        true,
        false,
        Transparency.TRANSLUCENT,
        DataBuffer.TYPE_BYTE
    )

    return BufferedImage(colorModel, raster!!, false, null)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM