firebase 如何使用多图像拾取器与抖动图像压缩?

txu3uszq  于 2023-01-05  发布在  其他
关注(0)|答案(2)|浏览(160)

我已经成功地实现了flutter多图像拾取器。但是我想降低它的质量。我看到过一些线程,据说使用flutter_Image_compress库。但是我似乎不明白如何用多图像拾取器实现它。
多图像选取器

Future<List<Asset>> loadAssets() async {
List<Asset> resultList = List<Asset>();
String error = "No error Detected";

try {
  resultList = await MultiImagePicker.pickImages(
    maxImages: 10,
    enableCamera: true,
    selectedAssets: images,
    cupertinoOptions: CupertinoOptions(takePhotoIcon: "chat"),
    materialOptions: MaterialOptions(
      actionBarColor: "#abcdef",
      actionBarTitle: "Upload Image",
      allViewTitle: "All Photos",
      useDetailsView: false,
      selectCircleStrokeColor: "#000000",
    ),
  );

  showInSnackBar("loading images");
  print(resultList.length);
  print((await resultList[0].getThumbByteData(122, 100)));
  print((await resultList[0].getByteData()));
  print((await resultList[0].metadata));
  print("loadAssets is called");

} on Exception catch (e) {
  error = e.toString();
  print(error);
}


if (!mounted){
  print("Not mounted");
}
else {
  setState(() {
    images = resultList;
    _error = error;
  });
}

return images;

}
Flutter图像压缩

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

    final compressedImage = await FlutterImageCompress.compressAndGetFile(
        filePath,
        outPath,
        minWidth: 1000,
        minHeight: 1000,
        quality: 70);
  }

这就是我所做的

Future<List<Asset>> loadAssets() async {
    List<Asset> resultList = List<Asset>();
    List<File> fileImageArray=[];
    String error = "No error Detected";

    try {

      resultList = await MultiImagePicker.pickImages(
        maxImages: 10,
        enableCamera: true,
        selectedAssets: images,
        cupertinoOptions: CupertinoOptions(takePhotoIcon: "chat"),
        materialOptions: MaterialOptions(
          actionBarColor: "#abcdef",
          actionBarTitle: "Upload Image",
          allViewTitle: "All Photos",
          useDetailsView: false,
          selectCircleStrokeColor: "#000000",
        ),

      );
      resultList.forEach((imageAsset) async {
        final filePath = await FlutterAbsolutePath.getAbsolutePath(imageAsset.identifier);

        File tempFile = File(filePath);
        if (tempFile.existsSync()) {
          fileImageArray.add(tempFile);
        }
      });
compressImage(fileImageArray);

      showInSnackBar("loading images");
      print(resultList.length);
      print((await resultList[0].getThumbByteData(122, 100)));
      print((await resultList[0].getByteData()));
      print((await resultList[0].metadata));
      print("loadAssets is called");

    } on Exception catch (e) {
      error = e.toString();
      print(error);
    }
    if (!mounted){
      print("Not mounted");
    }
    else {
      setState(() {
        print('Presed1');
        images = resultList;
        _error = error;
      });
    }

    return images;
  }

  void compressImage(fileImageArray) async {
    for(var i in fileImageArray){
      final filePath = i.absolute.path;
      final lastIndex = i.lastIndexOf(new RegExp(r'.jp'));
      final splitted = i.substring(0, (lastIndex));
      final outPath = "${splitted}_out${filePath.substring(lastIndex)}";

      final compressedImage = await FlutterImageCompress.compressAndGetFile(
          filePath,
          outPath,
          minWidth: 240,
          minHeight: 240,
          quality: 5);
      setState(() {
   print('pressed2');
        fileImageArray= compressedImage;
      });
    }

}

onPressed: () async {
                        List<Asset> asst = await loadAssets();
                        if (asst.length == 0) {
                          showAlert("No images selected");
                        }
                        SizedBox(height: 10,);

                        showInSnackBar('Images Successfully loaded');
                        //                 SnackBar snackbar = SnackBar(content: Text('Please wait, we are uploading'));
                        //_scaffoldKey.currentState.showSnackBar(new SnackBar(content: new Text(value)));
                      }
cnjp1d6j

cnjp1d6j1#

请使用此选项将列表转换为列表

List<File> fileImageArray=[];
...
resultList.forEach((imageAsset) async {
    final filePath = await FlutterAbsolutePath.getAbsolutePath(imageAsset.identifier);
    
    File tempFile = File(filePath);
    if (tempFile.existsSync()) {
        fileImageArray.add(tempFile);
    }
});

fileImageArray赋给compressImage方法,并使用for循环对其进行迭代

void compressImage(fileImageArray) async {
    for(var i in fileImageArray){
    final filePath = i.absolute.path;
    final lastIndex = i.lastIndexOf(new RegExp(r'.jp'));
    final splitted = i.substring(0, (lastIndex));
    final outPath = "${splitted}_out${filePath.substring(lastIndex)}";

    final compressedImage = await FlutterImageCompress.compressAndGetFile(
        filePath,
        outPath,
        minWidth: 240,
        minHeight: 240,
        quality: 5);
    setState(() {
      fileImageArray= compressedImage;
    });
   }
  }
bttbmeg0

bttbmeg02#

Flutter Absolute Path不再处于开发阶段,也没有在android v2嵌入中更新。因此,我建议使用下面的path_provider将资产转换为文件并压缩:

import 'package:multi_image_picker/multi_image_picker.dart';
import 'package:path_provider/path_provider.dart';

Future<File> compressAndUploadAssetImage(Asset asset, Reference ref) async {
        final byteData = await asset.getByteData();
        final tempFile = File("${(await getTemporaryDirectory()).path}/${asset.name}");
        final file = await tempFile.writeAsBytes(byteData.buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes),);
        File compressedFile = await FlutterNativeImage.compressImage(file.path, quality: 70);
        return compressedFile;

    }

相关问题