扫描条形码获取商品信息(iOS 开发)
一。导入ZBarSDK及其依赖库(这不是本文侧重点)
二。具体方法
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
扫描条形码获取商品信息(iOS 开发)的更多相关文章
- 导入外部proto获取商品信息
Models.proto syntax = "proto3"; package services; //商品模型 message ProdModel { int32 prod_id ...
- python:爬虫获取淘宝/天猫的商品信息
[需求]输入关键字,如书包,可以搜索出对应商品的信息,包括:商品标题.商品链接.价格范围:且最终的商品信息需要符合:包邮.价格差不会超过某数值 #coding=utf-8 ""&q ...
- iOS开发如何提高
阅读博客 在现在这个碎片化阅读流行的年代,博客的风头早已被微博盖过.而我却坚持写作博客,并且大量地阅读同行的iOS开发博客.博客的文章长度通常在 3000字左右,许多iOS开发知识都至少需要这样的篇幅 ...
- 使用selenium抓取淘宝的商品信息
淘宝的页面大量使用了js加载数据,所以采用selenium来进行爬取更为简单,selenum作为一个测试工具,主要配合无窗口浏览器phantomjs来使用. import re from seleni ...
- iOS开发中获取WiFi相关信息
iOS 开发中难免会遇到很多与网络方面的判断,这里做个汇总,大多可能是与WiFi相关的. 1.Ping域名.Ping某IP 有 时候可能会遇到ping 某个域名或者ip通不通,再做下一步操作.这里的p ...
- ios开发-获取手机相关信息
今天在做客户端的时候,里面有个意见反馈功能. 调用系统带的邮件功能,发送邮件到指定邮箱. 然后我就想,应该在邮件正文部分添加手机相关内容,比如型号,版本,应用程序的版本等等,这样不仅使用者方便,开发者 ...
- iOS开发-Swift获取手机设备信息(UIDevice)
使用UiDevice获取设备信息 获取设备名称 let name = UIDevice.currentDevice().name 获取设备系统名称 let systemName = UIDevice. ...
- iOS开发-获取设备型号信息
开发中有的时候查看设计统计数据,或者通过日志查看错误信息,这个时候我们就需要获取获取设备信息,看下关于设备有几种方法: NSLog(@"%@",[[UIDevice current ...
- iOS开发-二维码扫描和应用跳转
iOS开发-二维码扫描和应用跳转 序言 前面我们已经调到过怎么制作二维码,在我们能够生成二维码之后,如何对二维码进行扫描呢? 在iOS7之前,大部分应用中使用的二维码扫描是第三方的扫描框架,例如Z ...
随机推荐
- MAC端口被占用的解决方法
html { overflow-x: initial !important } :root { --bg-color: #ffffff; --text-color: #333333; --select ...
- git原理-本地仓库认识
项目人员使用git,几乎70%的工作都是在本地仓库完成的.由此可见本地仓库的重要性. 下面我们就通过一些基本的命令讲下git的本地仓库的结构,存储流程,数据类型,如何存储...... 仓库结构 大家都 ...
- Java基础教程——注解
注解 JDK 5开始,Java支持注解. 注解,Annotation,是一种代码里的特殊标记,这些标记可以在编译.类加载.运行时被读取并执行,而且不改变原有的逻辑. 注解可以用于:生成文档.编译检查. ...
- kafka监听出现的问题,解决和剖析
问题如下: kafka为什么监听不到数据 kafka为什么会有重复数据发送 kafka数据重复如何解决 为什么kafka会出现俩个消费端都可以消费问题 kafka监听配置文件 一. 解决问题一(kaf ...
- MongoDB 分片集群配置
本文测试环境为 CentOS 7 和 MongoDB 最新版 (4.0.12) 使用 root 操作 (实际操作中使用非 root 账户启动报错) 零.服务器分配 服务器 102 服务器 103 服务 ...
- 从Paxos到Zookeeper 分布式一致性原理与实践读书心得
一 本书作者介绍 此书名为从Paxos到ZooKeeper分布式一致性原理与实践,作者倪超,阿里巴巴集团高级研发工程师,国家认证系统分析师,毕业于杭州电子科技大学计算机系.2010年加入阿里巴巴中间件 ...
- C++ 虚基类的定义、功能、规定
原文声明:http://blog.sina.com.cn/s/blog_93b45b0f01011pkz.html 虚继承和虚基类的定义是非常的简单的,同时也是非常容易判断一个继承是否是虚继承的,虽然 ...
- 第3章 Python的数据类型 第3.1节 功能强大的 Python序列概述
一.概述 序列是Python中最基本的数据结构,C语言中没有这样的数据类型,只有数组有点类似,但序列跟数组差异比较大. 序列的典型特征如下: 序列使用索引来获取元素,这种索引方式适用于所有序列: 序列 ...
- PyQt(Python+Qt)学习随笔:containers容器类部件QMdiArea多文档界面的QMdiSubWindow子窗口相关属性和操作方法
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 1.增加子窗口 QMdiArea中的子窗口类型是QMdiSubWind ...
- 关于Python中中文文本文件使用二进制方式读取后的解码UnicodeDecodeError问题
最近老猿在进行文件操作的验证测试,发现对于中文文本文件如果使用二进制方式打开,返回的类型是bytes,如果要转换成可读的字符串信息需要进行解码.可是老猿使用decode()或decode(" ...