天天看点

iOS使用Zbar扫描二维码iOS使用Zbar扫描二维码

iOS使用Zbar扫描二维码

二维码扫描

iOS

Zbar64位

正文:

首先下载一个支持64位系统的ZbarSDK的包,保存在了我的云盘里,地址:ZbarSDK

  1. 把文件拖到工程里面
  2. 然后添加依赖库:AVFoundation.framework、libiconv.2.dylib、CoreMedia.framework、CoreVideo.framework,其中libzbar.a是SDK中的,(如果需要通过SVN上传自己代码时,要记得还有这个.a文件)
  3. 在你要进行添加扫描的界面的.h里 可直接复制到工程里

    添加头文件

  1. #import "ZBarSDK.h"
  2. //设置代理
  3. @protocol ErweimaDelegate <NSObject>
  4. //扫描出结果则调用这个方法
  5. -(void)finishRead:(NSString *)barCode;
  6. @end
  7. @interface ErweimaViewController : superVC < ZBarReaderViewDelegate > //此处superVC是自定义的父类
  8. @property (strong, nonatomic)id < ErweimaDelegate >delegate;
  9. @end

4.在.m文件中:

  1. @interface ErweimaViewController ()
  2. {
  3. ZBarReaderView *rederView;//二维码扫描View,可自定义样式
  4. UIImageView *lineView;//自定义的扫描线
  5. NSTimer *time;//定时器,控制扫描线跑动
  6. }
  1. //在进入界面时就开始扫描
  2. -(void)viewWillAppear:(BOOL)animated{
  3. [super viewWillAppear:animated];
  4. [rederView start];
  5. }
  6. //退出界面时就停止扫描
  7. - (void)viewWillDisappear:(BOOL)animated
  8. {
  9. [super viewWillDisappear:animated];
  10. [rederView stop];
  11. }
  1. -(void)viewDidLoad {
  2. [super viewDidLoad];
  3. // Do any additional setup after loading the view.
  4. self.title = @"扫描";
  5. [self initView];
  6. }
  7. - (void)initView
  8. {
  9. rederView = [[ZBarReaderView alloc] init];
  10. rederView.frame = CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height);
  11. rederView.readerDelegate = self;
  12. rederView.allowsPinchZoom = NO; // 不使用Pinch手势变焦
  13. rederView.torchMode = 0;//闪光灯 关
  14. rederView.trackingColor = [UIColor clearColor];//锁定图码 显示的颜色框
  15. if (TARGET_IPHONE_SIMULATOR) {
  16. ZBarCameraSimulator *camera = [[ZBarCameraSimulator alloc] initWithViewController:self];
  17. camera.readerView = rederView;
  18. }
  19. [self.view addSubview:rederView];
  20. /*************可忽略此域中的以下代码,可以自己自定义***************/
  21. //扫描区域计算
  22. CGRect scanMaskRect = CGRectMake(Width_Screen/2-75, 150, 150, 150);
  23. CGRect scanCrop=[self getScanCrop:scanMaskRect readerViewBounds:rederView.bounds];
  24. rederView.scanCrop = scanCrop;
  25. [rederView.captureReader captureFrame];
  26. [rederView start];
  27. UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(-20, -64, Width_Screen+40, Height_Screen)];
  28. image.image = [UIImage imageNamed:@"111111111"];
  29. image.backgroundColor = [UIColor clearColor];
  30. image.alpha = 0.3;
  31. [self.view addSubview:image];
  32. //这里添加了类似微信的扫描线,并开始上线滑动动画
  33. lineView=[[UIImageView alloc] initWithFrame:CGRectMake(51, 200, 219, 3)];
  34. // lineView.image=[UIImage imageNamed:@"scan_line"];
  35. lineView.backgroundColor = UIColor_Theme;
  36. [self.view addSubview:lineView];
  37. [self loadAnimationStart];
  38. }
  39. -(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)rvBounds{
  40. CGFloat x,y,width,height;
  41. x = rect.origin.y / rvBounds.size.height;
  42. y = 1 - (rect.origin.x + rect.size.width) / rvBounds.size.width;
  43. width = (rect.origin.y + rect.size.height) / rvBounds.size.height;
  44. height = 1 - rect.origin.x / rvBounds.size.width;
  45. return CGRectMake(x, y, width, height);
  46. }
  47. -(void)loadAnimationStart{
  48. if(Width_Screen != 320){
  49. [UIView animateWithDuration:2.0 animations:^{
  50. lineView.frame=CGRectMake(lineView.frame.origin.x, 390,lineView.frame.size.width,lineView.frame.size.height);
  51. } completion:^(BOOL finished) {
  52. time = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(secStart) userInfo:nil repeats:NO];
  53. }];
  54. }else{
  55. [UIView animateWithDuration:2.0 animations:^{
  56. lineView.frame=CGRectMake(lineView.frame.origin.x, 345,lineView.frame.size.width,lineView.frame.size.height);
  57. } completion:^(BOOL finished) {
  58. time = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(secStart) userInfo:nil repeats:NO];
  59. }];
  60. }
  61. }
  62. -(void)secStart{
  63. if(Width_Screen != 320){
  64. lineView.frame=CGRectMake(lineView.frame.origin.x, 175,lineView.frame.size.width,lineView.frame.size.height);
  65. if (time != nil) {
  66. [self loadAnimationStart];
  67. }
  68. }else{
  69. lineView.frame=CGRectMake(lineView.frame.origin.x, 130,lineView.frame.size.width,lineView.frame.size.height);
  70. if (time != nil) {
  71. [self loadAnimationStart];
  72. NSLog(@"123");
  73. }
  74. NSLog(@"qwe");
  75. }
  76. }
  77. -(void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image
  78. {
  79. NSString *str;//扫描出的字段,记录下来
  80. for (ZBarSymbol *symbol in symbols) {
  81. NSLog(@"%@", symbol.data);
  82. str=symbol.data;
  83. [rederView stop];
  84. [self.navigationController popViewControllerAnimated:YES];
  85. [self showHudMessageAndHidden:str];//展示出扫描结果,根据需求可自定义展示样式
  86. lineView.hidden = YES;
  87. [time setFireDate:[NSDate distantFuture]];
  88. [time invalidate];
  89. time = nil;
  90. break;
  91. }
  92. //这个代理是 返回到上个界面把str传过去的,根据需求可改变
  93. if([_delegate respondsToSelector:@selector(finishRead:)]){
  94. [_delegate finishRead:str];
  95. }
  96. }
结束,搞定。预祝成功。
<div class="remark-icons">
</div>