1

I'm using multi_image_picker package to pick images and upload to server, but before uploading I want to resize images. I'm trying to accomplish it using dart.ui but having a problem:

//assets is List<Asset> from MultiImagePicker.pickImages
 assets.forEach((asset) {
      Future<ByteData> byteData = asset.getByteData();
      byteData.then((d) async {
        List<int> imageData = d.buffer.asUint8List();
        String b64 =base64Encode(imageData);
        print(b64); // prints [/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE...
        //if i send b64 to server then decode it and save as img it's working well

  //Resize
    ui.instantiateImageCodec(imageData,targetHeight: 800, targetWidth: 600)
        .then((codec) {
      codec.getNextFrame().then((frameInfo) async {
        ui.Image i = frameInfo.image;
        ByteData bytes = await i.toByteData();
        List<int> resizedImageData = bytes.buffer.asUint8List();
        String rb64 = base64Encode(resizedImageData);
        print(rb64); // prints too many backslashes:[k5KO/5qWk/+ZlZL/mpaT/5uXlP+alpP/mJSR/5iUkf+YlJH/mZSR/5uWk/+blpP/n5qX/6GcmP+gm5f/oZyY/6GcmP+fmpb/nZi..
        //If i send rb64 to server then server cannot decode and save it.
      });
    });
  });
});
2
  • The important first question is whether Flutter on the mobile or on the Web? On mobile, it works just fine. On the Web, I have about three bug reports currently standing that make it impossible for the time being but I hope it will be fixed soon. :-) Commented Jun 4, 2020 at 10:23
  • I ended up using pub.dev/packages/flutter_native_image to resize Commented Oct 30, 2020 at 8:36

1 Answer 1

2

This is the function I normally use to resize:

import 'dart:ui' as skia;

Future<skia.Image> resizeImage(String path, {int width, int height}) async {
  Uint8List data = await File(path).readAsBytes();
  final codec = await skia.instantiateImageCodec(data, targetWidth: width, targetHeight: height);
  final frame = await codec.getNextFrame();
  return frame.image;
}

As I mentioned in the comment, this is currently not working in Flutter Web but it's due to a bug that will be fixed soon, hopefully.

Sign up to request clarification or add additional context in comments.

2 Comments

how to get Uint8List from resized ui.Image to encode it to base64 to send it to server? I think the code in my question is the same as yours except the encoding part.
Yes, using toByteData(). I can see nothing really wrong with your code. Do you have specific problems with it? Note that toByteData(), unless you provide the format, will return raw RGBA bitmap, not a PNG or JPG or similar, so you have to treat it as such on the server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.