Advertisement
Guest User

image and image_picker example (async)

a guest
Oct 11th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.74 KB | None | 0 0
  1. import 'dart:io';
  2. import 'dart:async';
  3.  
  4. import 'package:flutter/material.dart';
  5. import 'package:image/image.dart' as img;
  6. import 'package:image_picker/image_picker.dart';
  7.  
  8. void main() => runApp(MyApp());
  9.  
  10. class MyApp extends StatelessWidget {
  11.   @override
  12.   Widget build(BuildContext context) {
  13.     return MaterialApp(
  14.       title: 'Flutter Demo',
  15.       theme: ThemeData(
  16.         primarySwatch: Colors.blue,
  17.       ),
  18.       home: MyHomePage(title: 'Flutter Demo Home Page'),
  19.     );
  20.   }
  21. }
  22.  
  23. class MyHomePage extends StatefulWidget {
  24.   MyHomePage({Key key, this.title}) : super(key: key);
  25.   final String title;
  26.  
  27.   @override
  28.   _MyHomePageState createState() => new _MyHomePageState();
  29. }
  30.  
  31. class _MyHomePageState extends State<MyHomePage> {
  32.   File _originalImage;
  33.   File _compressedImage;
  34.   Size _originalResolution;
  35.   Size _compressedResolution;
  36.   bool _isLoading = false;
  37.   img.Image image;
  38.   void _loadImage() async {
  39.     setState(() {
  40.       _isLoading = true;
  41.     });
  42.     //final file = await ImagePicker.pickImage(source: ImageSource.camera, maxWidth: 400.0 );
  43.     final file = await ImagePicker.pickImage(source: ImageSource.camera);
  44.     _originalImage = file;
  45.     image = img.decodeJpg(await file.readAsBytes());
  46.     _originalResolution = Size(image.width.toDouble(), image.height.toDouble());
  47.     image = img.copyResize(image, 400);
  48.     _compressedResolution =
  49.         Size(image.width.toDouble(), image.height.toDouble());
  50.     _compressedImage = File('${_originalImage.parent.path}/compressed.jpg');
  51.     await _compressedImage.writeAsBytes(img.encodeJpg(image, quality: 60));
  52.     // _compressedImage.open();
  53.     image = null;
  54.  
  55.     setState(() {
  56.       _isLoading = false;
  57.     });
  58.   }
  59.  
  60.   @override
  61.   Widget build(BuildContext context) {
  62.     return Scaffold(
  63.       appBar: AppBar(
  64.         title: Text(widget.title),
  65.       ),
  66.       body: _isLoading
  67.           ? Center(
  68.               child: CircularProgressIndicator(),
  69.             )
  70.           : SingleChildScrollView(
  71.               child: Column(
  72.                 mainAxisAlignment: MainAxisAlignment.spaceAround,
  73.                 crossAxisAlignment: CrossAxisAlignment.start,
  74.                 children: <Widget>[
  75.                   FittedBox(
  76.                     child: Row(
  77.                       children: <Widget>[
  78.                         _originalImage != null
  79.                             ? Image.file(
  80.                                 _originalImage,
  81.                                 fit: BoxFit.scaleDown,
  82.                               )
  83.                             : Text("Load a new image to compress..."),
  84.                         _compressedImage != null
  85.                             ? Image.file(
  86.                                 _compressedImage,
  87.                                 fit: BoxFit.scaleDown,
  88.                               )
  89.                             : Text("Load a new image to compress..."),
  90.                       ],
  91.                     ),
  92.                   ),
  93.                   Text('Original Image Info:',
  94.                       style: TextStyle(fontSize: 30.0)),
  95.                   Text(
  96.                       'Resolution: ${_originalResolution?.width?.toInt()}x${_originalResolution?.height?.toInt()}'),
  97.                   Text('Path:${_originalImage?.path}'),
  98.                   FutureBuilder<int>(
  99.                     future: _originalImage?.length(),
  100.                     builder: (context, snapshot) {
  101.                       if (snapshot.hasData) {
  102.                         final size =
  103.                             (snapshot.data / 1024.0).toStringAsFixed(2);
  104.                         return Text('Size: $size Kb');
  105.                       }
  106.                       return Text('Size: not available');
  107.                     },
  108.                   ),
  109.                   Text('Compressed Image Info:',
  110.                       style: TextStyle(fontSize: 30.0)),
  111.                   Text(
  112.                       'Resolution: ${_compressedResolution?.width?.toInt()}x${_compressedResolution?.height?.toInt()}'),
  113.                   Text('Path:${_compressedImage?.path}'),
  114.                   FutureBuilder<int>(
  115.                     future: _compressedImage?.length(),
  116.                     builder: (context, snapshot) {
  117.                       if (snapshot.hasData) {
  118.                         final size =
  119.                             (snapshot.data / 1024.0).toStringAsFixed(2);
  120.                         return Text('Size: $size Kb');
  121.                       }
  122.                       return Text('Size: not available');
  123.                     },
  124.                   ),
  125.                 ],
  126.               ),
  127.             ),
  128.       floatingActionButton: FloatingActionButton(
  129.         onPressed: _loadImage,
  130.         tooltip: 'ImportImage',
  131.         child: Icon(Icons.photo_camera),
  132.       ),
  133.     );
  134.   }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement