微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何创建相机对焦系统?

如何解决如何创建相机对焦系统?

你好,我目前正在做一个相机项目。我想添加一个功能,比如当我触摸相机屏幕时,它会聚焦在那里。就像我们在 android 或 ios 设备上的认相机一样。我使用了 Flutter dev 的认相机包。

这是示例:

import 'dart:async';
import 'dart:io';
import 'package:Flutter/material.dart';
import 'package:Flutter_better_camera/camera.dart';
import 'package:path/path.dart' show join;
import 'package:path_provider/path_provider.dart';

Future<void> main() async {
  // Ensure that plugin services are initialized so that `availableCameras()`
  // can be called before `runApp()`
  WidgetsFlutterBinding.ensureInitialized();

  // Obtain a list of the available cameras on the device.
  final cameras = await availableCameras();

  // Get a specific camera from the list of available cameras.
  final firstCamera = cameras.first;

  runApp(
    MaterialApp(
      theme: ThemeData.dark(),home: TakePictureScreen(
        // Pass the appropriate camera to the TakePictureScreen widget.
        camera: firstCamera,),);
}

// A screen that allows users to take a picture using a given camera.
class TakePictureScreen extends StatefulWidget {
  final CameraDescription camera;

  const TakePictureScreen({
    Key key,@required this.camera,}) : super(key: key);

  @override
  TakePictureScreenState createState() => TakePictureScreenState();
}

class TakePictureScreenState extends State<TakePictureScreen> {
  CameraController _controller;
  Future<void> _initializeControllerFuture;

  @override
  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.veryHigh,);

    // 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(
      appBar: AppBar(title: Text('Take a picture')),// Wait until the controller is initialized before displaying the
      // camera preview. Use a FutureBuilder to display a loading spinner
      // until the controller has finished initializing.
      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;

            // Construct the path where the image should be saved using the
            // pattern package.
            final path = join(
              // Store the picture in the temp directory.
              // Find the temp directory using the `path_provider` plugin.
              (await getTemporaryDirectory()).path,'${DateTime.Now()}.png',);

            // Attempt to take a picture and log where it's been saved.
            await _controller.takePicture(path);

            // If the picture was taken,display it on a new screen.
            Navigator.push(
              context,MaterialPageRoute(
                builder: (context) => displayPictureScreen(imagePath: path),);
          } catch (e) {
            // If an error occurs,log the error to the console.
            print(e);
          }
        },);
  }
}

// A widget that displays the picture taken by the user.
class displayPictureScreen extends StatelessWidget {
  final String imagePath;

  const displayPictureScreen({Key key,this.imagePath}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('display the Picture')),// The image is stored as a file on the device. Use the `Image.file`
      // constructor with the given path to display the image.
      body: Image.file(File(imagePath)),);
  }
}

我只是用它来构建这种功能。如果有人知道请随时帮助我:) 谢谢

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。