Advertisement
momo350

flutter_image_compress fix

Oct 9th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.53 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'dart:async';
  3. import 'dart:io';
  4.  
  5. import 'package:flutter/services.dart';
  6. import 'package:flutter_image_compress/flutter_image_compress.dart';
  7.  
  8. // import 'package:path_provider/path_provider.dart';
  9. // import 'package:image_picker/image_picker.dart';
  10.  
  11. void main() => runApp(new MyApp());
  12.  
  13. class MyApp extends StatefulWidget {
  14.   @override
  15.   _MyAppState createState() => new _MyAppState();
  16. }
  17.  
  18. class _MyAppState extends State<MyApp> {
  19.   @override
  20.   void initState() {
  21.     super.initState();
  22.   }
  23.  
  24.   // Platform messages are asynchronous, so we initialize in an async method.
  25.   Future<void> compress() async {
  26.     var img = AssetImage("img/img.jpg");
  27.     print("pre compress");
  28.     var config = new ImageConfiguration();
  29.  
  30.     AssetBundleImageKey key = await img.obtainKey(config);
  31.     final ByteData data = await key.bundle.load(key.name);
  32.  
  33.     var beforeCompress = data.lengthInBytes;
  34.     print("beforeCompress = $beforeCompress");
  35.  
  36.     var result =
  37.         await FlutterImageCompress.compressWithList(data.buffer.asUint8List());
  38.  
  39.     print("after = ${result?.length ?? 0}");
  40.   }
  41.  
  42.   ImageProvider provider;
  43.  
  44.   @override
  45.   Widget build(BuildContext context) {
  46.     return new MaterialApp(
  47.       home: new Scaffold(
  48.         appBar: new AppBar(
  49.           title: const Text('Plugin example app'),
  50.         ),
  51.         body: new Center(
  52.           child: Column(
  53.             children: <Widget>[
  54.               Image(
  55.                 image: provider ?? AssetImage("img/img.jpg"),
  56.               ),
  57.               FlatButton(
  58.                 child: Text('capture'),
  59.                 onPressed: _capture,
  60.               ),
  61.               FlatButton(
  62.                 child: Text('file image'),
  63.                 onPressed: getFileImage,
  64.               ),
  65.             ],
  66.           ),
  67.         ),
  68.         floatingActionButton: FloatingActionButton(
  69.           child: Icon(Icons.computer),
  70.           onPressed: _capture,
  71.         ),
  72.       ),
  73.     );
  74.   }
  75.  
  76.   Future<Directory> getTemporaryDirectory() async {
  77.     return Directory.current;
  78.   }
  79.  
  80.   void _capture() async {
  81.     var img = AssetImage("img/img.jpg");
  82.     print("pre compress");
  83.     var config = new ImageConfiguration();
  84.  
  85.     AssetBundleImageKey key = await img.obtainKey(config);
  86.     final ByteData data = await key.bundle.load(key.name);
  87.     var dir = await getTemporaryDirectory();
  88.  
  89.     File file = File("${dir.absolute.path}/test.png");
  90.     file.writeAsBytesSync(data.buffer.asUint8List());
  91.  
  92.     List<int> list = await testCompressFile(file);
  93.     ImageProvider provider = MemoryImage(list);
  94.     this.provider = provider;
  95.     setState(() {});
  96.   }
  97.  
  98.   void getFileImage() async {
  99.     var img = AssetImage("img/img.jpg");
  100.     print("pre compress");
  101.     var config = new ImageConfiguration();
  102.  
  103.     AssetBundleImageKey key = await img.obtainKey(config);
  104.     final ByteData data = await key.bundle.load(key.name);
  105.     var dir = await getTemporaryDirectory();
  106.  
  107.     File file = File("${dir.absolute.path}/test.png");
  108.     file.writeAsBytesSync(data.buffer.asUint8List());
  109.  
  110.     var targetPath = dir.absolute.path + "/temp.png";
  111.     var imgFile = await testCompressAndGetFile(file, targetPath);
  112.  
  113.     provider = FileImage(imgFile);
  114.     setState(() {});
  115.   }
  116.  
  117.   Future<List<int>> testCompressFile(File file) async {
  118.     var result = await FlutterImageCompress.compressWithFile(
  119.       file.absolute.path,
  120.       minWidth: 2300,
  121.       minHeight: 1500,
  122.       quality: 94,
  123.     );
  124.     print(file.lengthSync());
  125.     print(result.length);
  126.     return result;
  127.   }
  128.  
  129.   Future<File> testCompressAndGetFile(File file, String targetPath) async {
  130.     var result = await FlutterImageCompress.compressAndGetFile(
  131.         file.absolute.path, targetPath,
  132.         quality: 88);
  133.  
  134.     print(file.lengthSync());
  135.     print(result.lengthSync());
  136.  
  137.     return result;
  138.   }
  139.  
  140.   Future<List<int>> testCompressAsset(String assetName) async {
  141.     var list = await FlutterImageCompress.compressAssetImage(
  142.       assetName,
  143.       minHeight: 1920,
  144.       minWidth: 1080,
  145.       quality: 96,
  146.     );
  147.  
  148.     return list;
  149.   }
  150.  
  151.   Future<List<int>> testComporessList(List<int> list) async {
  152.     var result = await FlutterImageCompress.compressWithList(
  153.       list,
  154.       minHeight: 1920,
  155.       minWidth: 1080,
  156.       quality: 96,
  157.     );
  158.     print(list.length);
  159.     print(result.length);
  160.     return result;
  161.   }
  162.  
  163.   void writeToFile(List<int> list, String filePath) {
  164.     var file = File(filePath);
  165.     file.writeAsBytes(list, flush: true, mode: FileMode.write);
  166.   }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement