User defined scanning interface function of scanning two dimensional code with ZBar in iOS
- 2021-10-13 08:53:17
- OfStack
ZXing has been used in Android to identify 2D codes before, and ZXing also has a corresponding version of iOS. After understanding, ZBar is also a commonly used 2D code recognition software, and SDK of iOS and Android are provided for use respectively. Finally, I chose ZBar for 2D code recognition, which has clear comments and is easy to use.
ZBar provides us with two ways to use it, one is to directly call ZBarReaderViewController provided by ZBar to open a scanning interface, and the other is to use ZBarReaderView provided by ZBar, which can be embedded in other views. In actual projects, we are more likely to use the second way, which allows us to customize the interface more.
ZBar is also very simple to use. Import ZBarSDK into the project, and import ZBarSDK. h header files in the files that need to use ZBar
#pragma mark Initialization scan
- (void)InitScan
{
readview = [ZBarReaderView new];
readview.backgroundColor = [UIColor clearColor];
readview.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
readview.readerDelegate = self;
readview.allowsPinchZoom = YES;// Use gesture zoom
readview.trackingColor = [UIColor redColor];
readview.showsFPS = NO;// Display frame rate YES Display NO Do not display
//readview.scanCrop = CGRectMake(0, 0, 1, 1);// The area of the image to be scanned
UIImage *hbImage=[UIImage imageNamed:@"pick_bg.png"];
scanZomeBack=[[UIImageView alloc] initWithImage:hbImage];
// Add 1 Background picture
CGRect mImagerect=CGRectMake((readview.frame.size.width-200)/2.0, (readview.frame.size.height-200)/2.0, 200, 200);
[scanZomeBack setFrame:mImagerect];
readview.scanCrop = [self getScanCrop:mImagerect readerViewBounds:readview.bounds];// The area of the image to be scanned
[readview addSubview:scanZomeBack];
[readview addSubview:readLineView];
[self.view addSubview:readview];
[readview start];
}
#pragma mark Get Scan Area
-(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);
}
#pragma mark Scan animation
-(void)loopDrawLine
{
CGRect rect = CGRectMake(scanZomeBack.frame.origin.x, scanZomeBack.frame.origin.y, scanZomeBack.frame.size.width, 2);
if (readLineView) {
[readLineView removeFromSuperview];
}
readLineView = [[UIImageView alloc] initWithFrame:rect];
[readLineView setImage:[UIImage imageNamed:@"line.png"]];
[UIView animateWithDuration:3.0
delay: 0.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
// Modify fream The code of is written here
readLineView.frame =CGRectMake(scanZomeBack.frame.origin.x, scanZomeBack.frame.origin.y+scanZomeBack.frame.size.height, scanZomeBack.frame.size.width, 2);
[readLineView setAnimationRepeatCount:0];
}
completion:^(BOOL finished){
if (!is_Anmotion) {
[self loopDrawLine];
}
}];
[readview addSubview:readLineView];
}
#pragma mark Get scan results
- (void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image
{
// Get the scanned barcode content
const zbar_symbol_t *symbol = zbar_symbol_set_first_symbol(symbols.zbarSymbolSet);
NSString *symbolStr = [NSString stringWithUTF8String: zbar_symbol_get_data(symbol)];
if (zbar_symbol_get_type(symbol) == ZBAR_QRCODE) {
// Whether or not QR2 Dimension code
}
for (ZBarSymbol *symbol in symbols) {
[sTxtField setText:symbol.data];
break;
}
[readerView stop];
[readerView removeFromSuperview];
}
github Address: https://github.com/ZBar/ZBar