crop_your_image icon indicating copy to clipboard operation
crop_your_image copied to clipboard

Image has tripled in size after being cropped

Open raulmabe opened this issue 3 years ago • 9 comments

I have tested these on several images and when cropping it multiplies its size up to x3. Why is that? Am I doing something wrong?

flutter: Bytes before cropping 1930990 (1.9 MB)
flutter: Bytes after cropping 4916747 (4.9 MB)

Dart file
class PictureCroppingPage extends StatefulWidget {
  const PictureCroppingPage({
    Key? key,
    required this.picture,
    required this.onCropped,
    this.isCircular = false,
    this.aspectRatio,
  }) : super(key: key);

  final Uint8List picture;
  final Function(Uint8List) onCropped;
  final bool isCircular;
  final double? aspectRatio;

  @override
  _PictureCroppingPageState createState() => _PictureCroppingPageState();
}

class _PictureCroppingPageState extends State<PictureCroppingPage> {
  final _controller = CropController();
  bool isCropping = false;

  @override
  Widget build(BuildContext context) {
    print('Bytes before cropping ${widget.picture.lengthInBytes}');
    return Scaffold(
      appBar: AppBar(
        leading: const CloseButton(),
        title: Text(context.l10n.cropPicturePageTitle),
        elevation: 1.0,
      ),
      body: Crop(
        controller: _controller,
        image: widget.picture,
        onCropped: (picture) {
          widget.onCropped(picture);
          print('Bytes after cropping ${picture.lengthInBytes}');
          context.navigator.pop();
        },
        aspectRatio: widget.aspectRatio,
        withCircleUi: widget.isCircular,
        baseColor: context.theme.backgroundColor,
      ),
      bottomNavigationBar: BottomBarAction(
        builder: (context) {
          return DivenElevatedButton(
            isLoading: isCropping,
            isDisabled: isCropping,
            onPressed: () {
              _controller.crop();

              setState(() {
                isCropping = true;
              });
            },
            label: Text(
              context.l10n.cropPicturePageCroppingButton,
            ),
          );
        },
      ),
    );
  }
}
Images attached

IMG_0003

IMG_0002

raulmabe avatar Aug 17 '21 14:08 raulmabe

Any news?

raulmabe avatar Sep 27 '21 11:09 raulmabe

Any fixes for this? It's a pretty good lib but it's increasing size by 4-5x

aseem2625 avatar Nov 11 '21 16:11 aseem2625

I think this may be a problem of the image package https://github.com/brendan-duncan/image/issues/312

raulmabe avatar Nov 11 '21 17:11 raulmabe

I suppose it's also because the image is encoded as PNG by default? https://github.com/chooyan-eng/crop_your_image/blob/main/lib/src/crop.dart#L687

luca-nardelli avatar Apr 05 '22 14:04 luca-nardelli

Sorry for replying this late.

@luca-nardelli Thank you for your suggestion! Now it's clear what point should be checked. I'll try to fix this problem by determining the image format first and returning cropped data with the same format.

chooyan-eng avatar Apr 05 '22 14:04 chooyan-eng

No problem, you can do that or also (even better) allow passing in a format + quality options, so that it's fully customizable

luca-nardelli avatar Apr 05 '22 14:04 luca-nardelli

Any news?

sandrocsimas avatar Feb 23 '23 00:02 sandrocsimas

I have opened a pull request (https://github.com/chooyan-eng/crop_your_image/pull/102) that would fix this issue, by exposing an encodeFunction parameter.

As an example i tried setting it to encodeJpg(croppedImage, quality: 75) (where encodeJpg comes from package:image) which reduced an image of 1700 KB to about 570 KB

Hannnes1 avatar Jun 02 '23 09:06 Hannnes1