Advertisement
Guest User

image and image_picker example

a guest
Oct 11th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.93 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.   img.Image image;
  37.   void _loadImage() {
  38.     //ImagePicker.pickImage(source: ImageSource.camera, maxWidth: 400.0 ).then((file) {
  39.     ImagePicker.pickImage(source: ImageSource.camera).then((file) {
  40.       setState(() {
  41.         _originalImage = file;
  42.         image = img.decodeJpg(file.readAsBytesSync());
  43.         _originalResolution =
  44.             Size(image.width.toDouble(), image.height.toDouble());
  45.         image = img.copyResize(image, 400);
  46.         _compressedResolution =
  47.             Size(image.width.toDouble(), image.height.toDouble());
  48.         _compressedImage = File('${_originalImage.parent.path}/compressed.jpg');
  49.         _compressedImage.writeAsBytesSync(img.encodeJpg(image, quality: 60));
  50.         // _compressedImage.open();
  51.         image = null;
  52.       });
  53.     });
  54.   }
  55.  
  56.   @override
  57.   Widget build(BuildContext context) {
  58.     return Scaffold(
  59.       appBar: AppBar(
  60.         title: Text(widget.title),
  61.       ),
  62.       body: SingleChildScrollView(
  63.         child: Column(
  64.           mainAxisAlignment: MainAxisAlignment.spaceAround,
  65.           crossAxisAlignment: CrossAxisAlignment.start,
  66.           children: <Widget>[
  67.             FittedBox(
  68.               child: Row(
  69.                 children: <Widget>[
  70.                   _originalImage != null
  71.                       ? Image.file(
  72.                           _originalImage,
  73.                           fit: BoxFit.scaleDown,
  74.                         )
  75.                       : Text("Load a new image to compress..."),
  76.                   _compressedImage != null
  77.                       ? Image.file(
  78.                           _compressedImage,
  79.                           fit: BoxFit.scaleDown,
  80.                         )
  81.                       : Text("Load a new image to compress..."),
  82.                 ],
  83.               ),
  84.             ),
  85.             Text('Original Image Info:', style: TextStyle(fontSize: 30.0)),
  86.             Text(
  87.                 'Resolution: ${_originalResolution?.width?.toInt()}x${_originalResolution?.height?.toInt()}'),
  88.             Text('Path:${_originalImage?.path}'),
  89.             FutureBuilder<int>(
  90.               future: _originalImage?.length(),
  91.               builder: (context, snapshot) {
  92.                 final size = (snapshot.data / 1024.0).toStringAsFixed(2);
  93.                 return Text('size: $size Kb');
  94.               },
  95.             ),
  96.             Text('Compressed Image Info:', style: TextStyle(fontSize: 30.0)),
  97.             Text(
  98.                 'Resolution: ${_compressedResolution?.width?.toInt()}x${_compressedResolution?.height?.toInt()}'),
  99.             Text('Path:${_compressedImage?.path}'),
  100.             FutureBuilder<int>(
  101.               future: _compressedImage?.length(),
  102.               builder: (context, snapshot) {
  103.                 final size = (snapshot.data / 1024.0).toStringAsFixed(2);
  104.                 return Text('size: $size Kb');
  105.               },
  106.             ),
  107.           ],
  108.         ),
  109.       ),
  110.       floatingActionButton: FloatingActionButton(
  111.         onPressed: _loadImage,
  112.         tooltip: 'ImportImage',
  113.         child: Icon(Icons.photo_camera),
  114.       ),
  115.     );
  116.   }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement