Scan the QR code using Zbar in iOS

Source: Internet
Author: User

  

Recently in the project to use the QR code scanning function, previously used in Android zxing identification QR code, zxing also has the corresponding iOS version, after understanding, Zbar is also a common QR code recognition software, and provide iOS and Android SDK is available for use , I finally chose Zbar to identify the two-dimensional code, and its annotations are clear and easy to use.

Zbar provides us with two ways of using a direct call to the Zbarreaderviewcontroller provided by Zbar to open a scanning interface, and the other is to use Zbar provides zbarreaderview that can be embedded in other views, In actual projects we are more likely to use the second approach, which allows us to customize the interface more.

Zbar is also very simple to use, import zbarsdk into the project, import ZBarSDK.h header files in files that need to use Zbar, the following is the initialization method of Zbarreaderview:

    1. Zbarreaderview Readerview = [[Zbarreaderview alloc]init];
    2. Readerview.frame = CGRectMake (0, Self.view.frame.size.width, self.view.frame.size.height-44);
    3. Readerview.readerdelegate = self;
    4. Turn off the Flash
    5. Readerview.torchmode = 0;
    6. Scan area
    7. CGRect scanmaskrect = CGRectMake (Cgrectgetmidy (readerview.frame)-126, 200, 200);
    8. Processing simulator
    9. if (target_iphone_simulator) {
    10. Zbarcamerasimulator *camerasimulator
    11. = [[Zbarcamerasimulator alloc]initwithviewcontroller:self];
    12. Camerasimulator.readerview = Readerview;
    13. }
    14. [Self.view Addsubview:readerview];
    15. Scan area Calculation
    16. Readerview.scancrop = [self getscancrop:scanmaskrect readerViewBounds:self.readerView.bounds];
    17. [Readerview start];
Copy Code

The above code needs to be explained in the following points:

Flash settings
I don't want to turn on the flash when I scan the QR code, so set the Zbarreaderview torchmode to 0 and you can make it any other appropriate value.
Scan area Calculation
This is more important, we commonly used two-dimensional code scanning software effective scanning area is generally central area, the other part is not scanned, Zbar can be set by the Zbarreaderview Scancrop property scanning area, its default value is CGRect (0, 0, 1, 1), Indicates that the entire Zbarreaderview area is a valid scan area. We need to calculate the scanning area coordinates as the corresponding Baidu score coordinates, which is called in the above code Getscancrop:readerviewbounds method, pro-Test no problem, as follows:

    1. -(CGRect) Getscancrop: (cgrect) rect readerviewbounds: (cgrect) readerviewbounds
    2. {
    3. CGFloat x,y,width,height;
    4. x = Rect.origin.x/readerviewbounds.size.width;
    5. y = rect.origin.y/readerviewbounds.size.height;
    6. width = rect.size.width/readerviewbounds.size.width;
    7. Height = rect.size.height/readerviewbounds.size.height;
    8. return CGRectMake (x, y, width, height);
    9. }
Copy Code

PS: Find a lot of this method on the Internet is the horizontal and vertical intersection, so there is a problem, carefully think about it will understand.

Once the initialization section is complete, you can call Zbarreaderview's Start method to begin scanning, and you need to have your class implement the Zbarreaderviewdelegate protocol, which will call delegate's corresponding method when scanning to a QR code. Finally, when the QR code has been recognized, you can call Zbarreaderview's Stop method to stop the scan. As shown below:

    1. -(void) Readerview: (Zbarreaderview *) Readerview didreadsymbols: (Zbarsymbolset *) symbols FromImage: (UIImage *) image
    2. {
    3. For (Zbarsymbol *symbol in symbols) {
    4. NSLog (@ "%@", symbol.data);
    5. Break
    6. }
    7. [Self.readerview stop];
    8. }
Copy Code

All right, so much, isn't it very simple?

Http://www.apkbus.com/android-127507-1-1.html

Scan the QR code using Zbar in iOS

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.