Flutter拍照 连拍 多拍 调取系统相机或者摄像头

Flutter拍照 连拍 多拍 调取系统相机或者摄像头,第1张

插件
  # 相机
  camera:
  # 图片存储需要用到
  path:

IOS

  • 相机插件适用于任何版本的 iOS,但其功能需要 iOS 10 或更高版本。如果为 iOS 9 编译,请确保在使用任何相机插件功能之前以编程方式检查设备上运行的 iOS 版本。例如,device_info_plus插件可用于检查 iOS 版本。

将两行添加到ios/Runner/Info.plist:

如果编辑Info.plist为文本,请添加:

	NSCameraUsageDescription
	为了给您提供更好的服务,需要获取您的手机相机
	NSPhotoLibraryAddUsageDescription
	需要您的相册权限
	NSPhotoLibraryUsageDescription
	需要您的相册权限

安卓
android/app/build.gradle在您的文件中将最低 Android sdk 版本更改为 21(或更高) 。

minSdkVersion 21

配置项

    
    
    
    

具体实现:

  CameraDescription _camera;//相机
  CameraController _cameraController;//相机视图处理
  Future _initializeControllerFuture;

final cameras = await availableCameras();
  if (cameras.isNotEmpty) {
    // 初始化
    _camera = cameras.first;
    _cameraController =
        CameraController(_camera, ResolutionPreset.medium);
    _initializeControllerFuture =
        _cameraController.initialize(); //初始化
  } else {
	pint("相机获取失败,请检查相机权限");
  }

相机呈现:

Container(
    color: Colors.black,
    child: Column(
      children: [
        Expanded(
          child: FutureBuilder(
            future: _initializeControllerFuture,
            builder: (futureBuilderContent, snapshot) {
            //根据当前相机拍摄处理显示
              if (snapshot.connectionState == ConnectionState.done) {
                return (_cameraController != null)
                    ? CameraPreview(_cameraController) //预览图
                    : Container();
              } else {
                return const Center(
                  child: CircularProgressIndicator(),
                );
              }
            },
          ),
        ),
        Container(
          margin: const EdgeInsets.only(top: 12, bottom: 12),
          child: MaterialButton(
            color: Colors.black,
            child: Image.asset(
              'images/custom_camer.png',//相机拍摄按钮图片
              width: 72,
              height: 72,
            ),
            onPressed: () async {
              try {
              	//拍摄
                await _initializeControllerFuture;
                final dateTime = DateTime.now();//以时间为图片名称处理
                // ignore: unused_local_variable
                final path = join(
                    (await getApplicationDocumentsDirectory()).path,
                    '${dateTime.millisecondsSinceEpoch}.png');
                final result = await _cameraController.takePicture();
                if (result != null) {
                 print("得到拍摄的图片");
                }
              } catch (err) {
                print(err);
              }
            },
          ),
        ),
      ],
    ),
  );```

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/719777.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存