Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 831
Jaehyung Kim Avatar asked Jun 23 '20 02:06

Jaehyung Kim


2 Answers

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

like image 185
Jaehyung Kim Avatar answered Nov 15 '22 04:11

Jaehyung Kim


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
});
like image 22
Ely Dantas Avatar answered Nov 15 '22 05:11

Ely Dantas