0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Flutterでカメラアプリ

Posted at

Flutterとは?

Androidでも、IOSでも動くアプリが作れるプラットフォーム,Google製
Dartという、Javaに近い言語で書きます。

以下ソース

import 'dart:async';
import 'dart:io';

import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';



class Capture extends StatefulWidget {
  @override
  _CaptureState createState() => new _CaptureState();
}

class _CaptureState extends State<Capture> {
  CameraController controller;

  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

  @override
  void initState() {
    super.initState();
    availableCameras().then((cameras) {
      CameraDescription rearCamera = cameras.firstWhere(
              (desc) => desc.lensDirection == CameraLensDirection.back, orElse: () => null);
      if (rearCamera == null) {
        return;
      }

      controller = new CameraController(rearCamera, ResolutionPreset.high);
      controller.initialize().then((_) {
        if (!mounted) {
          return;
        }
        setState(() {});
      });
    });
  }

  @override
  void dispose() {
    controller?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {

    Widget preview;
    if (!_isReady()) {
      preview = new Container();
    } else {
      preview = new Container(
          child: new Center(
              child: new AspectRatio(
                  aspectRatio: controller.value.aspectRatio,
                  child: new CameraPreview(controller))
          )
      );
    }
    return new Scaffold(

        backgroundColor: Colors.black,

        key: _scaffoldKey,
        appBar: new AppBar(

          title: const Text("画像解析APP",
              style: TextStyle(

                color: Colors.white,
              )


          ),
          backgroundColor: Colors.black,
        ),
        body:
        new Column(
            children: <Widget>[
              new Expanded(
                child: preview,
              ),
              new SizedBox(
                width: 335.0,
                height: 100.0,

                child: new RaisedButton.icon(
                  icon: Icon(
                    Icons.android,
                    color: Colors.white,
                    size: 70,
                  ),
                  label: Text("画像解析",

                    style: TextStyle(

                      fontSize: 20,
                      color: Colors.white,
                    )
                  ),
                  onPressed: () {_onPressedCaptureIcon();},
                  color: Colors.pinkAccent,
                  textColor: Colors.white,

                ),

              )



            ]

        ),

    );
  }

  bool _isReady() {
    return controller != null && controller.value.isInitialized;
  }

  _onPressedCaptureIcon() {
    _takePicture().then((String filePath) {
      _showSnackBar('Picture saved to $filePath');
      Navigator.of(context).pushNamed("/result");

    }).catchError((e) {
      _showSnackBar(e);
    });

  }

  Future<String> _takePicture() async {
    if (!_isReady()) {
      throw("Camera controller is not initialized.");
    }

    final Directory extDir = await getApplicationDocumentsDirectory();
    final String dirPath = '${extDir.path}/Pictures/flutter_test';
    await new Directory(dirPath).create(recursive: true);
    final String filePath = '$dirPath/${_timestamp()}.jpg';

    if (controller.value.isTakingPicture) {
      // A capture is already pending, do nothing.
      throw("Camera is already pending.");
    }

    await controller.takePicture(filePath);

    return filePath;
  }

  String _timestamp() => new DateTime.now().millisecondsSinceEpoch.toString();

  void _showSnackBar(String text) {
    _scaffoldKey.currentState.showSnackBar(new SnackBar(
      content: new Text(text),
    ));
  }
}
0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?