camera_utils 0.1.8

  • Readme
  • Changelog
  • Example
  • Installing
  • 89

For help getting started with Flutter, view our online documentation.

Pub

camera_utils #

Flutter plugin for capturing and picking image and videos and getting thumbnail from videos on the Android & iOS devices.

Implementation in Flutter #

Simply add a dependency to you pubspec.yaml for camera_utils.

Then import the package in your dart file with

import 'package:camera_utils/camera_utils.dart';

Usages #

  1. Capture Image

     // Capture image
     final path = await CameraUtils.captureImage;
    
  2. Pick Image

      // Pick image
     final path = await CameraUtils.pickImage;
    
  3. Capture Video

     // Capture video
     final path = await CameraUtils.captureVideo;
    
  4. Pick Video

     // Pick video
     final path = await CameraUtils.pickVideo;
    
  5. Thumbnail from Video

     // Pass the path and get thumbnail from video
     Future<String> thumbPath = CameraUtils.getThumbnail(path);
       thumbPath.then((path) {
         setState(() {
           _thumbPath = path;
           print(path);
         });
       });
    

0.1.8 #

  • README.md file updated

0.1.7 #

  • Swift code updated

  • README.md file updated

0.1.6 #

  • README.md file updated

0.1.5 #

  • README.md file updated

0.1.4 #

  • README.md file updated

0.1.3 #

  • Moved to Flutter stable channel

0.1.2 #

  • Moved to Flutter stable channel

0.1.1 #

  • Moved to Flutter stable channel

0.1.0 #

  • Initial Release

example/README.md

Example #


import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:camera_utils/camera_utils.dart';

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

void main() => runApp(
      MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'Flutter Camera Plugin Example',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: MyApp(),
      ),
    );

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _path = null;
  String _thumbPath = null;
  double _headerHeight = 320.0;
  final String _assetImagePath = 'assets/images/ic_no_image.png';
  final String _assetPlayImagePath = 'assets/images/ic_play.png';
  final String _assetLogoImagePath = 'assets/images/ic_flutter_devs_logo.png';

  bool _isVideo = false;
  String _videoName;

  BuildContext context;

  @override
  Widget build(BuildContext context) {
    this.context = context;
    return new Scaffold(
      body: Stack(
        children: <Widget>[
          _path != null
              ? _isVideo ? _getVideoContainer() : _getImageFromFile()
              : _getImageFromAsset(),
          _getContentContainerLogo(),
        ],
      ),
    );
  }

  Widget _getImageFromFile() {
    return Container(
      padding: EdgeInsets.only(bottom: 30.0),
      child: new Container(
          width: double.infinity,
          height: _headerHeight,
          color: Colors.grey,
          child: Stack(
            children: <Widget>[
              new Image.file(
                File(
                  _path,
                ),
                fit: BoxFit.cover,
                width: double.infinity,
                height: _headerHeight,
              ),
              _buildPathWidget(),
            ],
          )),
    );
  }

  Widget _getImageFromAsset() {
    return Container(
      padding: EdgeInsets.only(bottom: 30.0),
      child: new Container(
          width: double.infinity,
          height: _headerHeight,
          color: Colors.grey,
          child: Stack(
            children: <Widget>[
              new Align(
                alignment: Alignment.center,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    new Image.asset(
                      _assetImagePath,
                      width: 48.0,
                      height: 32.0,
                    ),
                    new Container(
                      margin: EdgeInsets.only(top: 8.0),
                      child: Text(
                        'No Preview Available',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 16.0,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              _buildPathWidget(),
            ],
          )),
    );
  }

  Widget _getVideoContainer() {
    return Container(
      padding: EdgeInsets.only(bottom: 30.0),
      child: new Container(
          width: double.infinity,
          height: _headerHeight,
          color: Colors.grey,
          child: Stack(
            children: <Widget>[
              _thumbPath != null
                  ? new Opacity(
                      opacity: 0.5,
                      child: new Image.file(
                        File(
                          _thumbPath,
                        ),
                        fit: BoxFit.cover,
                        width: double.infinity,
                        height: _headerHeight,
                      ),
                    )
                  : new Container(),
              new Align(
                alignment: Alignment.center,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    GestureDetector(
                      onTap: () {
                        Navigator.of(context).push(new MaterialPageRoute(
                            builder: (BuildContext context) =>
                                new VideoPlayerScreen(_path)));
                      },
                      child: new Image.asset(
                        _assetPlayImagePath,
                        width: 72.0,
                        height: 72.0,
                      ),
                    ),
                    new Container(
                      margin: EdgeInsets.only(top: 2.0),
                      child: Text(
                        _videoName != null ? _videoName : '',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 16.0,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              _buildPathWidget()
            ],
          )),
    );
  }

  Widget _buildPathWidget() {
    return _path != null
        ? new Align(
            alignment: Alignment.bottomCenter,
            child: Container(
              width: double.infinity,
              height: 60.0,
              padding: EdgeInsets.all(20.0),
              color: Color.fromRGBO(00, 00, 00, 0.7),
              child: Text(
                'PATH: $_path',
                style: TextStyle(color: Colors.white),
              ),
            ),
          )
        : new Container();
  }

  Widget _getContentContainerLogo() {
    return Container(
        margin: EdgeInsets.only(top: _headerHeight + 5.0),
        alignment: Alignment.center,
        child: Column(
          children: <Widget>[
            new Container(
              color: Colors.red,
              padding: EdgeInsets.all(10.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  InkWell(
                    onTap: _captureImage,
                    child:
                        buildButtonControl(Icons.camera_alt, 'Capture Image'),
                  ),
                  InkWell(
                    onTap: _pickImage,
                    child: buildButtonControl(Icons.image, 'Pick Image'),
                  ),
                  InkWell(
                    onTap: () => _takeVideo(true),
                    child: buildButtonControl(Icons.videocam, 'Capture Video'),
                  ),
                  InkWell(
                    onTap: () => _takeVideo(false),
                    child:
                        buildButtonControl(Icons.video_library, 'Pick Video'),
                  ),
                ],
              ),
            ),
            new Center(
              child: Image.asset(
                _assetLogoImagePath,
                width: 160.0,
                height: 160.0,
                fit: BoxFit.contain,
              ),
            ),
          ],
        ));
  }

  Widget buildButtonControl(IconData icon, String label) {
    return Container(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(icon, color: Colors.white),
          Container(
            margin: const EdgeInsets.only(top: 8.0),
            child: Text(
              label,
              style: TextStyle(
                fontSize: 12.0,
                fontWeight: FontWeight.w400,
                color: Colors.white,
              ),
            ),
          ),
        ],
      ),
    );
  }

  Future _captureImage() async {
    final path = await CameraUtils.captureImage;
    if (path != null) {
      setState(() {
        _path = path;
        _isVideo = false;
      });
    }
  }

  Future _pickImage() async {
    final path = await CameraUtils.pickImage;
    if (path != null) {
      setState(() {
        _path = path;
        _isVideo = false;
      });
    }
  }

  Future _takeVideo(bool isCapture) async {
    setState(() {
      _thumbPath = null;
      _isVideo = false;
      _path = null;
      _videoName = null;
    });

    final path = isCapture
        ? await CameraUtils.captureVideo
        : await CameraUtils.pickVideo;

    if (path != null) {
      setState(() {
        _path = path;
        _isVideo = true;
      });
      Future<String> name = CameraUtils.getFileName(path);
      name.then((fileName) {
        setState(() {
          _videoName = fileName;
          print(fileName);
        });
      });
      Future<String> thumbPath = CameraUtils.getThumbnail(path);
      thumbPath.then((path) {
        setState(() {
          _thumbPath = path;
          print(path);
        });
      });
    }
  }
}

class VideoPlayerScreen extends StatefulWidget {
  String path;

  VideoPlayerScreen(this.path);

  @override
  _VideoPlayerScreenState createState() => _VideoPlayerScreenState(path);
}

class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
  VideoPlayerController playerController;
  VoidCallback listener;
  String path;

  BuildContext context;

  _VideoPlayerScreenState(this.path);

  @override
  @override
  void initState() {
    super.initState();

    listener = () {};
    initializeVideo();
    playerController.play();
  }

  void initializeVideo() {
    playerController = VideoPlayerController.file(File(path))
      ..addListener(listener)
      ..setVolume(1.0)
      ..initialize()
      ..play();
  }

  @override
  void deactivate() {
    if (playerController != null) {
      playerController.setVolume(0.0);
      playerController.removeListener(listener);
    }
    super.deactivate();
  }

  @override
  void dispose() {
    if (playerController != null) playerController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    this.context = context;
    return Scaffold(
        appBar: new AppBar(
          title: Text('Video Player'),
        ),
        body: Stack(fit: StackFit.expand, children: <Widget>[
          new AspectRatio(
              aspectRatio: 9 / 16,
              child: Container(
                child: (playerController != null
                    ? VideoPlayer(
                        playerController,
                      )
                    : Container()),
              )),
        ]));
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  camera_utils: ^0.1.8

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:camera_utils/camera_utils.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
79
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
89
Learn more about scoring.

We analyzed this package on Nov 15, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.0
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.6

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health issues and suggestions

Document public APIs. (-1 points)

10 out of 10 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.7 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8