怎样在网页建立二维码_建立您的二维码阅读器

怎样在网页建立二维码

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

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

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

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

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:

之后,运行会话之前的下一步将是:

  1. Set the output type, in this case QR

    设置输出类型,在这种情况下为QR

  2. Merge the the video variable with the session

    视频变量会话合并

  3. Set video frames

    设置视频帧
  4. Set the video level with the whole screen

    全屏设置视频电平

  5. Set a sublevel with the your viewfinder

    使用取景器设置子级别

And finally you can run the session.

最后,您可以运行会话。

Image for post

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

It’s time to run the application on your real device.

现在该在实际设备上运行该应用程序了。

Image for post

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

怎样在网页建立二维码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值