qr码是二维码码_SwiftUI中的相机预览和QR码扫描仪-程序员宅基地

qr码是二维码码

In this post, I will guide you through the creation of a QR-code scanner for iOS, using SwiftUI.

在本文中,我将指导您使用SwiftUI创建iOS的QR码扫描仪。

We are also going to see how to create a preview of the camera feed and use it as a SwiftUI View. The end result will be compatible with the simulator, Xcode’s Live Preview as well as UI tests!

我们还将看到如何创建相机提要的预览并将其用作SwiftUI视图。 最终结果将与模拟器,Xcode的Live Preview以及UI测试兼容!

什么是QR码? (What is QR-code?)

QR-code stands for Quick Response Code. They contain small amounts of information presented in such a way that computers and mobile devices can interpret it. QR-codes are a kind of 2D barcodes called Matrix codes. Some variations exist, like Aztec Codes or Data Matrix codes. QR-codes usually look like this:

QR码代表快速响应代码。 它们包含少量信息,这些信息以计算机和移动设备可以解释的方式呈现。 QR码是一种二维码,称为矩阵码。 存在一些变体,例如Aztec代码或Data Matrix代码。 QR码通常如下所示:

入门 (Getting started)

Let’s begin by creating a new iOS app project in Xcode. You will want to choose the Single View App template and make sure the User Interface technology is set to SwiftUI.

让我们从Xcode创建一个新的iOS应用项目开始。 您将要选择Single View App模板,并确保将User Interface技术设置为SwiftUI

Image for post
Create a new Xcode Single View App in Xcode
在Xcode中创建新的Xcode Single View App
Image for post
Run the project to make sure everything is setup
运行项目以确保一切都已设置

准备布局 (Prepare the Layout)

The layout of the scanner screen probably depends on your specific app use case, so we are going to create a very simple view which allows us to see the camera preview, the value of any detected QR-codes as well as a button to toggle the torch light on and off.

扫描仪屏幕的布局可能取决于您特定的应用程序用例,因此我们将创建一个非常简单的视图,使我们可以查看相机预览,任何检测到的QR码的值以及用于切换开启和关闭手电筒。

Image for post

Create the ScannerViewModel, which will hold the state the View

创建ScannerViewModel,它将保留View的状态

The scan interval is a variable which allows us to control how often we are going to retry scanning for a QR-code. This is useful if we want to provide a continuous scan function.

扫描间隔是一个变量,它使我们可以控制重新尝试扫描QR码的频率。 如果我们要提供连续扫描功能,这将很有用。

The torchIsOn field holds the current state of the device torch light. This tutorial assumes that a torch is always available.

torchIsOn字段保存设备手电筒灯的当前状态。 本教程假定割炬始终可用。

The lastQrCode field will hold the string value of the last detected QR-code value. When the user is scanning, this field will update to the last QR-code scanned.

lastQrCode字段将保存最后检测到的QR码值的字符串值。 用户扫描时,此字段将更新为最后扫描的QR码。

Finally a function onFoundQrCode which our QrCodeScanner is going to call whenever a new QR-code has been detected.

最后一个功能onFoundQrCode ,我们的QrCodeScanner是怎么回事时已检测到新的QR码来调用。

Now let’s create the ScannerView layout

现在让我们创建ScannerView布局

We are going to use a ZStack to create a layout where the Scanner showing the camera view will be seen at the back, taking the entire screen and on top of it, we are going to layout some interface elements like the balloon with the recently scanned QR-code or the torch button.

我们将使用ZStack来创建一个布局,在该布局中,可以在背面看到显示摄像机视图的Scanner,并占据整个屏幕,并在其顶部,我们将使用最近扫描的内容来布局一些界面元素,例如气球QR码或手电筒按钮。

关于相机视图 (About the Camera View)

We are going to create a View which shows the feed from the device camera. Ideally, we also want to be able to position the Camera View using SwiftUI.

我们将创建一个视图,该视图显示来自设备相机的提要。 理想情况下,我们还希望能够使用SwiftUI定位Camera View。

Most of the functionality we are going to use comes from the AVFoundation framework. For presently it doesn’t offer any ready made SwiftUI views, we are going to create a traditional UIView and embed it as a UIViewRepresentable in our SwiftUI app.

我们将要使用的大多数功能来自AVFoundation框架。 目前,它不提供任何现成的SwiftUI视图,我们将创建一个传统的UIView并将其作为UIViewRepresentable嵌入到我们的SwiftUI应用中。

We are going to setup a AVCaptureSession which will use the device back camera as an input and a AVCaptureMetadataOutput as an output, allowing us to receive a stream of metadata objects recognised from the camera input. Those metadata objects are the actual QR-code values we are aiming to gather.

我们将设置一个AVCaptureSession ,它将使用设备后置摄像头作为输入,将AVCaptureMetadataOutput用作输出,从而使我们能够接收从摄像头输入中识别出的元数据对象流。 这些元数据对象是我们旨在收集的实际QR码值。

Since we are going to use the camera view in SwiftUI layouts, it will be ideal if the implementation plays nice with the simulator (so the SwiftUI preview function will work). Since the simulator lacks a physical camera device, we are going to embed a fallback in the camera view.

由于我们将在SwiftUI布局中使用摄影机视图,因此如果实现与模拟器完美配合将是理想的选择(因此SwiftUI预览功能将起作用)。 由于模拟器缺少物理相机设备,因此我们将在相机视图中嵌入一个备用。

代表 (The Delegate)

The delegate will be responsible for handling the metadata output, checking if a QR-code was found and informing the parent View of that QR-code value.

委托人将负责处理元数据输出,检查是否找到QR码,并将该QR码值通知父视图。

Most notably, our delegate object will adopt the AVCaptureMetadataOutputObjectsDelegate protocol in order to listen for new metadata objects which were detected in the camera feed.

最值得注意的是,我们的委托对象将采用AVCaptureMetadataOutputObjectsDelegate协议,以侦听在相机供稿中检测到的AVCaptureMetadataOutputObjectsDelegate数据对象。

The delegate will also take advantage of the scanning interval we prepared for in our View Model. As long as the capture session is active and a QR-code image is in view of the camera feed, new metadata objects will be constantly detected. For this, we are going to skip frames and only notify the parent view once per scanning interval.

委托还将利用我们在视图模型中准备的扫描间隔。 只要捕获会话处于活动状态并且QR码图像在摄像机馈送的视角下,就会不断检测到新的元数据对象。 为此,我们将跳过帧,并且每个扫描间隔仅通知一次父视图。

相机预览 (The CameraPreview)

The camera preview is the UIView we are going to use to show a live feed from the camera (or a simulated value for the simulator). This is a UIKit view which will eventually be presented using a UIViewRepresentable in our SwiftUI layout.

相机预览是UIView我们将使用它来显示相机的实时供稿(或模拟器的模拟值)。 这是一个UIKit视图,最终将在我们的SwiftUI布局中使用UIViewRepresentable进行呈现。

The view is created for a given AVCaptureSession and includes a AVCaptureVideoPreviewLayer which is based on CoreAnimation and enables us to preview the visual output from the camera session. This is what the user will eventually see as a “camera preview”. When the camera is not available, we are going to substitute the vide preview with a simple UILabel showing the mock data content we’ve provided.

该视图是为给定的AVCaptureSession创建的,并包括一个基于CoreAnimation的AVCaptureVideoPreviewLayer ,使我们能够预览摄像头会话的视觉输出。 这就是用户最终将看到的“相机预览”。 当摄像机不可用时,我们将用一个简单的UILabel代替视频预览,该UILabel显示我们提供的模拟数据内容。

This means a QR-code scan can be “simulated” during preview or live view in Xcode, the simulator or a UI test.

这意味着可以在Xcode,模拟器或UI测试的预览或实时取景期间“模拟” QR码扫描。

几乎在那里,创建UIViewRepresentable (Almost there, creating the UIViewRepresentable)

The last piece we need is a way to show the CameraPreview view in SwiftUI. This can be achieved by implementing the framework-provided UIViewRepresentable protocol.

我们需要的最后一块是在SwiftUI中显示CameraPreview视图的方法。 这可以通过实现框架提供的UIViewRepresentable协议来实现。

The purpose of this protocol is for us to sort of translate the SwiftUI state and lifecycle events to UIKit. For example, in SwiftUI views are often destroyed and recreated when their state changes while in UIKit a UIView is usually kept around and updated.

该协议的目的是让我们将SwiftUI状态和生命周期事件转换为UIKit。 例如,在SwiftUI中,视图的状态更改时通常会销毁并重新创建视图,而在UIKit中,通常会保留并更新UIView。

The bare minimum we are going to need is:

我们需要的最低要求是:

  • Create a CameraView and associate it with a AVCaptureSession.

    创建一个CameraView并将其与AVCaptureSession关联。

  • Check if we are running on a real device and if it is the case, request permission to access the camera.

    检查我们是否在真实设备上运行,如果是,请请求访问摄像机的权限。

Remember to add the NSCameraUsageDescription key to the Info.plist file otherwise using the camera will result in a crash.

请记住将 NSCameraUsageDescription 添加 Info.plist 文件,否则使用相机会导致崩溃。

  • When the SwiftUI view is updated, we need to make sure the CameraView will also update its layout.

    更新SwiftUI视图时,我们需要确保CameraView也将更新其布局。
  • We need to correctly dispose of the capture session when SwiftUI is preparing to remove and destroy the QrCodeScannerView.

    当SwiftUI准备删除和销毁QrCodeScannerView时,我们需要正确处理捕获会话。

Let’s tweak the QrCodeScannerView with the following additions:

让我们对QrCodeScannerView进行以下调整:

  • The ability to request permission to use the camera

    要求许可使用相机的能力
  • The ability to setup the scanning interval from the SwiftUI view as a modifier.

    从SwiftUI视图设置扫描间隔作为修改器的功能。
  • The ability to set the mock data for use during simulation or UI Testing.

    设置模拟数据以在仿真或UI测试期间使用的能力。
  • The ability to control the torch light of the device.

    控制设备的手电筒的能力。

Here is the complete implementation of the scanner view:

这是扫描器视图的完整实现:

结束 (The End)

Now we can use the QrCodeScannerView in the SwiftUI layout. Replace the Text view with the following:

现在我们可以在SwiftUI布局中使用QrCodeScannerView了。 将文本视图替换为以下内容:

QrCodeScannerView()
.found(r: self.viewModel.onFoundQrCode)
.torchLight(isOn: self.viewModel.torchIsOn)
.interval(delay: self.viewModel.scanInterval)

Tadaa

多田a

Image for post
Kodos app for macOS! Kodos应用程序打个招呼!
Image for post

翻译自: https://medium.com/dev-genius/camera-preview-and-a-qr-code-scanner-in-swiftui-48b111155c66

qr码是二维码码

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_26638123/article/details/108171550

智能推荐

分布式光纤传感器的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告_预计2026年中国分布式传感器市场规模有多大-程序员宅基地

文章浏览阅读3.2k次。本文研究全球与中国市场分布式光纤传感器的发展现状及未来发展趋势,分别从生产和消费的角度分析分布式光纤传感器的主要生产地区、主要消费地区以及主要的生产商。重点分析全球与中国市场的主要厂商产品特点、产品规格、不同规格产品的价格、产量、产值及全球和中国市场主要生产商的市场份额。主要生产商包括:FISO TechnologiesBrugg KabelSensor HighwayOmnisensAFL GlobalQinetiQ GroupLockheed MartinOSENSA Innovati_预计2026年中国分布式传感器市场规模有多大

07_08 常用组合逻辑电路结构——为IC设计的延时估计铺垫_基4布斯算法代码-程序员宅基地

文章浏览阅读1.1k次,点赞2次,收藏12次。常用组合逻辑电路结构——为IC设计的延时估计铺垫学习目的:估计模块间的delay,确保写的代码的timing 综合能给到多少HZ,以满足需求!_基4布斯算法代码

OpenAI Manager助手(基于SpringBoot和Vue)_chatgpt网页版-程序员宅基地

文章浏览阅读3.3k次,点赞3次,收藏5次。OpenAI Manager助手(基于SpringBoot和Vue)_chatgpt网页版

关于美国计算机奥赛USACO,你想知道的都在这_usaco可以多次提交吗-程序员宅基地

文章浏览阅读2.2k次。USACO自1992年举办,到目前为止已经举办了27届,目的是为了帮助美国信息学国家队选拔IOI的队员,目前逐渐发展为全球热门的线上赛事,成为美国大学申请条件下,含金量相当高的官方竞赛。USACO的比赛成绩可以助力计算机专业留学,越来越多的学生进入了康奈尔,麻省理工,普林斯顿,哈佛和耶鲁等大学,这些同学的共同点是他们都参加了美国计算机科学竞赛(USACO),并且取得过非常好的成绩。适合参赛人群USACO适合国内在读学生有意向申请美国大学的或者想锻炼自己编程能力的同学,高三学生也可以参加12月的第_usaco可以多次提交吗

MySQL存储过程和自定义函数_mysql自定义函数和存储过程-程序员宅基地

文章浏览阅读394次。1.1 存储程序1.2 创建存储过程1.3 创建自定义函数1.3.1 示例1.4 自定义函数和存储过程的区别1.5 变量的使用1.6 定义条件和处理程序1.6.1 定义条件1.6.1.1 示例1.6.2 定义处理程序1.6.2.1 示例1.7 光标的使用1.7.1 声明光标1.7.2 打开光标1.7.3 使用光标1.7.4 关闭光标1.8 流程控制的使用1.8.1 IF语句1.8.2 CASE语句1.8.3 LOOP语句1.8.4 LEAVE语句1.8.5 ITERATE语句1.8.6 REPEAT语句。_mysql自定义函数和存储过程

半导体基础知识与PN结_本征半导体电流为0-程序员宅基地

文章浏览阅读188次。半导体二极管——集成电路最小组成单元。_本征半导体电流为0

随便推点

【Unity3d Shader】水面和岩浆效果_unity 岩浆shader-程序员宅基地

文章浏览阅读2.8k次,点赞3次,收藏18次。游戏水面特效实现方式太多。咱们这边介绍的是一最简单的UV动画(无顶点位移),整个mesh由4个顶点构成。实现了水面效果(左图),不动代码稍微修改下参数和贴图可以实现岩浆效果(右图)。有要思路是1,uv按时间去做正弦波移动2,在1的基础上加个凹凸图混合uv3,在1、2的基础上加个水流方向4,加上对雾效的支持,如没必要请自行删除雾效代码(把包含fog的几行代码删除)S..._unity 岩浆shader

广义线性模型——Logistic回归模型(1)_广义线性回归模型-程序员宅基地

文章浏览阅读5k次。广义线性模型是线性模型的扩展,它通过连接函数建立响应变量的数学期望值与线性组合的预测变量之间的关系。广义线性模型拟合的形式为:其中g(μY)是条件均值的函数(称为连接函数)。另外,你可放松Y为正态分布的假设,改为Y 服从指数分布族中的一种分布即可。设定好连接函数和概率分布后,便可以通过最大似然估计的多次迭代推导出各参数值。在大部分情况下,线性模型就可以通过一系列连续型或类别型预测变量来预测正态分布的响应变量的工作。但是,有时候我们要进行非正态因变量的分析,例如:(1)类别型.._广义线性回归模型

HTML+CSS大作业 环境网页设计与实现(垃圾分类) web前端开发技术 web课程设计 网页规划与设计_垃圾分类网页设计目标怎么写-程序员宅基地

文章浏览阅读69次。环境保护、 保护地球、 校园环保、垃圾分类、绿色家园、等网站的设计与制作。 总结了一些学生网页制作的经验:一般的网页需要融入以下知识点:div+css布局、浮动、定位、高级css、表格、表单及验证、js轮播图、音频 视频 Flash的应用、ul li、下拉导航栏、鼠标划过效果等知识点,网页的风格主题也很全面:如爱好、风景、校园、美食、动漫、游戏、咖啡、音乐、家乡、电影、名人、商城以及个人主页等主题,学生、新手可参考下方页面的布局和设计和HTML源码(有用点赞△) 一套A+的网_垃圾分类网页设计目标怎么写

C# .Net 发布后,把dll全部放在一个文件夹中,让软件目录更整洁_.net dll 全局目录-程序员宅基地

文章浏览阅读614次,点赞7次,收藏11次。之前找到一个修改 exe 中 DLL地址 的方法, 不太好使,虽然能正确启动, 但无法改变 exe 的工作目录,这就影响了.Net 中很多获取 exe 执行目录来拼接的地址 ( 相对路径 ),比如 wwwroot 和 代码中相对目录还有一些复制到目录的普通文件 等等,它们的地址都会指向原来 exe 的目录, 而不是自定义的 “lib” 目录,根本原因就是没有修改 exe 的工作目录这次来搞一个启动程序,把 .net 的所有东西都放在一个文件夹,在文件夹同级的目录制作一个 exe._.net dll 全局目录

BRIEF特征点描述算法_breif description calculation 特征点-程序员宅基地

文章浏览阅读1.5k次。本文为转载,原博客地址:http://blog.csdn.net/hujingshuang/article/details/46910259简介 BRIEF是2010年的一篇名为《BRIEF:Binary Robust Independent Elementary Features》的文章中提出,BRIEF是对已检测到的特征点进行描述,它是一种二进制编码的描述子,摈弃了利用区域灰度..._breif description calculation 特征点

房屋租赁管理系统的设计和实现,SpringBoot计算机毕业设计论文_基于spring boot的房屋租赁系统论文-程序员宅基地

文章浏览阅读4.1k次,点赞21次,收藏79次。本文是《基于SpringBoot的房屋租赁管理系统》的配套原创说明文档,可以给应届毕业生提供格式撰写参考,也可以给开发类似系统的朋友们提供功能业务设计思路。_基于spring boot的房屋租赁系统论文