Jaehyung Kim
Jaehyung Kim

Reputation: 303

Change Image Size From Uint8List Data in Flutter

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?

Upvotes: 11

Views: 9281

Answers (2)

Jaehyung Kim
Jaehyung Kim

Reputation: 303

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

Upvotes: 12

Ely Dantas
Ely Dantas

Reputation: 705

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

Upvotes: 1

Related Questions