码迷,mamicode.com
首页 > Windows程序 > 详细

Flutter 调用原生硬件 Api 实现照相机 拍照和相册选择 以及拍照上传

时间:2020-02-27 16:10:19      阅读:1033      评论:0      收藏:0      [点我收藏+]

标签:zhang   file   multipart   调用   dev   build   案例   sync   info   

一、Flutter image_picker 实现相机拍照和相册选择
  https://pub.dev/packages/image_picker
 
二、Flutter 上传图片到服务器
  https://pub.dev/packages/dio
 
上传2.0    dio: 2.1.7
//上传图片
_uploadImage(_imageDir) async{
FormData formData = new FormData.from({
"name": "zhangsna 6666666666",
"age": 20,
"sex":"男",
"file":new UploadFileInfo(_imageDir, "xxx.jpg"),
});
var response = await Dio().post("http://jd.itying.com/imgupload", data: formData);

print(response);
}
案例代码   上传3.0
import ‘dart:io‘;
import ‘package:flutter/material.dart‘;
import ‘package:image_picker/image_picker.dart‘;
import ‘package:dio/dio.dart‘;

class CameraPage extends StatefulWidget{
CameraPage({Key key});
_CameraPage createState() => _CameraPage();
}

class _CameraPage extends State {
var cameraImg;
var albumImg;

// 相机
getCameraImg() async {
var camera = await ImagePicker.pickImage(source: ImageSource.camera, maxWidth: 100);
uploadImg(camera);
setState(() {
cameraImg = camera;
});
}
// 相册
getAlbumImg() async {
var album = await ImagePicker.pickImage(source: ImageSource.gallery, maxWidth: 100);
setState(() {
albumImg = album;
});
}
// 上传图片
uploadImg(File imgUrl) async {
var path = imgUrl.path;
var formData = FormData.fromMap({
"name": "wendux",
"age": 25,
"file": MultipartFile.fromFileSync(path, filename: "upload.img")
});
var response = await Dio().post("https://pub.dev/packages/dio", data: formData);
print(response);
}
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text(‘设备硬件‘)
),
body: ListView(
children: <Widget>[
RaisedButton(child: Text(‘相机 上传图片‘), onPressed: () {
getCameraImg();
}),
cameraImg == null ? SizedBox(height: 0) : Container(
height: 100,
child: Image.file(cameraImg),
),
RaisedButton(child: Text(‘相册‘), onPressed: getAlbumImg),
albumImg == null ? SizedBox(height: 0) : Container(
height: 100,
child: Image.file(albumImg),
)
],
)
);
}
}

Flutter 调用原生硬件 Api 实现照相机 拍照和相册选择 以及拍照上传

标签:zhang   file   multipart   调用   dev   build   案例   sync   info   

原文地址:https://www.cnblogs.com/zhaofeis/p/12372441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!