将 Skija 图像转换为 BufferedImage?

Convert Skija Image to BufferedImage?

如何把 Skija Image 转换成 BufferedImage

Skija Image 可以给我一个以某种格式编码的字节列表,但这不是我需要的。

这个小片段对我有用!

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)
}