Change Image Size From Uint8List Data in Flutter

2,499

Solution 1

I resized the image data in Uint8List like below.

import package:image/image.dart as IMG

Uint8List resizeImage(Uint8List data) {
        Uint8List resizedData = data;
        IMG.Image img = IMG.decodeImage(data);
        IMG.Image resized = IMG.copyResize(img, width: img.width*2, height: img.height*2);
        resizedData = IMG.encodeJpg(resized);
        return resizedData;
     }  

IMG : flutter image package

Solution 2

Have you tried using ui.decodeImageFromList instead of ui.instantiateImageCodec and getNextFrame() ?

import 'dart:ui' as ui;

ui.decodeImageFromList(IMG.encodeJpg(resized), (ui.Image img) {
  // returned ui.Image
});
Share:
2,499
Jaehyung Kim
Author by

Jaehyung Kim

Updated on December 21, 2022

Comments

  • Jaehyung Kim
    Jaehyung Kim over 1 year

    I'm trying to change image size from uint8list data.

    There are lots of Image classes in Flutter as you know, and The final type of image I want to get is ui.Image.

    I get Uint8List Image(.jpg) data from socket communication.
    Now, I want to set the size of the image and get the Image of ui.Image at the end.

    I tried to do that through (dart:ui : ui, package:image/image.dart : IMG)

    IMG.Image img = IMG.decodeImage(data);
    IMG.Image resized = IMG.copyResize(img, width: 400, height: 200);
    ui.Codec codec = await ui.instantiateImageCodec(IMG.encodeJpg(resized));
    ui.Image image = (await codec.getNextFrame()).image;
    

    ,but It freezes the app.

    How can I do that? Is there any way? Thank you.

    Additionally,
    In "flutter/lib/src/widgets/image.dart", Image.memory(Uint8List, width, height) can make it be easy to set image size. Is there any way to get Uint8List from that Image widget?