因此,我正在创建一个应用程序使用flutter在其中我需要的手机摄像头.为了做到这一点,我已经添加了包camera 0.10.3+2
到我的项目.以下是代码的屏幕,我有一些麻烦
// ignore_for_file: file_names
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:prototype/constants.dart';
import 'package:prototype/main.dart' as main;
import 'package:prototype/tempPhoto.dart';
class MainScreen extends StatefulWidget {
const MainScreen({Key? key}) : super(key: key);
static String id ="MainScreen";
@override
State<MainScreen> createState() => _MainScreenState();
}
class _MainScreenState extends State<MainScreen> {
XFile? imageFile;
late CameraController controller;
@override
void initState() {
// TODO: implement initState
super.initState();
controller = CameraController(main.cameras[0], ResolutionPreset.max);
controller.initialize().then((_) {
if (!mounted) {
return;
}
setState(() {});
}).catchError((Object e) {
if (e is CameraException) {
switch (e.code) {
case 'CameraAccessDenied':
// Handle access errors here.
break;
default:
// Handle other errors here.
break;
}
}
});
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: [
SizedBox(
width: double.infinity,
height: 80,
child: Container(
decoration: kcustomContainer,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
TextButton(
onPressed: (){},
style: kButtonStyleAppBar,
child: const Padding(
padding: EdgeInsets.symmetric(horizontal: 5,vertical: 10),
child: Text("Filters",style: kButtonTextStyleAppbar),
),
),
SizedBox(
width: 150.0,
child: TextField(
decoration: const InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: kCPGreenLight,width: 2.0),
borderRadius: BorderRadius.all(Radius.circular(10.0)),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: kCPGteenDark, width: 3.0),
borderRadius: BorderRadius.all(Radius.circular(10.0)),
),
filled: true,
fillColor: kCPWhite,
hintText: "Product name",
prefixIcon: Icon(Icons.search),
iconColor: kCPGreenMid,
),
onSubmitted: (value){
},
),
),
TextButton(
onPressed: (){},
style: kButtonStyleAppBar,
child: const Padding(
padding: EdgeInsets.symmetric(horizontal: 5,vertical: 10),
child: Text("History",style: kButtonTextStyleAppbar),
),
),
InkWell(
child: const Icon(
Icons.more_vert,
color:kCPWhite,
size: 30,),
onTap: (){
},
)
],
),
),
),
if (!controller.value.isInitialized) const Text("Camera Error")
else Expanded(
child: SizedBox(
width:double.infinity,
child: CameraPreview(controller)
)
),
SizedBox(
height: 80,
width: double.infinity,
child: Container(
decoration: kcustomContainer,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
TextButton(
onPressed: (){},
style: kButtonStyleAppBar,
child: const Padding(
padding: EdgeInsets.symmetric(horizontal: 5,vertical: 10),
child: Text("Mes Promos",style: kButtonTextStyleAppbar,),
),
),
TextButton(
onPressed: (){
if(controller.value.isInitialized) onTakePictureButtonPressed();
},
style: kButtonStyleAppBar.copyWith(shape: MaterialStateProperty.all<CircleBorder>(
const CircleBorder(
side: BorderSide(width: 0,color: Color.fromRGBO(0, 0, 0, 0))
)
),),
child: Padding(
padding: const EdgeInsets.all(18.0),
child: Text("Scan",style: kButtonTextStyleAppbar.copyWith(fontSize: 20),),
),
),
TextButton(
onPressed: (){},
style: kButtonStyleAppBar,
child: const Padding(
padding: EdgeInsets.symmetric(horizontal: 5,vertical: 10),
child: Text("Mes Recommandation",style: kButtonTextStyleAppbar,),
),
),
],
),
),
)
],
),
)
);
}
Future<XFile?> takePicture() async {
final CameraController cameraController = controller;
if (!cameraController.value.isInitialized) {
print('Error: select a camera first.');
return null;
}
if (cameraController.value.isTakingPicture) {
// A capture is already pending, do nothing.
return null;
}
try {
print("taking photo !!!!!");
final XFile file = await cameraController.takePicture();
return file;
} on CameraException catch (e) {
_showCameraException(e);
return null;
}
}
void _logError(String code, String? message) {
// ignore: avoid_print
print('Error: $code${message == null ? '' : '\nError Message: $message'}');
}
void _showCameraException(CameraException e) {
_logError(e.code, e.description);
showInSnackBar('Error: ${e.code}\n${e.description}');
}
void showInSnackBar(String message) {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text(message)));
}
void onTakePictureButtonPressed() async {
await takePicture().then((XFile? file) async {
if (mounted) {
setState(() {
imageFile = file;
});
if (file != null) {
Navigator.push(context, MaterialPageRoute(builder: (context)=>TempPhoto(path: file.path)));
showInSnackBar('Picture saved to ${file.path}');
}
}
});
}
}
下面是代码的输出:
Output
点击扫描按钮后(在上面的照片中),应用程序将在吐司中发送回图像的路径。路径格式如下/data/user/0/com.examlple.prototype/cache/(photoname).jpg
。
但无论我做了什么,我找不到这个文件夹在我的Internal Storage
我的手机.我甚至检查了Android/data/
,它不在那里.所以我可以做什么来检查,如果它实际上保存了图片,我怎么才能查看它之后.
1条答案
按热度按时间snz8szmq1#
这是因为插件将文件保存在应用程序内存缓存中。您可以通过在手机设置中查看应用程序大小来验证这一点。我个人使用https://pub.dev/packages/gallery_saver(gallery_saver)将相机文件保存到gallery/photos,这很方便。