Advertisement
Guest User

Untitled

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