Advertisement
Guest User

Untitled

a guest
May 25th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'dart:async';
  3. import 'package:flutter/services.dart';
  4. import 'package:multi_image_picker/multi_image_picker.dart';
  5. import 'dart:typed_data';
  6. import 'dart:io';
  7. import 'package:image/image.dart' as v3;
  8. //import 'package:flutter/widgets.dart' as v2;
  9.  
  10. void main() => runApp(new MyApp());
  11.  
  12.  
  13. class MyApp extends StatefulWidget {
  14. @override
  15. _MyAppState createState() => new _MyAppState();
  16. }
  17.  
  18. class _MyAppState extends State<MyApp> {
  19. List<Asset> images = List<Asset>();
  20.  
  21. @override
  22. void initState() {
  23. super.initState();
  24. }
  25.  
  26. Widget buildGridView() {
  27. return GridView.count(
  28. crossAxisCount: 3, //images.length
  29. children: List.generate(images.length, (index) {
  30. Asset asset1 = images[index];
  31. var x = asset1.identifier;
  32. print("The asset's name is $x");
  33. //print("The string rep is $grayScaleImage(asset1).toString()");
  34. return AssetThumb(
  35. asset: asset1,
  36. width: 300,
  37. height: 300,
  38. );
  39.  
  40. }), // displays the selected images in a grid view
  41. );
  42. }
  43.  
  44. Future<AssetThumb> assetThumbMaker(Asset asset1) async {
  45. await saveImage(asset1);
  46. return await assetThumbMaker2(asset1);
  47. }
  48.  
  49. Future<AssetThumb> assetThumbMaker2(Asset asset1) async {
  50. await Future.delayed(const Duration(seconds: 5), (){});
  51. print("got here 3");
  52. return AssetThumb(
  53. asset: asset1,
  54. width: 300,
  55. height: 300,
  56. );
  57. }
  58.  
  59. Future<void> saveImage(Asset asset1) async {
  60. var z = await grayScaleImage(asset1);
  61. v3.Image image1 = v3.decodeJpg(z);
  62. v3.Image thumbnail = v3.copyResize(image1, width: 120);
  63. File('assets/thumbnail-test.jpeg').writeAsBytesSync(v3.encodeJpg(thumbnail));
  64. print("got here");
  65. //Image.memory(z);
  66. }
  67.  
  68. Future<void> deleteAssets() async {
  69. await MultiImagePicker.deleteImages(assets: images);
  70. setState(() {
  71. images = List<Asset>();
  72. });
  73. }
  74.  
  75. Future<Uint8List> grayScaleImage(Asset asset) async {
  76. print("here 1");
  77. ByteData bd = await asset.requestThumbnail(
  78. asset.originalWidth, asset.originalHeight,
  79. quality: 100); // width and height
  80. var a2 = bd.buffer.asUint8List();
  81. var input1 =
  82. v3.Image.fromBytes(asset.originalWidth, asset.originalHeight, a2);
  83. v3.Image grayImage = v3.grayscale(input1);
  84. print("here 2");
  85. return Uint8List.fromList(v3.encodeJpg(grayImage));
  86. //return v3.encodeJpg(grayImage);
  87. // Image(image: MemoryImage(listaU8L[0]));
  88. }
  89.  
  90. Future<void> loadAssets() async {
  91. List<Asset> resultList = List<Asset>();
  92. String error = 'No Error Dectected';
  93.  
  94. try {
  95. resultList = await MultiImagePicker.pickImages(
  96. maxImages: 300,
  97. enableCamera: true,
  98. selectedAssets: images,
  99. cupertinoOptions: CupertinoOptions(takePhotoIcon: "chat"),
  100. materialOptions: MaterialOptions(
  101. allViewTitle: "All Photos",
  102. ));
  103. } on PlatformException catch (e) {
  104. error = e.message;
  105. print(error);
  106. }
  107.  
  108. // If the widget was removed from the tree while the asynchronous platform
  109. // message was in flight, we want to discard the reply rather than calling
  110. // setState to update our non-existent appearance.
  111. if (!mounted) return;
  112.  
  113. setState(() {
  114. images = resultList;
  115. print(error);
  116. });
  117. }
  118.  
  119. @override
  120. Widget build(BuildContext context) {
  121. return new MaterialApp(
  122. debugShowCheckedModeBanner: false,
  123. home: new Scaffold(
  124. appBar: new AppBar(
  125. title: const Text('GrayScale Maker'),
  126. centerTitle: true,
  127. backgroundColor: Colors.deepOrangeAccent,
  128. ),
  129. body: Stack(
  130. children: <Widget>[
  131. new Container(
  132. decoration: new BoxDecoration(
  133. image: new DecorationImage(
  134. image: new AssetImage("assets/background.jpg"),
  135. fit: BoxFit.cover,
  136. ), // adds background image
  137. ),
  138. ),
  139. Column(
  140. children: <Widget>[
  141. RaisedButton(
  142. child: Text("Pick images"),
  143. onPressed: loadAssets,
  144. ),
  145. images.length > 0
  146. ? RaisedButton(
  147. child: Text("Remove All Images"),
  148. onPressed: deleteAssets,
  149. )
  150. : Container(),
  151. Expanded(
  152. child: buildGridView(),
  153. )
  154. ],
  155. ),
  156. ],
  157. ),
  158. ),
  159. );
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement