Flutter 调用原生硬件 Api 实现照相机 拍照和相册选择 以及拍照上传
2021-03-18 02:26
标签:zhang file multipart 调用 dev build 案例 sync info Flutter 调用原生硬件 Api 实现照相机 拍照和相册选择 以及拍照上传 标签:zhang file multipart 调用 dev build 案例 sync info 原文地址:https://www.cnblogs.com/zhaofeis/p/12372441.html//上传图片
_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);
}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:
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 实现照相机 拍照和相册选择 以及拍照上传
文章链接:http://soscw.com/index.php/essay/65587.html