iOS QR code scan, you need to be aware of two things

Source: Internet
Author: User

Before iOS7, QR code and barcode scanning were implemented in iOS, and we know there are two big open source components Zbar and zxing. We've all worked on both of these components, and here's a summary of their shortcomings:

    • Zbar

Zbar on the sensitivity of the scan, and the use of memory relative to the zxing is better, but for the "Round corner QR Code" scan is very difficult. Such as:

    • ZXing

ZXing is an open-source barcode scanning library on Google code that is designed in Java and is used even by Google Glass. But someone in pursuit of more efficient and portability, the emergence of C + + port. The objectivc-c port on GitHub is actually packaged in OC code, and has been stopped for maintenance. This is very inefficient, under the instrument can see the CPU and memory crazy rise, in the memory of small machines easily crash.

    • Avfoundation

Avfoundation is optimal both in terms of scanning sensitivity and performance, so there is no doubt that we should switch to avfoundation, which requires compatibility with iOS 6 or previous versions that can be replaced with Zbar or zxing.

Here are the highlights of this article, whether you are using one or the other solutions above, you need to know the following two points.

1. QR Code with small image

Before the test to mention a bug, said there are two-dimensional code sweep, get two-dimensional code a look, is a very small two-dimensional code, edge length less than 1cm, so modified sessionpreset for 1080p, then used is zxing, when the picture quality change, also caused the performance of the decline, Basic open scanning interface will be reported memorywarning, but also did solve the problem of small QR code scanning.

Avcapturesession can set the Sessionpreset property, which determines the size of the video input image quality per frame.

    • avcapturesessionpreset320x240

    • avcapturesessionpreset352x288

    • avcapturesessionpreset640x480

    • avcapturesessionpreset960x540

    • avcapturesessionpreset1280x720

    • avcapturesessionpreset1920x1080

The above lists some of the attribute values, representing the input image quality, in general avcapturesessionpreset640x480 enough to use, but if you want to ensure that the smaller QR code image can be quickly scanned, preferably set higher, Like avcapturesessionpreset1920x1080 (which is what we often call 1080p).

2. Scancrop

Another way to improve the speed and performance of the scan is to set the scope of the resolution, in Zbar and Zxing is Scancrop, Avcapturemetadataoutput Rectofinterest property is set in Avfoundation to configure the resolution range.

At first I set this property according to the document according to the proportional value, as follows:

123456 CGSize size = self.view.bounds.size;CGRect cropRect = CGRectMake(40, 100, 240, 240);captureOutput.rectOfInterest = CGRectMake(cropRect.origin.x/size.width,                                         cropRect.origin.y/size.height,                                         cropRect.size.width/size.width,                                         cropRect.size.height/size.height);

But found that Ops, it seems wrong ah, can't sweep, obviously incorrect, so guess: avcapture output picture size is horizontal, and the iphone screen is vertical, then I rotate it 90°:

123456 CGSize size = self.view.bounds.size;CGRect cropRect = CGRectMake(40, 100, 240, 240);captureOutput.rectOfInterest = CGRectMake(cropRect.origin.y/size.height,                                         cropRect.origin.x/size.width,                                         cropRect.size.height/size.height,                                         cropRect.size.width/size.width);

OK, it seems right, all work well on the IPhone5, but on the 4s, or change the size of sessionpreset, this box seems to be less accurate, you may find out the box up and down some can also be scanned out. Again, the aspect ratio of the picture is not the same as the phone screen, and the rectofinterest is relative to the size of the picture. For example, the iphone4s screen size is 640x960, and the image output size is 1920x1080. The actual situation may be the effect of:

The following represents the Iphone4s screen, the size of the 640x960, which represents the position of the image previewed in Avcapturevideopreviewlayer, when the image is entered in 1920x1080 size, the actual size will be truncated a bit, Because the videogravity we avcapturevideopreviewlayer set is Avlayervideogravityresizeaspectfill, Similar to the Uiviewcontentmodescaleaspectfill effect of UIView.

So I made a correction to the size:

12345678910111213141516171819 CGSize size = self.view.bounds.size;CGRect cropRect = CGRectMake(40, 100, 240, 240);CGFloat p1 = size.height/size.width;CGFloat p2 = 1920./1080.;  //使用了1080p的图像输出if(p1 < p2) {  CGFloat fixHeight = bounds.size.width * 1920. / 1080.;  CGFloat fixPadding = (fixHeight - size.height)/2;  captureOutput.rectOfInterest = CGRectMake((cropRect.origin.y + fixPadding)/fixHeight,                                              cropRect.origin.x/size.width,                                              cropRect.size.height/fixHeight,                                              cropRect.size.width/size.width);else{    CGFloat fixWidth = bounds.size.height * 1080. / 1920.;    CGFloat fixPadding = (fixWidth - size.width)/2;    captureOutput.rectOfInterest = CGRectMake(cropRect.origin.y/size.height,                                              (cropRect.origin.x + fixPadding)/fixWidth,                                              cropRect.size.height/size.height,                                              cropRect.size.width/fixWidth);}

The above verification proves that the conjecture Rectofinterest is based on the size of the image.

3. Summary

Scancrop for scanning is more important, think of the picture truncation point to parse is not theoretically will be faster. The network seems to be hard to find about the scancrop of the detailed, hope to see the people have help.

iOS QR code scan, you need to be aware of two things

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.