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