Advertisement
momo350

flutter_image_compress errors example code issue directory

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