Android 如何通过相机单击图像并在“颤振”中设置为“图像视图”
我正在开发一个颤振应用程序,其中有一个包含一些文本字段的屏幕和一个imageviewAndroid 如何通过相机单击图像并在“颤振”中设置为“图像视图”,android,flutter,Android,Flutter,我正在开发一个颤振应用程序,其中有一个包含一些文本字段的屏幕和一个imageview 我必须通过相机点击一个图像并显示到imageview中 我已经输入了摄像头的基本代码,但无法通过单击按钮打开摄像头 我使用了下面的代码来实现相同的 class Survey extends StatefulWidget { @override State<StatefulWidget> createState() { // TODO: implement createStat
我必须通过相机点击一个图像并显示到imageview中 我已经输入了摄像头的基本代码,但无法通过单击按钮打开摄像头
我使用了下面的代码来实现相同的
class Survey extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return SurveyState();
}
}
class SurveyState extends State<Survey> {
@override
Widget build(BuildContext context) {
// TODO: implement build
return new MaterialApp(
debugShowCheckedModeBanner: false,
home: new Scaffold(
appBar: AppBar(
actions: <Widget>[
new Padding(padding: new EdgeInsets.fromLTRB(10, 0, 0, 0)),
RaisedButton.icon(
textColor: Colors.white,
onPressed: () {},
label: Text("Fetch data"),
color: Colors.blue,
icon: new Image.asset(
'images/fetch.png',
width: 20,
height: 20,
),
),
Spacer(),
RaisedButton.icon(
textColor: Colors.white,
onPressed: () {},
label: Text("Sync"),
color: Colors.blue,
icon: new Image.asset(
'images/sync.png',
width: 20,
height: 20,
),
),
new Padding(padding: new EdgeInsets.fromLTRB(0, 0, 10, 0)),
],
),
body: new SurveyForm(),
));
}
}
class SurveyForm extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return new SurveyFormState();
}
}
class SurveyFormState extends State<SurveyForm> {
TextEditingController feederName = new TextEditingController();
TextEditingController poleType = new TextEditingController();
CameraController controller;
double _animatedHeight = 0.0;String _errorMsg = '';
// Add two variables to the state class to store the CameraController and
// the Future.
Future<void> _initializeControllerFuture;
String imagePath;
var cameras;
var selectedCameraIdx;
@override
void initState() {
// TODO: implement initState
super.initState();
// To display the current output from the camera,
// create a CameraController.
initCamera();
}
@override
void dispose() {
// Dispose of the controller when the widget is disposed.
controller.dispose();
super.dispose();
}
void initCamera() async {
final camerasList = await availableCameras();
controller = new CameraController(camerasList[0], ResolutionPreset.medium);
await controller.initialize();
if (mounted) {
setState(() {});
}
}
@override
Widget build(BuildContext context) {
// TODO: implement build
return new Container(
margin: const EdgeInsets.all(20.0),
child: new Column(
children: <Widget>[
new Padding(padding: new EdgeInsets.fromLTRB(0, 20, 0, 0)),
SizedBox(
width: 200.0,
height: 45.0,
child: RaisedButton.icon(
textColor: Colors.white,
onPressed: () {},
label: Text("New pole survey"),
color: Colors.blue,
icon: new Image.asset(
'images/sync.png',
width: 20,
height: 20,
),
)),
new Padding(padding: new EdgeInsets.fromLTRB(0, 10, 0, 0)),
new TextField(
controller: feederName,
obscureText: true,
decoration: new InputDecoration(
hintText: "",
border:
OutlineInputBorder(borderRadius: BorderRadius.circular(5.0)),
),
),
new Padding(padding: new EdgeInsets.fromLTRB(0, 10, 0, 0)),
new TextField(
controller: poleType,
decoration: new InputDecoration(
hintText: "",
border:
OutlineInputBorder(borderRadius: BorderRadius.circular(5.0)),
),
),
new Padding(padding: new EdgeInsets.fromLTRB(0, 10, 0, 0)),
new TextField(
controller: poleType,
decoration: new InputDecoration(
hintText: "",
border:
OutlineInputBorder(borderRadius: BorderRadius.circular(5.0)),
),
),
new Padding(padding: new EdgeInsets.fromLTRB(0, 10, 0, 0)),
new Container(
width: double.infinity,
height: 150.0,
// here I want to display the clicked image
),
new Padding(padding: new EdgeInsets.fromLTRB(0, 10, 0, 0)),
SizedBox(
width: 200.0,
height: 45.0,
child:
new RaisedButton.icon(
textColor: Colors.white,
onPressed: () async {
takePicture();
},
label: Text("Click picture"),
color: Colors.blue,
icon: new Image.asset(
'images/sync.png',
width: 20,
height: 20,
),
)),
],
),
);
}
/// Display Camera preview.
Widget _cameraPreviewWidget() {
if (controller == null || !controller.value.isInitialized) {
return const Text(
'Loading',
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
fontWeight: FontWeight.w900,
),
);
}
return AspectRatio(
aspectRatio: controller.value.aspectRatio,
child: CameraPreview(controller),
);
}
void showInSnackBar(String message) {
setState(() {
_animatedHeight = 30.0;
_errorMsg = message;
});
Future<void>.delayed(const Duration(seconds: 1), _hideErrorMsg);
}
void _hideErrorMsg() {
setState(() {
_animatedHeight = 0.0;
_errorMsg = '';
});
}
Future<String> takePicture() async {
if (!controller.value.isInitialized) {
showInSnackBar('Error: select a camera first.');
return null;
}
final String filePath = join(
// Store the picture in the temp directory.
// Find the temp directory using the `path_provider` plugin.
(await getTemporaryDirectory()).path,
'${DateTime.now()}.png',
);
if (controller.value.isTakingPicture) {
// A capture is already pending, do nothing.
return null;
}
try {
await controller.takePicture(filePath);
} on CameraException catch (e) {
print('Exception -> $e');
return null;
}
setState(() {
});
return filePath;
}
}
类调查扩展了StatefulWidget{
@凌驾
状态createState(){
//TODO:实现createState
返回SurveyState();
}
}
类SurveyState扩展状态{
@凌驾
小部件构建(构建上下文){
//TODO:实现构建
返回新材料PP(
debugShowCheckedModeBanner:false,
家:新脚手架(
appBar:appBar(
行动:[
新填充(填充:从LTRB(10,0,0,0)开始的新边集),
RaisedButton.icon(
textColor:Colors.white,
按下:(){},
标签:文本(“获取数据”),
颜色:颜色,蓝色,
图标:new Image.asset(
'images/fetch.png',
宽度:20,
身高:20,
),
),
垫片(),
RaisedButton.icon(
textColor:Colors.white,
按下:(){},
标签:文本(“同步”),
颜色:颜色,蓝色,
图标:new Image.asset(
“images/sync.png”,
宽度:20,
身高:20,
),
),
新填充(填充:从LTRB(0,0,10,0)开始的新边插入集),
],
),
正文:新的SurveyForm(),
));
}
}
类SurveyForm扩展StatefulWidget{
@凌驾
状态createState(){
//TODO:实现createState
返回新的SurveyFormState();
}
}
类SurveyFormState扩展状态{
TextEditingController feederName=新的TextEditingController();
TextEditingController poleType=新的TextEditingController();
摄像机控制器;
double _animatedHeight=0.0;String _errorMsg='';
//将两个变量添加到状态类以存储CameraController和
//未来。
未来(初始化控制未来);
字符串图像路径;
var摄像机;
var选择的cameraidx;
@凌驾
void initState(){
//TODO:实现initState
super.initState();
//要显示摄像机的电流输出,
//创建一个CameraController。
initCamera();
}
@凌驾
无效处置(){
//处置小部件时处置控制器。
controller.dispose();
super.dispose();
}
void initCamera()异步{
最终摄像机列表=等待可用摄像机();
控制器=新的CameraController(camerasList[0],ResolutionPreset.medium);
等待控制器初始化();
如果(已安装){
setState((){});
}
}
@凌驾
小部件构建(构建上下文){
//TODO:实现构建
退回新货柜(
边距:所有常数边集(20.0),
子:新列(
儿童:[
新填充(填充:来自LTRB(0,20,0,0)的新边集),
大小盒子(
宽度:200.0,
身高:45.0,
子:RaisedButton.icon(
textColor:Colors.white,
按下:(){},
标签:文本(“新极点测量”),
颜色:颜色,蓝色,
图标:new Image.asset(
“images/sync.png”,
宽度:20,
身高:20,
),
)),
新填充(填充:从LTRB(0,10,0,0)开始的新边集),
新文本字段(
控制器:feederName,
蒙昧文字:对,
装饰:新的输入装饰(
hintText:“,
边界:
OutlineInputBorder(borderRadius:borderRadius.circular(5.0)),
),
),
新填充(填充:从LTRB(0,10,0,0)开始的新边集),
新文本字段(
控制器:poleType,
装饰:新的输入装饰(
hintText:“,
边界:
OutlineInputBorder(borderRadius:borderRadius.circular(5.0)),
),
),
新填充(填充:从LTRB(0,10,0,0)开始的新边集),
新文本字段(
控制器:poleType,
装饰:新的输入装饰(
hintText:“,
边界:
OutlineInputBorder(borderRadius:borderRadius.circular(5.0)),
),
),
新填充(填充:从LTRB(0,10,0,0)开始的新边集),
新容器(
宽度:double.infinity,
高度:150.0,
//我想在这里显示单击的图像
),
新填充(填充:从LTRB(0,10,0,0)开始的新边集),
大小盒子(
宽度:200.0,
身高:45.0,
儿童:
新建RaisedButton.icon(
textColor:Colors.white,
onPressed:()异步{
拍摄照片();
},
标签:文本(“点击图片”),
颜色:颜色,蓝色,
图标:new Image.asset(
“images/sync.png”,
宽度:20,
身高:20,
),
)),
],
),
);
}
///显示相机预览。
小部件\u cameraPreviewWidget(){
if(controller==null | |!controller.value.isInitialized){
返回常量文本(
“加载”,
样式:TextStyle(
颜色:颜色,白色,
字体大小:20.0,
fontWeight:fontWeight.w900,
),
);
}
返回AspectRatio(
aspectRatio:controller.value.aspectRatio,
子项:CameraPreview(控制器),
);
}
真空显示纳克巴
CameraController _controller;
Future<void> _initializeControllerFuture;
isCameraReady = false;
showCapturedPhoto = false;
var ImagePath;
@override
void initState() {
super.initState();
_initializeCamera();
}
Future<void> _initializeCamera() async {
final cameras = await availableCameras();
final firstCamera = cameras.first;
_controller = CameraController(firstCamera,ResolutionPreset.high);
_initializeControllerFuture = _controller.initialize();
if (!mounted) {
return;
}
setState(() {
isCameraReady = true;
});
}
final size = MediaQuery.of(context).size;
final deviceRatio = size.width / size.height
FutureBuilder<void>(
future: _initializeControllerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
// If the Future is complete, display the preview.
return Transform.scale(
scale: _controller.value.aspectRatio / deviceRatio,
child: Center(
child: AspectRatio(
aspectRatio: _controller.value.aspectRatio,
child: CameraPreview(_controller), //cameraPreview
),
));
} else {
return Center(
child:
CircularProgressIndicator()); // Otherwise, display a loading indicator.
}
},
),
void onCaptureButtonPressed() async { //on camera button press
try {
final path = join(
(await getTemporaryDirectory()).path, //Temporary path
'$pageStatus${DateTime.now()}.png',
);
ImagePath = path;
await _controller.takePicture(path); //take photo
setState(() {
showCapturedPhoto = true;
});
} catch (e) {
print(e);
}
}
Center(child: Image.file(File(ImagePath)));
@override
void dispose() {
// TODO: implement dispose
_controller?.dispose();
super.dispose();
}
showCapturedPhoto ? Container(
width: double.infinity,
height: 150.0,
child: Image.file(File(ImagePath)))
: Container(),