Scan barcodes for product information (IOS development)

Source: Internet
Author: User

One. Import ZBARSDK and its dependent libraries (this is not the focus of this article)

1, Https://github.com/bmorton/ZBarSDK 2, import header file #import "ZBarSDK.h" to implement delegate events <ZBarReaderDelegate>

Two. Specific methods

1.VIEWCONTROLLER.M file

#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;

}

Scan Success Callback method

-(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);

Scan results Get Product number

[Self httpCode:symbol.data];

}

Use afnetworking POST request to get product information (for many examples on afnetworking web)

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

Method One

/* China Commodity Information Service Platform

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

*/

/*

Method One: China commodity information Service Platform

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

Method Two: Afanda data platform

Http://api.avatardata.cn/Barcode/Lookup

This example uses method two

*/

Request Path

NSString * urlstring = @ "Http://api.avatardata.cn/Barcode/Lookup";

Afhttpsessionmanager *manager = [Afhttpsessionmanager manager];

Set return type

Manager.responseserializer = [Afhttpresponseserializer serializer];

Manager.responseSerializer.acceptableContentTypes = [Nsset setwithobjects:@ "Application/json", nil];

Request parameter Settings

Nsdictionary *dict = @{@ "key": @ "317acbbe2ec74dbdbe880d90cffaf65d",

@ "Barcode": Code,

};

2. Send Request

[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:@ "result"][@ "goodsname"];

Self.goods_code.text = [JSON objectforkey:@ "result"][@ "code"];

Self.manuName.text = [JSON objectforkey:@ "result"][@ "manuname"];

Reader.hidden = YES;

}

} failure:^ (Nsurlsessiondatatask * _nullable task, Nserror * _nonnull error) {

NSLog (@ "%@", error);

}];

}

-(void) Viewdidload {

[Super Viewdidload];

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);//a small block in the view, preferably in the center of the area

CGRect reader_rect1 = CGRectMake (0, 0, W, h);//Full Screen mode

Reader.frame = Reader_rect1;

Reader.backgroundcolor = [Uicolor Redcolor];

[Reader start];

[Self.view Addsubview:reader];

_overlayview = [[Zbaroverlayview alloc]initwithframe:reader.frame];//Add Overlay view

[_overlayview startanimation];

_overlayview.transparentarea = reader_rect;//Set the middle optional box size

[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;

Scan results Get Product number

[Self httpcode:s.data];

Break

}

}

-(void) Viewdiddisappear: (BOOL) animated

{

[Super viewdiddisappear:animated];

[_overlayview stopanimation];

}

Three. Achieve results

Four. Contact us if you have any questions.

1. Email: [Email protected]

2.qq:282020508

Scan barcodes for product information (IOS development)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.