为相机抖动添加焦点



我有一个在flutter上写的应用程序,我在其中拍摄了一张照片,将此照片转换为base64,然后发送。但我有一个问题,我不能把注意力集中在相机上。所以我是个新手,我自己解决不了这个问题。我将感谢你的帮助。我的目标是当你点击屏幕时,把注意力集中在那个地方。这是我的代码:

import 'dart:async';
import 'dart:io';
import 'dart:convert';
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';

class PicturePreview extends StatefulWidget {
final CameraDescription camera;
const PicturePreview(this.camera, {Key? key}) : super(key: key);
@override
_PicturePreviewState createState() => _PicturePreviewState();
}
class _PicturePreviewState extends State<PicturePreview> {
late CameraController _controller;
Future<void>? _initializeControllerFuture;
@override
late String _imageB64;
late File  _image;
void initState() {
super.initState();
// To display the current output from the Camera,
// create a CameraController.
_controller = CameraController(
// Get a specific camera from the list of available cameras.
widget.camera,
// Define the resolution to use.
ResolutionPreset.low,
);
// Next, initialize the controller. This returns a Future.
_initializeControllerFuture = _controller.initialize();
}
@override
void dispose() {
// Dispose of the controller when the widget is disposed.
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder<void>(
future: _initializeControllerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
// If the Future is complete, display the preview.
return CameraPreview(_controller);
} else {
// Otherwise, display a loading indicator.
return Center(child: CircularProgressIndicator());
}
},
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.camera_alt),
// Provide an onPressed callback.
onPressed: () async {
// Take the Picture in a try / catch block. If anything goes wrong,
// catch the error.
try {
// Ensure that the camera is initialized.
await _initializeControllerFuture;
// Attempt to take a picture and get the file `image`
// where it was saved.
final image = await _controller.takePicture();
_image = File(image.path);
List<int> imageBytes = _image.readAsBytesSync();
_imageB64 = base64Encode(imageBytes);
Navigator.pop(context, _imageB64);
} catch (e) {
// If an error occurs, log the error to the console.
print(e);
}
},
),
);
}
}

相机包中没有对焦功能。您应该自己实现它,或者尝试adv_camera包。

这里有一个简单的例子:

import 'package:adv_camera/adv_camera.dart';
import 'package:flutter/material.dart';
void main() {
String id = DateTime.now().toIso8601String();
runApp(MaterialApp(home: MyApp(id: id)));
}
class MyApp extends StatefulWidget {
final String id;
const MyApp({Key? key, required this.id}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home'),
),
body: Center(child: Text('Press Floating Button to access camera')),
floatingActionButton: FloatingActionButton(
heroTag: "test3",
child: Icon(Icons.camera),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) {
String id = DateTime.now().toIso8601String();
return CameraApp(id: id);
},
),
);
},
),
);
}
}
class CameraApp extends StatefulWidget {
final String id;
const CameraApp({Key? key, required this.id}) : super(key: key);
@override
_CameraAppState createState() => _CameraAppState();
}
class _CameraAppState extends State<CameraApp> {
List<String> pictureSizes = <String>[];
String? imagePath;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('AdvCamera Example'),
),
body: SafeArea(
child: AdvCamera(
initialCameraType: CameraType.rear,
onCameraCreated: _onCameraCreated,
onImageCaptured: (String path) {
if (this.mounted)
setState(() {
imagePath = path;
});
},
cameraPreviewRatio: CameraPreviewRatio.r16_9,
focusRectColor: Colors.purple,
focusRectSize: 200,
),
),
floatingActionButton: FloatingActionButton(
heroTag: "capture",
child: Icon(Icons.camera),
onPressed: () {
cameraController!.captureImage();
},
),
);
}
AdvCameraController? cameraController;
_onCameraCreated(AdvCameraController controller) {
this.cameraController = controller;
this.cameraController!.getPictureSizes().then((pictureSizes) {
setState(() {
this.pictureSizes = pictureSizes ?? <String>[];
});
});
}
}

Camera是一个bareback插件,其中大多数相机功能都是自行实现的。他们为开发人员公开了setFocusPointsetFlashMode等所有基本功能。

关于焦点实现部分,您可以参考这篇StackOverflow文章。

然而,对于您的用例,我不建议使用相机或adv_camera包。如果你只是想用安卓系统提供的具有所有功能的默认相机拍摄照片,最好使用Image Picker软件包。这会让你的生活变得轻松。

最新更新