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:
- Zbarreaderview Readerview = [[Zbarreaderview alloc]init];
- Readerview.frame = CGRectMake (0, Self.view.frame.size.width, self.view.frame.size.height-44);
- Readerview.readerdelegate = self;
- Turn off the Flash
- Readerview.torchmode = 0;
- Scan area
- CGRect scanmaskrect = CGRectMake (Cgrectgetmidy (readerview.frame)-126, 200, 200);
- Processing simulator
- if (target_iphone_simulator) {
- Zbarcamerasimulator *camerasimulator
- = [[Zbarcamerasimulator alloc]initwithviewcontroller:self];
- Camerasimulator.readerview = Readerview;
- }
- [Self.view Addsubview:readerview];
- Scan area Calculation
- Readerview.scancrop = [self getscancrop:scanmaskrect readerViewBounds:self.readerView.bounds];
- [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:
- -(CGRect) Getscancrop: (cgrect) rect readerviewbounds: (cgrect) readerviewbounds
- {
- CGFloat x,y,width,height;
- x = Rect.origin.x/readerviewbounds.size.width;
- y = rect.origin.y/readerviewbounds.size.height;
- width = rect.size.width/readerviewbounds.size.width;
- Height = rect.size.height/readerviewbounds.size.height;
- return CGRectMake (x, y, width, height);
- }
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:
- -(void) Readerview: (Zbarreaderview *) Readerview didreadsymbols: (Zbarsymbolset *) symbols FromImage: (UIImage *) image
- {
- For (Zbarsymbol *symbol in symbols) {
- NSLog (@ "%@", symbol.data);
- Break
- }
- [Self.readerview stop];
- }
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