1、首先下载ZbarSDk,这里不再赘余。唯一提醒的一点事找支持arm64的版本的sdk
2、话不多说,直接贴代码:
ScanViewController.h
#import <UIKit/UIKit.h>
#import "ZBarSDK.h"
#import <Cordova/CDVPlugin.h>
#import <Cordova/CDVViewController.h>
@interface ScanViewController : CDVViewController<ZBarReaderDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIAlertViewDelegate>
@property (nonatomic,strong) CDVInvokedUrlCommand* command;
@property (nonatomic,strong) CDVPlugin* scanPlugin;
@property (nonatomic,assign) int scanType;
@property (nonatomic,strong) UIImagePickerController* imagePickerController;
- (void) scan2D;
@end
ScanViewController.m
#import "ScanViewController.h"
@interface ScanViewController ()
@end
@implementation ScanViewController
@synthesize command,scanPlugin,scanType,imagePickerController;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void) scan2D
{
UIAlertView* imageAlertView = [[UIAlertView alloc]initWithTitle:@"扫描方式" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"相机",@"相册选取", nil];
[imageAlertView show];
}
/*
* 选择框点击事件
*/
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//NSLog(@"buttonIndex is : %li",(long)buttonIndex);
switch (buttonIndex) {
case 0:{
}break;
case 1:{
[self scan2DByCamera];
scanType = 1;
}break;
case 2:{
[self scan2DByPhoto];
scanType = 2;
}break;
default:
break;
}
}
-(void) scan2DByCamera
{
ZBarReaderViewController * reader = [[ZBarReaderViewController alloc] init];
reader.readerDelegate = self;
//非全屏
reader.wantsFullScreenLayout = NO;
//隐藏底部控制按钮
reader.showsZBarControls = YES;
//设置自己定义的界面
[self setOverlayPickerView:reader];
ZBarImageScanner * scanner = reader.scanner;
[scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0];
reader.sourceType = UIImagePickerControllerSourceTypeCamera;
reader.showsZBarControls = YES;
//reader.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:reader animated:YES completion:nil];
}
-(void) scan2DByPhoto
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
imagePickerController = [[UIImagePickerController alloc]init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.delegate = self;
[self.view addSubview: imagePickerController.view];
}else{
NSLog(@"相册不能用。。。");
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
CDVPluginResult* pluginResult = nil;
if (scanType == 1) {
id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults];
ZBarSymbol * symbol;
for(symbol in results)
break;
//_imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
//NSLog(@"扫描到的二维码:%@",symbol.data);
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:symbol.data];
}else if(scanType == 2){
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
ZBarReaderController* read = [ZBarReaderController new];
read.readerDelegate = self;
CGImageRef cgImageRef = image.CGImage;
ZBarSymbol* symbol = nil;
for(symbol in [read scanImage:cgImageRef])
break;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:symbol.data];
}
[scanPlugin.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
[picker dismissViewControllerAnimated:YES completion:nil];
[self.view removeFromSuperview];
[self removeFromParentViewController];
//_label.text = symbol.data;
}
- (void)setOverlayPickerView:(ZBarReaderViewController *)reader
{
//清除原有控件
for (UIView *temp in [reader.view subviews]) {
for (UIButton *button in [temp subviews]) {
if ([button isKindOfClass:[UIButton class]]) {
[button removeFromSuperview];
}
}
for (UIToolbar *toolbar in [temp subviews]) {
if ([toolbar isKindOfClass:[UIToolbar class]]) {
[toolbar setHidden:YES];
[toolbar removeFromSuperview];
}
}
}
//画中间的基准线
//UIView* line = [[UIView alloc] initWithFrame:CGRectMake(40, 220, 240, 1)];
//line.backgroundColor = [UIColor redColor];
//[reader.view addSubview:line];
//最上部view
UIView* upView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
upView.alpha = 0.3;
upView.backgroundColor = [UIColor blackColor];
[reader.view addSubview:upView];
//用于说明的label
UILabel * labIntroudction= [[UILabel alloc] init];
labIntroudction.backgroundColor = [UIColor clearColor];
labIntroudction.frame=CGRectMake(15, 20, 290, 50);
labIntroudction.numberOfLines=2;
labIntroudction.textColor=[UIColor whiteColor];
labIntroudction.text=@"将二维码图像置于矩形方框内,离手机摄像头10CM左右,系统会自动识别。";
[upView addSubview:labIntroudction];
//左侧的view
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 80, 20, 280)];
leftView.alpha = 0.3;
leftView.backgroundColor = [UIColor blackColor];
[reader.view addSubview:leftView];
//右侧的view
UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(300, 80, 20, 280)];
rightView.alpha = 0.3;
rightView.backgroundColor = [UIColor blackColor];
[reader.view addSubview:rightView];
//底部view
UIView * downView = [[UIView alloc] initWithFrame:CGRectMake(0, 360, 320, 160)];
downView.alpha = 0.3;
downView.backgroundColor = [UIColor blackColor];
[reader.view addSubview:downView];
//用于取消操作的button
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
cancelButton.alpha = 0.4;
[cancelButton setFrame:CGRectMake(20, 390, 280, 40)];
[cancelButton setTitle:@"取消" forState:UIControlStateNormal];
[cancelButton.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
[cancelButton addTarget:self action:@selector(dismissOverlayView:)forControlEvents:UIControlEventTouchUpInside];
[reader.view addSubview:cancelButton];
}
//取消button方法
- (void)dismissOverlayView:(id)sender{
[self dismissViewControllerAnimated:YES completion:nil];
[self.view removeFromSuperview];
[self removeFromParentViewController];
}
//取消选择相片
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
if (nil != imagePickerController) {
[imagePickerController.view removeFromSuperview];
}
}
@end