首页 > 上网技巧 > 电脑小技巧 > Flutter实现拍照功能

Flutter实现拍照功能

时间:2020-05-29 09:10 作者:QQ地带 我要评论

一、进入App直接显示拍照页面
 
import 'package:flutter/material.dart';
import 'package:faceimage/camera.dart';
import 'package:faceimage/renderPicture.dart';
 
void main() => runApp(MyApp());
 
class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primarySwatch: Colors.blue,),
      home: MyHomePage(),
      routes: <String, WidgetBuilder>{
        "camera": (context) => camera(),
        "renderPicture": (context) => renderPicture(),
      },
    );
  }
}
 
class MyHomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return page();
  }
}
 
class page extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return camera();
  }
}
二、在拍照页面,显示相机
 
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:flutter/material.dart';
import 'package:faceimage/renderPicture.dart';
import 'package:camera/camera.dart';
import 'dart:async';
 
class camera extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return page();
  }
}
 
class page extends State<camera> {
  CameraController controller;
  @override
  void initState() {
    super.initState();
    // 初始化相机对象
//    creatController();
  }
 
  @override
  void dispose() {
    super.dispose();
    controller?.dispose();
  }
 
  @override
  Widget build(BuildContext context) {
//    if (controller == null || !controller.value.isInitialized) {
//      return Container();
//    }
    return Scaffold(
//      body: AspectRatio(
//          aspectRatio: controller.value.aspectRatio,
//          child: CameraPreview(controller)
//      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.camera_alt),
        onPressed: () async {
          final path = "";
//          final path = (await getTemporaryDirectory()).path + '${DateTime.now()}.png';
//          await controller.takePicture(path);
          Navigator.push(context, MaterialPageRoute(
            builder: (context) => renderPicture(picture: path),
          ));
        },
      ),
    );
  }
 
  Future creatController() async {
    List<CameraDescription> cameras = await availableCameras();
    controller = CameraController(cameras[1], ResolutionPreset.high);
    controller.initialize().then((_){
      if (!mounted) {
        return;
      }
      setState(() {});
    });
  }
 
//  @override
//  Widget build(BuildContext context) {
//    // TODO: implement build
//    return new Scaffold(
//      backgroundColor: Colors.yellow,
//      body: Container(
//        margin: EdgeInsets.only(top: 60, left: 30),
//        child: Column(
//          children: <Widget>[
//            FlatButton(
//              child: Text("测试"),
//              onPressed: _clickButton,
//            )
//          ],
//        ),
//      )
//    );
//  }
//
//  _clickButton() {
//    Navigator.of(context).push(
//        PageRouteBuilder(opaque: false,
//            pageBuilder: (context, animation, secondaryAnimation) {
//            return renderPicture();
//        })
//    );
//  }
}
三、点击拍照,进入下个页面,显示照片
 
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
 
class renderPicture extends StatefulWidget {
  renderPicture({Key key, this.picture}): super(key: key);
  final String picture;
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return page();
  }
}
 
class page extends State<renderPicture> {
 
  @override
  void initState() {
    super.initState();
    // 上传图片
    uploadImage();
//    getFaceMessage();
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      backgroundColor: Colors.red,
//      body: Image.file(File(widget.picture)),
    );
  }
 
  _clickButton() {
    Navigator.of(context).pop(false);
  }
  // 上传图片
  void uploadImage() async {
//    FormData formData = new FormData.fromMap({
//      "file": await MultipartFile.fromFile(widget.picture, filename: "upload.png"),
//    });
    Dio dio = new Dio();
    dio.options.connectTimeout = 3000 * 10;
    Response response = await dio.get("http://www.xx:8080/face/index?name=upload.png");
//    Response response = await dio.post("http://www.xx:8080/face/index", data: formData);
//    print("11111111111111");
    if (response.statusCode == 200) {
    }
  }
 
  // 获取人脸识别数据
  void getFaceMessage() async {
//    Dio dio11 = new Dio();
//    Response response = await dio11.get("xx:8080/face/index?name=upload.png");
//    if (response.statusCode == 200) {
//      print("3333333333333");
//    }
  }
}
 
/*
* import 'dart:convert';
import 'dart:ui';
import 'package:flutter/material.dart';
{1}
class homePage extends StatefulWidget {
  @override
  State createState() {
    // 
 

标签: Flutter
顶一下
(0)
0%
踩一下
(0)
0%

Google提供的广告