因此,我正在创建一款使用Ffltter的应用程序,其中我需要手机摄像头.为了做到这一点,我将包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}');
}
}
});
}
}
以下是代码的输出:
点击扫描按钮(如上图所示)后,应用程序会在Toast中传回图片的路径.路径格式如下/data/user/0/com.examlple.prototype/cache/(photoname).jpg
.
但无论我做了什么,我都无法在我的Internal Storage
部手机中找到这个文件夹.我甚至判断了Android/data/
,但它不在那里.那么,我可以做些什么来判断它是否真的保存了图片,以及之后如何查看它.