一。导入ZBarSDK及其依赖库(这不是本文侧重点)

    1、下载地址 https://github.com/bmorton/ZBarSDK
    2、导入头文件 #import "ZBarSDK.h" 实现委托事件 <ZBarReaderDelegate>

二。具体方法

1.viewController.m文件

#import "ViewController.h"

#import "ZBarSDK.h"

#import "AFNetworking.h"

#import "ZbarOverlayView.h"

@interface ViewController ()<ZBarReaderDelegate>

{

ZBarReaderView * reader;

ZbarOverlayView *_overLayView;

}

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@property (weak, nonatomic) IBOutlet UILabel *goods_name;

@property (weak, nonatomic) IBOutlet UILabel *goods_code;

@property (weak, nonatomic) IBOutlet UILabel *manuName;

@end

@implementation ViewController

- (IBAction)didBt:(UIButton *)sender {

reader.hidden = NO;

}

//扫描成功回调方法

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

id results = [info objectForKey:ZBarReaderControllerResults];

ZBarSymbol * symbol;

for(symbol in results)

break;

_imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];

[picker dismissViewControllerAnimated:YES completion:nil];

NSLog(@"%@  %u----%@----%d",symbol.data,symbol.type,symbol.typeName,symbol.count);

//扫描结果得到产品编号

[self httpCode:symbol.data];

}

//运用AFNetworking post请求 得到产品信息(关于AFNetworking网上很多例子)

-(void)httpCode:(NSString *)code{

//方法一

/*中国商品信息服务平台

http://search.anccnet.com/searchResult2.aspx

*/

/*

//方法一:中国商品信息服务平台

http://search.anccnet.com/searchResult2.aspx

//方法二:第三方接口

http://www.mxnzp.com/api/barcode/goods/details

本例子采用方法二

*/

//请求路径

NSString * URLString = @"http://www.mxnzp.com/api/barcode/goods/detail?";

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

//设置返回类型

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", nil];

// 请求参数设置

NSDictionary *dict = @{

@"barcode":code,

};

//2、发送请求

[manager POST:URLString parameters:dict progress:^(NSProgress * _Nonnull downloadProgress) {

} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

NSDictionary* json = [NSJSONSerialization

JSONObjectWithData:responseObject

options:kNilOptions

error:nil];

NSLog(@"----------%@----%@",responseObject,json);

if ([[json objectForKey:@"error_code"] integerValue] == 0) {

self.goods_name.text = [json objectForKey:@"data"][@"goodsName"];

self.goods_code.text = [json objectForKey:@"data"][@"code"];

self.manuName.text = [json objectForKey:@"data"][@"manuName"];

reader.hidden = YES;

}

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

NSLog(@"%@",error);

}];

}

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

[self init_camera];

}

- (void) init_camera

{

reader = [ZBarReaderView new];

ZBarImageScanner * scanner = [ZBarImageScanner new];

[scanner setSymbology:ZBAR_PARTIAL config:0 to:0];

[reader initWithImageScanner:scanner];

reader.readerDelegate = self;

const float h = [UIScreen mainScreen].bounds.size.height;

const float w = [UIScreen mainScreen].bounds.size.width;

const float h_padding = 0.20*w;

const float v_padding = 60;

CGRect reader_rect = CGRectMake(h_padding, v_padding,

w * 0.6, w * 0.6);//视图中的一小块,实际使用中最好传居中的区域

CGRect reader_rect1 = CGRectMake(0, 0, w, h);//全屏模式

reader.frame = reader_rect1;

reader.backgroundColor = [UIColor redColor];

[reader start];

[self.view addSubview: reader];

_overLayView = [[ZbarOverlayView alloc]initWithFrame:reader.frame];//添加覆盖视图

//    [_overLayView startAnimation];

_overLayView.transparentArea = reader_rect;//设置中间可选框大小

[reader addSubview:_overLayView];

reader.scanCrop = [self getScanCrop:reader_rect readerViewBounds:reader_rect1];;// CGRectMake(100 / h,0.5, 1/3.0,0.4);

}

-(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)readerViewBounds

{

CGFloat fullWidth = readerViewBounds.size.width;

CGFloat fullHeight = readerViewBounds.size.height;

CGFloat x,y,width,height;

x = rect.origin.x;

y = rect.origin.y;

width = rect.size.width;

height = rect.size.height;

if (x + width > fullWidth) {

if (width > fullWidth) {

width = fullWidth;

}else{

x = 0;

}

}

if (y + height > fullHeight) {

if (height > fullHeight) {

height = fullHeight;

}else{

y = 0;

}

}

CGFloat x1,y1,width1,height1;

x1 = (fullWidth - width - x) / fullWidth;

y1 = y / fullHeight;

width1 = width / fullWidth;

height1 = rect.size.height / readerViewBounds.size.height;

NSLog(@"frame:%@",NSStringFromCGRect(CGRectMake(y1, x1,height1, width1)));

return CGRectMake(y1, x1,height1, width1);

}

- (void) readerView:(ZBarReaderView *)readerView didReadSymbols: (ZBarSymbolSet *)symbols fromImage:(UIImage *)image

{

ZBarSymbol * s = nil;

for (s in symbols)

{

NSLog(@"----%@",s.data);

self.goods_code.text = s.data;

_imageView.image = image;

//扫描结果得到产品编号

[self httpCode:s.data];

break;

}

}

- (void)viewDidDisappear:(BOOL)animated

{

[super viewDidDisappear:animated];

[_overLayView stopAnimation];

}

三。实现效果

四。如有问题可以联系。

1.邮箱:liuzhuan155@163.com

2.QQ:282020508

最新文章

  1. [Linux] 账户管理命令(二)
  2. Elasticsearch增删改查 之 —— Update更新
  3. reason: &#39;-[__NSCFNumber rangeOfCharacterFromSet:]: unrecognized selector sent to instance
  4. SAE J2534 Pass-Thru API
  5. 转自作者:phylips@bmy
  6. 淘宝可以传照片搜索商品,verygood.雅客VC多味水果糖
  7. Linux内核空间-用户空间通信之debugfs
  8. Google maps API开发
  9. django rest-framework 2.请求和响应
  10. 解决android 大图OOM的两种方法
  11. 使用maven生成可执行jar包(包含依赖)
  12. java结合node.js非对称加密,实现密文登录传参——让前后端分离的项目更安全
  13. 《Head First 设计模式》例子的C++实现(2 观察者模式)
  14. 计时器setTimeout()
  15. HDU3440(差分约束)
  16. Java互联网架构-Mysql分库分表订单生成系统实战分析
  17. iOS开源项目:FlatUIKit
  18. Unix系统编程()发送信号的其他方式:raise和killpg
  19. IOS证书之Certificates,Devices, Identifiers &amp; Profiles
  20. hdu 4989(水题)

热门文章

  1. Java基础教程——Set
  2. LinuxKernel(一)
  3. VM15 Ubuntu18.04下固定IP
  4. Leetcode 021 Merge Two Sorted Lists
  5. 最佳置换算法OPT
  6. MinIO
  7. websocket简单实现五子棋即时对战功能
  8. 在django中使用原生sql语句
  9. 转:csdn怎么快速别人的文章
  10. 74CMS3.0储存型XSS漏洞代码审计