Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:io';
- import 'dart:async';
- import 'package:flutter/material.dart';
- import 'package:image/image.dart' as img;
- import 'package:image_picker/image_picker.dart';
- void main() => runApp(MyApp());
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'Flutter Demo',
- theme: ThemeData(
- primarySwatch: Colors.blue,
- ),
- home: MyHomePage(title: 'Flutter Demo Home Page'),
- );
- }
- }
- class MyHomePage extends StatefulWidget {
- MyHomePage({Key key, this.title}) : super(key: key);
- final String title;
- @override
- _MyHomePageState createState() => new _MyHomePageState();
- }
- class _MyHomePageState extends State<MyHomePage> {
- File _originalImage;
- File _compressedImage;
- Size _originalResolution;
- Size _compressedResolution;
- bool _isLoading = false;
- img.Image image;
- void _loadImage() async {
- setState(() {
- _isLoading = true;
- });
- //final file = await ImagePicker.pickImage(source: ImageSource.camera, maxWidth: 400.0 );
- final file = await ImagePicker.pickImage(source: ImageSource.camera);
- _originalImage = file;
- image = img.decodeJpg(await file.readAsBytes());
- _originalResolution = Size(image.width.toDouble(), image.height.toDouble());
- image = img.copyResize(image, 400);
- _compressedResolution =
- Size(image.width.toDouble(), image.height.toDouble());
- _compressedImage = File('${_originalImage.parent.path}/compressed.jpg');
- await _compressedImage.writeAsBytes(img.encodeJpg(image, quality: 60));
- // _compressedImage.open();
- image = null;
- setState(() {
- _isLoading = false;
- });
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text(widget.title),
- ),
- body: _isLoading
- ? Center(
- child: CircularProgressIndicator(),
- )
- : SingleChildScrollView(
- child: Column(
- mainAxisAlignment: MainAxisAlignment.spaceAround,
- crossAxisAlignment: CrossAxisAlignment.start,
- children: <Widget>[
- FittedBox(
- child: Row(
- children: <Widget>[
- _originalImage != null
- ? Image.file(
- _originalImage,
- fit: BoxFit.scaleDown,
- )
- : Text("Load a new image to compress..."),
- _compressedImage != null
- ? Image.file(
- _compressedImage,
- fit: BoxFit.scaleDown,
- )
- : Text("Load a new image to compress..."),
- ],
- ),
- ),
- Text('Original Image Info:',
- style: TextStyle(fontSize: 30.0)),
- Text(
- 'Resolution: ${_originalResolution?.width?.toInt()}x${_originalResolution?.height?.toInt()}'),
- Text('Path:${_originalImage?.path}'),
- FutureBuilder<int>(
- future: _originalImage?.length(),
- builder: (context, snapshot) {
- if (snapshot.hasData) {
- final size =
- (snapshot.data / 1024.0).toStringAsFixed(2);
- return Text('Size: $size Kb');
- }
- return Text('Size: not available');
- },
- ),
- Text('Compressed Image Info:',
- style: TextStyle(fontSize: 30.0)),
- Text(
- 'Resolution: ${_compressedResolution?.width?.toInt()}x${_compressedResolution?.height?.toInt()}'),
- Text('Path:${_compressedImage?.path}'),
- FutureBuilder<int>(
- future: _compressedImage?.length(),
- builder: (context, snapshot) {
- if (snapshot.hasData) {
- final size =
- (snapshot.data / 1024.0).toStringAsFixed(2);
- return Text('Size: $size Kb');
- }
- return Text('Size: not available');
- },
- ),
- ],
- ),
- ),
- floatingActionButton: FloatingActionButton(
- onPressed: _loadImage,
- tooltip: 'ImportImage',
- child: Icon(Icons.photo_camera),
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement