扫描条形码获取商品信息(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 ...
随机推荐
- 关于Jersey框架下的Aop日志 和Spring 框架下的Aop日志
摘要 最近新接手的项目经常要查问题,但是,前面一拨人,日志打的非常乱,好多就根本没有打日志,所以弄一个AOP统一打印一下 请求数据和响应数据 框架 spring+springmvc+jersey 正文 ...
- 模拟赛41 A. 四个质数的和
题目描述 给定了一个正整数 \(N\).有多少种方法将 \(N\) 分解成为四个质数 \(a,b,c,d\)的和. 例如: \(9=2+2+2+3=2+2+3+2=2+3+2+2=3+2+2+2\), ...
- CentOS 7防火墙的关闭与开启
(1)CentOS 7.0默认使用的是firewall作为防火墙:若没有启用iptables 作为防火墙,则使用以下方式关闭防火墙: systemctl stop firewalld.service ...
- Dotnet Core下的Channel, 你用了吗?
今天给大家分享一个微软官方的好东西:Channel. 前言 今天给大家分享一个微软官方的生产者/消费者方案的特性解决:Channel. Channel在System.Threading.Chann ...
- LeetCode 046 Permutations
题目要求:Permutations(全排列) Given a collection of numbers, return all possible permutations. For example, ...
- 02_启动和销毁Service
在Application关闭后,Service仍然会运行. package com.example.servdemo; import android.app.Activity; import andr ...
- 欢天喜地七仙女——UML设计
这个作业的要求在哪里 作业要求 团队名称 欢天喜地七仙女 团队成员 王玮晗.林鑫宇.黄龙骏.陈少龙.何一山.崔亚明.陆桂莺 这个作业的目标 团队一起绘制UML图 作业正文 如下 其它参考文献 见文末 ...
- 第8.1节 Python类的构造方法__init__深入剖析:语法释义
一. 引言 凡是面向对象设计的语言,在类实例化时都有构造方法,很多语言的构造方法名与类名一致,Python中类的构造方法比较特殊,必须是__init__特殊方法. 二. 语法释义 1. ...
- PyQt(Python+Qt)学习随笔:布局控件layoutStretch属性
在Qt Designer中布局控件有4个,分别是Vertical Layout(垂直布局).Horizontal Layout(水平布局).Grid Layout(网格布局).Form Layout( ...
- PyQt(Python+Qt)学习随笔:Qt Designer中建立CommandLinkButton信号与Action的槽函数连接
在Qt Designer中,通过F4进行信号和槽函数连接编辑时,接收信号的对象不能是Action对象,但在右侧的编辑界面,可以选择将一个界面对象的信号与Action对象的槽函数连接起来. 如图: 上图 ...