Advertisement
Guest User

Untitled

a guest
Oct 12th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 5.24 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.         final r = 55;
  51.     _compressedImage = File('${_originalImage.parent.path}/compressed$r.jpg');
  52.     await _compressedImage.writeAsBytes(img.encodeJpg(image, quality: 60));
  53.     // _compressedImage.open();
  54.     image = null;
  55.  
  56.     setState(() {
  57.       _isLoading = false;
  58.     });
  59.   }
  60.  
  61.   @override
  62.   Widget build(BuildContext context) {
  63.     return Scaffold(
  64.       appBar: AppBar(
  65.         title: Text(widget.title),
  66.       ),
  67.       body: _isLoading
  68.           ? Center(
  69.               child: CircularProgressIndicator(),
  70.             )
  71.           : SingleChildScrollView(
  72.               child: Column(
  73.                 mainAxisAlignment: MainAxisAlignment.spaceAround,
  74.                 crossAxisAlignment: CrossAxisAlignment.start,
  75.                 children: <Widget>[
  76.                   FittedBox(
  77.                     child: Row(
  78.                       mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  79.                       crossAxisAlignment: CrossAxisAlignment.center,
  80.                       children: <Widget>[
  81.                         _originalImage != null
  82.                             ? Container(
  83.                                 width: MediaQuery.of(context).size.width / 2,
  84.                                 child: Image.file(
  85.                                   _originalImage,
  86.                                   fit: BoxFit.scaleDown,
  87.                                 ),
  88.                               )
  89.                             : Text("Load a new image to compress..."),
  90.                         _compressedImage != null
  91.                             ? Container(
  92.                                 width: MediaQuery.of(context).size.width / 2,
  93.                                 child: Image.file(
  94.                                   _compressedImage,
  95.                                   fit: BoxFit.scaleDown,
  96.                                 ),
  97.                               )
  98.                             : Text("Load a new image to compress..."),
  99.                       ],
  100.                     ),
  101.                   ),
  102.                   Text('Original Image Info:',
  103.                       style: TextStyle(fontSize: 30.0)),
  104.                   Text(
  105.                       'Resolution: ${_originalResolution?.width?.toInt()}x${_originalResolution?.height?.toInt()}'),
  106.                   Text('Path:${_originalImage?.path}'),
  107.                   FutureBuilder<int>(
  108.                     future: _originalImage?.length(),
  109.                     builder: (context, snapshot) {
  110.                       if (snapshot.hasData) {
  111.                         final size =
  112.                             (snapshot.data / 1024.0).toStringAsFixed(2);
  113.                         return Text('Size: $size Kb');
  114.                       }
  115.                       return Text('Size: not available');
  116.                     },
  117.                   ),
  118.                   Text('Compressed Image Info:',
  119.                       style: TextStyle(fontSize: 30.0)),
  120.                   Text(
  121.                       'Resolution: ${_compressedResolution?.width?.toInt()}x${_compressedResolution?.height?.toInt()}'),
  122.                   Text('Path:${_compressedImage?.path}'),
  123.                   FutureBuilder<int>(
  124.                     future: _compressedImage?.length(),
  125.                     builder: (context, snapshot) {
  126.                       if (snapshot.hasData) {
  127.                         final size =
  128.                             (snapshot.data / 1024.0).toStringAsFixed(2);
  129.                         return Text('Size: $size Kb');
  130.                       }
  131.                       return Text('Size: not available');
  132.                     },
  133.                   ),
  134.                 ],
  135.               ),
  136.             ),
  137.       floatingActionButton: FloatingActionButton(
  138.         onPressed: _loadImage,
  139.         tooltip: 'ImportImage',
  140.         child: Icon(Icons.photo_camera),
  141.       ),
  142.     );
  143.   }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement