如何使用image_picker压缩图像 - 颤动

发布于 2025-01-26 14:17:48 字数 551 浏览 4 评论 0原文

我正在构建图像压缩机。为此,我使用的是flutter image_picker软件包,它具有称为imagequality的属性,可让您减少图像的大小。

我要实现的目标是首先通过ImagePicker上传图像,然后将其压缩。因此,我可以得到原始的和压缩的大小(前后)。

目前,它的作用是在上传/选择图像时压缩大小。那就是我不想要的。我如何分为两个步骤(首先上传,然后使用图像质量参数压缩大小)

  void selectImage() async {
    final imagePicker = await ImagePicker().pickImage(source: ImageSource.gallery, imageQuality: 80); // upload and compress (I want to split this process)
    photoSize = await getFileSize(imagePicker!.path, 1);
    setState(() {
      _file = File(imagePicker.path);
    });
  }

I'm building an image compressor. For that, I'm using the flutter image_picker package, it has a property called imageQuality which let you reduce the size of the image.

What I'm trying to achieve is that first upload the image through imagePicker and then compress it. So I can get both original and compressed sizes (before and after).

Currently what it does is compress the size while uploading/picking the image. That's what I do not want. How can I break into two steps (first upload and then compress the size using image quality parameter)

  void selectImage() async {
    final imagePicker = await ImagePicker().pickImage(source: ImageSource.gallery, imageQuality: 80); // upload and compress (I want to split this process)
    photoSize = await getFileSize(imagePicker!.path, 1);
    setState(() {
      _file = File(imagePicker.path);
    });
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不再让梦枯萎 2025-02-02 14:17:48

基于您的问题,为什么不使用 image_compression_flutter_flutter 在选择<<<代码>图像Quality:100

通过使用另一个压缩功能,您可以压缩图像并将压缩图像作为输出。

Based on your question why don't you use Image_compression_flutter to compress image after you have selected the image with imageQuality: 100
.

By using another compression function , you can compress the image and get the compressed image as output.

思念满溢 2025-02-02 14:17:48

请查找以下代码,

您可以检查此 link 用于完整的示例代码。

您可以尝试使用以下功能来压缩图像。

请参阅下面的插件以供压缩图像

flutter_image_compress: ^0.7.0

Future<File> compressImage(File file) async {
    final filePath = file.absolute.path;
    final lastIndex = filePath.lastIndexOf(new RegExp(r'.png|.jp'));
    final splitted = filePath.substring(0, (lastIndex));
    final outPath = "${splitted}_out${filePath.substring(lastIndex)}";

    if (lastIndex == filePath.lastIndexOf(new RegExp(r'.png'))) {
      final compressedImage = await FlutterImageCompress.compressAndGetFile(
          filePath, outPath,
          minWidth: 1000,
          minHeight: 1000,
          quality: 50,
          format: CompressFormat.png);
      return compressedImage;
    } else {
      final compressedImage = await FlutterImageCompress.compressAndGetFile(
        filePath,
        outPath,
        minWidth: 1000,
        minHeight: 1000,
        quality: 50,
      );
      return compressedImage;
    }
  }

 File photoCompressedFile =
            await compressImage(File(pickedImage.path));
        print("Path of Compressed File: ${photoCompressedFile.path}");

Please find the below code

You can check this link for the complete example code.

You can try compressing the image using the following function.

Please refer to the below plugins for compressing image

flutter_image_compress: ^0.7.0

Future<File> compressImage(File file) async {
    final filePath = file.absolute.path;
    final lastIndex = filePath.lastIndexOf(new RegExp(r'.png|.jp'));
    final splitted = filePath.substring(0, (lastIndex));
    final outPath = "${splitted}_out${filePath.substring(lastIndex)}";

    if (lastIndex == filePath.lastIndexOf(new RegExp(r'.png'))) {
      final compressedImage = await FlutterImageCompress.compressAndGetFile(
          filePath, outPath,
          minWidth: 1000,
          minHeight: 1000,
          quality: 50,
          format: CompressFormat.png);
      return compressedImage;
    } else {
      final compressedImage = await FlutterImageCompress.compressAndGetFile(
        filePath,
        outPath,
        minWidth: 1000,
        minHeight: 1000,
        quality: 50,
      );
      return compressedImage;
    }
  }

 File photoCompressedFile =
            await compressImage(File(pickedImage.path));
        print("Path of Compressed File: ${photoCompressedFile.path}");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文