怎样在网页建立二维码
The QR Codes are two-dimensional barcodes designed by the automotive industry in 1994, but now widely used in many fields such as banking, industrial, commercial or social.
QR码是由汽车行业在1994年设计的二维条形码,但现在已广泛用于银行,工业,商业或社会等许多领域。
In the projects you are working on, this technology may need to be integrated, so let’s see how to build a QR code reader with the AVFoundation library in Swift .
在您正在从事的项目中,可能需要集成这项技术,因此让我们看看如何使用Swift中的AVFoundation库构建QR码阅读器。
我们去上班 (We go to work)
After creating a new project in Xcode the first thing to do is go to info.plist, to set the camera permissions.
在Xcode中创建新项目后,要做的第一件事是进入info.plist,以设置摄像机权限。
Now you have to add a new key, scrolling down to Privacy - Camera use description and then you have to set a relative string
现在,您必须添加一个新密钥,向下滚动到“ 隐私-相机使用说明” ,然后必须设置一个相对字符串
Like in this screen:
就像在这个屏幕上一样:
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/dfaf03bc14829b03f3cd0561744d9ff6.png)
I wrote Used to scan the QR Code, but you can write other.
我写了“用于扫描QR码”,但您可以写其他。
Well, now for have a good look create a viewfinder in .png format, to include in the project.
好吧,现在好了,创建一个.png格式的取景器以包含在项目中。
As you can see, to make the demo more beautiful, I’ve also included a navigation controller.
如您所见,为使演示更加美观,我还包括了一个导航控制器。
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/b1dfb87e69932d80e68c755c1d9105d0.png)
To get the viewfinder you can use any graphics application such as Photoshop or Affinity Photo or you can search for it on the web.
要获取取景器,您可以使用任何图形应用程序,例如Photoshop或Affinity Photo,也可以在网络上搜索它。
是时候开始编码了 (It’s time to start coding)
Go to ViewController.swift and start importing the AVFoundation library, entering the delegate responsible for capturing metadata next to the ViewController class.
转到ViewController.swift并开始导入AVFoundation库,在ViewController类旁边输入负责捕获元数据的委托。
Without forgetting to import the viewfinder too.
也不要忘记导入取景器。
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/9c79696f4a19038a60e3089687d993db.png)
Now, create a variable that will contain the video capture preview, then it will contain the view that you will show to the user when he wants to scan a QR Code.
现在,创建一个将包含视频捕获预览的变量 ,然后它将包含您要在用户想要扫描QR码时显示给用户的视图。
Then, open a CaptureSession (currently empty), define the device used (iPhone, iPad, etc) and then get input capture, which will return a value after an error check.
然后,打开一个CaptureSession (当前为空),定义使用的设备 (iPhone,iPad等),然后获取输入capture ,它将在错误检查后返回一个值。
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/69b7b663b84fbe012257a19b63c4033e.png)
The next step will be to define the constant where the output will be processed.
下一步将是定义将在其中处理输出的常量 。
Now the thing you need to be careful, is to process the output obtained (from the session) in the best possible way and you can do that by processing it on the main thread.
现在,您需要注意的事情是以最佳方式处理( 从会话获得的)输出,您可以通过在主线程上进行处理来实现。
This will give you the best results in terms of performance.
就性能而言,这将为您带来最佳结果。
After that, the next steps before running the session will be:
之后,运行会话之前的下一步将是:
Set the output type, in this case QR
设置输出类型,在这种情况下为QR
Merge the the video variable with the session
将视频变量与会话合并
- Set video frames 设置视频帧
Set the video level with the whole screen
全屏设置视频电平
Set a sublevel with the your viewfinder
使用取景器设置子级别
And finally you can run the session.
最后,您可以运行会话。
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/0461833c3bc0bfde9785e54fd0767b85.png)
Almost done!
快完成了!
Your app is able to access the camera and scan the QR Codes and then get the output, but you are not able to use it yet.
您的应用程序可以访问相机并扫描QR码 ,然后获取输出,但是您尚无法使用它。
Then, call the metadataOutput function where you provide the output as an array and where you check if there is something in it or if you need continue to scan.
然后,调用metadataOutput函数,在其中您将输出作为数组提供,并在其中检查其中是否有内容或是否需要继续扫描。
To view the contents of a QR Code, in this example you will print the value in a string and show it in an alert, but first you need to check if the object in the array is QR type.
要查看QR Code的内容,在本示例中,您将以字符串形式打印该值并将其显示在警报中 , 但是首先,您需要检查数组中的对象是否为QR类型。
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/f3819e9a34cef51308c43e0bf9a80f97.png)
It’s time to run the application on your real device.
现在该在实际设备上运行该应用程序了。
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/f7892b42445e1f80341e0d945fb6b13a.png)
Wonderful! Everything has gone well!
精彩! 一切顺利!
Congratulations! Now you can scan any QR code you want and know what’s hidden.
恭喜你! 现在,您可以扫描所需的任何QR码,并知道隐藏了什么。
结论 (Conclusion)
This is a simple example of how to get a QR code reader and work with them.
这是一个简单的示例,说明如何获取QR码阅读器并与之配合使用。
There are so many things you can do with the scan, I hope this little window of a big topic was useful.
扫描可以做很多事情,我希望这个大主题的小窗口对您有用。
You can learn more about this by looking at the sample project available on GitHub: https://github.com/OryDS/Swift_QRCode_Reader
您可以通过查看GitHub上的示例项目来了解有关此内容的更多信息: https : //github.com/OryDS/Swift_QRCode_Reader
翻译自: https://medium.com/swlh/build-your-qr-code-reader-7b89ca401e9a
怎样在网页建立二维码