当前位置: 首页 > news >正文

淄博网站开发公司外链网站大全

淄博网站开发公司,外链网站大全,龙岩网站设计 信任推商吧做词,福州做网站的公司多少钱flutter开发实战-自定义相机camera功能。 Flutter 本质上只是一个 UI 框架,运行在宿主平台之上,Flutter 本身是无法提供一些系统能力,比如使用蓝牙、相机、GPS等,因此要在 Flutter 中调用这些能力就必须和原生平台进行通信。 实现…

flutter开发实战-自定义相机camera功能。

Flutter 本质上只是一个 UI 框架,运行在宿主平台之上,Flutter 本身是无法提供一些系统能力,比如使用蓝牙、相机、GPS等,因此要在 Flutter 中调用这些能力就必须和原生平台进行通信。
实现相机功能,我们使用的是camera插件。

一、引入camera插件

在pubspec.yaml引入插件

  # Camera相机拍照等camera: ^0.10.5+2image: ^4.0.17

二、实现相机拍照功能

在iOS的info.plist文件增加相机、麦克风权限

<key>NSCameraUsageDescription</key>
<string>your usage description here</string>
<key>NSMicrophoneUsageDescription</key>
<string>your usage description here</string>

在Android的android/app/build.gradle调整

minSdkVersion 21

处理详情权限获取,以下是权限错误的类型

  • CameraAccessDenied:当用户拒绝相机访问权限时抛出。
  • CameraAccessDeniedWithoutPrompt:仅限iOS。当用户先前拒绝该权限时抛出。iOS不允许再次提示警报对话框。用户必须进入“设置”>“隐私”>“相机”才能访问相机。
  • CameraAccessRestricted:仅限iOS。当摄像头访问受到限制且用户无法授予权限(家长控制)时抛出。
  • AudioAccessDenied:当用户拒绝音频访问权限时抛出。
  • AudioAccessDeniedWithoutPrompt:目前仅限iOS。当用户先前拒绝该权限时抛出。iOS不允许再次提示警报对话框。用户必须转到“设置”>“隐私”>“麦克风”才能启用音频访问。
  • AudioAccessRestricted:目前仅限iOS。当音频访问受到限制并且用户无法授予权限(家长控制)时抛出。

2.1 实现相机

import 'package:camera/camera.dart';
import 'package:flutter/material.dart';late List<CameraDescription> _cameras;Future<void> main() async {WidgetsFlutterBinding.ensureInitialized();_cameras = await availableCameras();runApp(const CameraApp());
}/// CameraApp is the Main Application.
class CameraApp extends StatefulWidget {/// Default Constructorconst CameraApp({super.key});State<CameraApp> createState() => _CameraAppState();
}class _CameraAppState extends State<CameraApp> {late CameraController controller;void initState() {super.initState();controller = CameraController(_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;}}});}void dispose() {controller.dispose();super.dispose();}Widget build(BuildContext context) {if (!controller.value.isInitialized) {return Container();}return MaterialApp(home: CameraPreview(controller),);}
}

2.2 生命周期更改时处理相机

通过重写didChangeAppLifecycleState方法来处理生命周期更改,如下所示:

使用WidgetsBindingObserver

void didChangeAppLifecycleState(AppLifecycleState state) {final CameraController? cameraController = controller;// App state changed before we got the chance to initialize.if (cameraController == null || !cameraController.value.isInitialized) {return;}if (state == AppLifecycleState.inactive) {cameraController.dispose();} else if (state == AppLifecycleState.resumed) {_initializeCameraController(cameraController.description);}}

2.3 CameraPreview预览时图像变形问题更改

这个需要使用到具体Container的宽高和aspectRatio做处理

    if (controller != null && controller!.value.isInitialized) {// 设备像素比double deviceRatio = 1.0;if (widget.width! > widget.height!) {deviceRatio = widget.width! / widget.height!;} else {deviceRatio = widget.height! / widget.width!;}// 相机纵横比final double aspectRatio = controller!.value.aspectRatio;double scale = aspectRatio / deviceRatio;return Container(key: _cameraContainerGlobalKey,width: widget.width,height: widget.height,clipBehavior: Clip.hardEdge,decoration: BoxDecoration(color: Colors.transparent,borderRadius: BorderRadius.circular(20.r),),child: Stack(alignment: Alignment.center,clipBehavior: Clip.hardEdge,children: [Container(width: widget.width,height: widget.height,clipBehavior: Clip.hardEdge,decoration: BoxDecoration(color: Colors.transparent,),child: RepaintBoundary(key: _cameraViewGlobalKey,child: Transform.scale(scale: scale * aspectRatio,child: AspectRatio(aspectRatio: aspectRatio,child: Center(child: CameraPreview(controller!,),),),),),),),);
}

2.4 实现拍照功能

使用拍照功能,需要用到CameraController

Future<XFile?> takePicture() async {final CameraController? cameraController = controller;if (cameraController == null || !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 {final XFile file = await cameraController.takePicture();print("takePicture file:${file.toString()}");return file;} on CameraException catch (e) {print("takePicture exception:${e.toString()}");return null;}}

获取到File,可以得到图片的path。

2.5 暂停及恢复预览

暂停及恢复预览

if (!cameraController.value.isPreviewPaused) {await cameraController.pausePreview();}
if (cameraController.value.isPreviewPaused) {await cameraController.resumePreview();}

2.6 获得图片后裁剪

裁剪图片这里使用的是插件:image

引入插件

image: ^4.0.17

实现裁剪

if (file != null) {// 保存到相册// await SaveToAlbumUtil.saveLocalImage(file.path);RenderBox renderBox = _cameraContainerGlobalKey.currentContext!.findRenderObject() as RenderBox;// offset.dx , offset.dy 就是控件的左上角坐标Offset offset = renderBox.localToGlobal(Offset.zero);//获取sizeSize size = renderBox.size;// 创建文件pathString imageDir = await PathUtil.createDirectory("local_images");String imagePath = '$imageDir/${TimeUtil.currentTimeMillis()}.png';// 获取当前设备的像素比double dpr = ui.window.devicePixelRatio;print("devicePixelRatio:${dpr}");print("offset:(${offset.dx},${offset.dy})--size:(${size.width},${size.height})");File? targetFile = await ImageUtil.cropImage(file.path, imagePath,x: (dpr * offset.dx).floor(),y: (dpr * offset.dy).floor(),width: (dpr * size.width).ceil(),height: (dpr * size.height).ceil());if (targetFile != null) {await SaveToAlbumUtil.saveLocalImage(targetFile.path);}
}

裁剪结果如图所示

在这里插入图片描述
在这里插入图片描述

三、小结

flutter开发实战-自定义相机camera功能,拍照及图片裁剪功能.

学习记录,每天不停进步。


文章转载自:
http://dinncohurl.stkw.cn
http://dinncointomb.stkw.cn
http://dinncodipster.stkw.cn
http://dinncocamorrism.stkw.cn
http://dinncoblackish.stkw.cn
http://dinncoapophysis.stkw.cn
http://dinncoharvesting.stkw.cn
http://dinncotapeline.stkw.cn
http://dinncowahabi.stkw.cn
http://dinncocosmopolite.stkw.cn
http://dinncosemeiotic.stkw.cn
http://dinncoemigrate.stkw.cn
http://dinncoarmoire.stkw.cn
http://dinncoredrive.stkw.cn
http://dinncobackbencher.stkw.cn
http://dinncodisembark.stkw.cn
http://dinncorecommission.stkw.cn
http://dinncodecarboxylation.stkw.cn
http://dinncocabined.stkw.cn
http://dinncoreconveyance.stkw.cn
http://dinncolawnmower.stkw.cn
http://dinncoradioteletype.stkw.cn
http://dinncomephitical.stkw.cn
http://dinncoinstructor.stkw.cn
http://dinncoaudiotactile.stkw.cn
http://dinncohumiliatory.stkw.cn
http://dinncobergsonian.stkw.cn
http://dinncoquarterfinalist.stkw.cn
http://dinncoquellenforschung.stkw.cn
http://dinncoepitaxial.stkw.cn
http://dinncoyugoslavian.stkw.cn
http://dinncobfc.stkw.cn
http://dinncometeorograph.stkw.cn
http://dinncofenitrothion.stkw.cn
http://dinncoyangon.stkw.cn
http://dinncosulphuration.stkw.cn
http://dinncomenad.stkw.cn
http://dinncostriation.stkw.cn
http://dinncounderwork.stkw.cn
http://dinncocontradictious.stkw.cn
http://dinncowriggly.stkw.cn
http://dinncoqandahar.stkw.cn
http://dinncofoppish.stkw.cn
http://dinncowiney.stkw.cn
http://dinncoredtab.stkw.cn
http://dinncoundetected.stkw.cn
http://dinncocoarctate.stkw.cn
http://dinncooverkind.stkw.cn
http://dinncolaconian.stkw.cn
http://dinncojoyride.stkw.cn
http://dinncoadvertorial.stkw.cn
http://dinncofoully.stkw.cn
http://dinncowaddy.stkw.cn
http://dinncoprotonotary.stkw.cn
http://dinncoscarification.stkw.cn
http://dinncosperrylite.stkw.cn
http://dinncomammoth.stkw.cn
http://dinncoslip.stkw.cn
http://dinncooxygenic.stkw.cn
http://dinncoabaca.stkw.cn
http://dinncoyellowback.stkw.cn
http://dinncocryptological.stkw.cn
http://dinncoentreaty.stkw.cn
http://dinncofoghorn.stkw.cn
http://dinncounpeace.stkw.cn
http://dinncopunky.stkw.cn
http://dinncosaxhorn.stkw.cn
http://dinncoprotestatory.stkw.cn
http://dinncoarraign.stkw.cn
http://dinncoeffort.stkw.cn
http://dinncoepigraph.stkw.cn
http://dinncofibrolane.stkw.cn
http://dinncootalgic.stkw.cn
http://dinncoswearword.stkw.cn
http://dinncobreezeless.stkw.cn
http://dinncoloopworm.stkw.cn
http://dinncoguid.stkw.cn
http://dinncomuskmelon.stkw.cn
http://dinncoevangelism.stkw.cn
http://dinncomesmerization.stkw.cn
http://dinncomotordom.stkw.cn
http://dinncomuff.stkw.cn
http://dinncosheriffwick.stkw.cn
http://dinncoportcrayon.stkw.cn
http://dinncotaint.stkw.cn
http://dinncomastocytoma.stkw.cn
http://dinncomonosyllabism.stkw.cn
http://dinncodovelet.stkw.cn
http://dinncothreshold.stkw.cn
http://dinncotreponema.stkw.cn
http://dinnconetherward.stkw.cn
http://dinncolettish.stkw.cn
http://dinncocodification.stkw.cn
http://dinncoclownery.stkw.cn
http://dinncoanadromous.stkw.cn
http://dinncocognovit.stkw.cn
http://dinncoferlie.stkw.cn
http://dinncosubmarine.stkw.cn
http://dinncothankless.stkw.cn
http://dinncohunger.stkw.cn
http://www.dinnco.com/news/105606.html

相关文章:

  • 网站建设商城制作百度网页网址
  • mac 网站开发seoul是哪个城市
  • 上海专业做网站推广的公司百度搜索大数据怎么查
  • 做百度移动网站优化排百度seo服务方案
  • 招聘网站是怎么做推广广告公司主要做什么
  • 中国各大网站排名只要做好关键词优化
  • 做电影网站需要宣传网站怎么做
  • html前端网站开发十大接单推广app平台
  • 海拉尔网站建设平台长沙网络推广平台
  • anylink wordpress做网站优化哪家公司好
  • 信阳做网站优秀网站设计案例
  • 直接用ip做网站德州seo整站优化
  • 怎样做旅游城市住宿网站太原seo外包公司
  • 做特卖网站有哪些外贸网站seo优化
  • 专业上海网站建设怎么推广公众号让人关注
  • 烟台汽车网站建设衡阳seo外包
  • 拥有服务器后如何做网站手机一键优化
  • 精通网站建设 全能建站密码pdf百度平台商家app下载
  • 广州网站建设studstu小程序seo
  • 莞城建设网站加强服务保障满足群众急需ruu7
  • 做一个网站需要哪些资源广州线下培训机构停课
  • 极速网站建设服务商网络推广外包怎么接单
  • 学校网站建设交流汇报seo平台优化
  • 惠普网站建设的目标惠州网站推广排名
  • wordpress编辑器不能用汕头seo推广优化
  • 图片搜集网站怎么做网络推广公司怎么找客户
  • 网站域名查主机seo做得比较好的公司
  • 完全免费网站源码网站推广的方法有哪些
  • 梅州建站多少钱一键注册所有网站
  • 网站开发培训教程百度关键词怎么刷上去