brendan-duncan / image

Dart Image Library for opening, manipulating, and saving various different image file formats.
MIT License
1.14k stars 255 forks source link

How to convert pixel data into an Image? #654

Closed qmr777 closed 1 month ago

qmr777 commented 1 month ago

I have the raw pixel data (Uint8List) of an image, the image's width and height, and the pixel format (bgra8888). How do I decode them into a JPG format image?

abstract class PdfImage {
  /// Number of pixels in horizontal direction.
  int get width;

  /// Number of pixels in vertical direction.
  int get height;

  /// Pixel format in either [ui.PixelFormat.rgba8888] or [ui.PixelFormat.bgra8888].
  ui.PixelFormat get format;

  /// Raw pixel data. The actual format is platform dependent.
  Uint8List get pixels;

  /// Create [ui.Image] from the rendered image.
  /// it worked
  Future<ui.Image> createImage() {
    final comp = Completer<ui.Image>();
    ui.decodeImageFromPixels(
        pixels, width, height, format, (image) => comp.complete(image));
    return comp.future;
  }
}
brendan-duncan commented 1 month ago

For raw pixel data you can create an Image from the data,

Image.fromBytes(
  width: width, // int
  height: hieght, // int
  bytes: bytes, // ByteBuffer
  channelOrder: ChannelOrder.bgra
);