1.下载ZBar的第三方库,添加入工程
https://github.com/bmorton/ZBarSDK
2.添加相关库
AVFoundation.framework
CoreMedia.framework
CoreVideo.framework
libiconv.2.4.0.dylib
3.增加一个以
ZBarReaderViewController
为父类的控制器
,
并实现
ZBarReaderDelegate
代理
4.在控制器中添加如下代码
条形码的扫描
- (
void
)viewDidLoad{
[ super viewDidLoad ];
// 设置代理
self . readerDelegate = self ;
// 扫瞄图像
ZBarImageScanner *mScanner = self . scanner ;
// 是否显示绿色的追踪框,注意,即当选择 yes 的时候,这个框仅仅当扫瞄 EAN 和 I2/5 的时候才可见。
self . tracksSymbols = YES ;
// 是否使用备用控制组
self . showsZBarControls = YES ;
// 支持的方向,用 ZBarOrientationMask() 和 ZBarOrientationMaskAll
self . supportedOrientationsMask = ZBarOrientationMask ( UIInterfaceOrientationMaskPortrait );
// 提供自定义覆盖层。注意,在 showsZBarControls 启用的情况下才可以用
UIView *view = [[ UIView alloc ] initWithFrame : self . view . bounds ];
view. backgroundColor = [ UIColor grayColor ];
self . cameraOverlayView = view;
// 裁剪扫描的图像,在扫描前图像将被裁剪到这个矩形内,这个矩形框是将图像的尺寸和宽高比标准化,
// 有效值将放置矩形内介于 0 和 1 的每个轴,其中 x 轴对应于图像的长轴。默认为完整的图像( 0 , 0 , 1 , 1 )。
// self.scanCrop
// 调节以适应预览图片
// self.cameraViewTransform
//
解码配置
[mScanner setSymbology : ZBAR_I25
config : ZBAR_CFG_ENABLE
to : 0 ];
// Do any additional setup after loading the view.
}
- ( void )viewDidAppear:( BOOL )animated
{
[ super viewDidAppear :animated];
[ self . readerView start ];
}
- ( void )viewWillDisappear:( BOOL )animated
{
[ super viewWillDisappear :animated];
[ self . readerView stop ];
}
- ( void )imagePickerController:( UIImagePickerController *)picker didFinishPickingMediaWithInfo:( NSDictionary *)info
{
// 获取扫瞄结果
id < NSFastEnumeration > results = [info objectForKey : ZBarReaderControllerResults ];
ZBarSymbol *symbol = nil ;
[mScanner setSymbology : ZBAR_I25
config : ZBAR_CFG_ENABLE
to : 0 ];
// Do any additional setup after loading the view.
}
- ( void )viewDidAppear:( BOOL )animated
{
[ super viewDidAppear :animated];
[ self . readerView start ];
}
- ( void )viewWillDisappear:( BOOL )animated
{
[ super viewWillDisappear :animated];
[ self . readerView stop ];
}
- ( void )imagePickerController:( UIImagePickerController *)picker didFinishPickingMediaWithInfo:( NSDictionary *)info
{
// 获取扫瞄结果
id < NSFastEnumeration > results = [info objectForKey : ZBarReaderControllerResults ];
ZBarSymbol *symbol = nil ;
//
仅仅是抓住第一个条形码
for (symbol in results)
break ;
NSString *text = symbol. data ;
// 解决中文乱码问题
if ([text canBeConvertedToEncoding : NSShiftJISStringEncoding ]) {
text = [ NSString stringWithCString :[text cStringUsingEncoding : NSShiftJISStringEncoding ] encoding : NSUTF8StringEncoding ];
}
NSDictionary *dic = [[ NSDictionary alloc ] initWithObjectsAndKeys :[ NSString stringWithFormat : @"%@" ,text], @"resultLabel" ,[info objectForKey : UIImagePickerControllerOriginalImage ], @"resultImgView" , nil ];
[ self performSelectorOnMainThread : @selector (mainAction:) withObject :dic waitUntilDone : NO ];
}
- (
void
)mainAction:(
NSDictionary
*)dic
{
OtherViewController *other = [[ OtherViewController alloc ] init ];
other. resultString = [dic objectForKey : @"resultLabel" ];
other. image = [dic objectForKey : @"resultImgView" ];
[ self . navigationController pushViewController :other animated : YES ];
// delegate is notified of decode results.
// enable pinch gesture recognition for zooming the preview/decode
// (default YES).
for (symbol in results)
break ;
NSString *text = symbol. data ;
// 解决中文乱码问题
if ([text canBeConvertedToEncoding : NSShiftJISStringEncoding ]) {
text = [ NSString stringWithCString :[text cStringUsingEncoding : NSShiftJISStringEncoding ] encoding : NSUTF8StringEncoding ];
}
NSDictionary *dic = [[ NSDictionary alloc ] initWithObjectsAndKeys :[ NSString stringWithFormat : @"%@" ,text], @"resultLabel" ,[info objectForKey : UIImagePickerControllerOriginalImage ], @"resultImgView" , nil ];
[ self performSelectorOnMainThread : @selector (mainAction:) withObject :dic waitUntilDone : NO ];
}
{
OtherViewController *other = [[ OtherViewController alloc ] init ];
other. resultString = [dic objectForKey : @"resultLabel" ];
other. image = [dic objectForKey : @"resultImgView" ];
[ self . navigationController pushViewController :other animated : YES ];
}
此时,我们再来看看
ZBarReaderViewController
中的
ZBarReaderView这个类
// supply a pre-configured image scanner.
//支持一个预先准备的图片扫描
- ( id ) initWithImageScanner: ( ZBarImageScanner *) imageScanner;
// start the video stream and barcode reader.
- ( id ) initWithImageScanner: ( ZBarImageScanner *) imageScanner;
// start the video stream and barcode reader.
//开始视频流和条形码扫描
- ( void ) start;
// stop the video stream and barcode reader.
- ( void ) start;
// stop the video stream and barcode reader.
//停止视频流和条形码扫描
- ( void ) stop;
// clear the internal result cache
- ( void ) stop;
// clear the internal result cache
//清理内部的缓存
- ( void ) flushCache;
// compensate for device/camera/interface orientation
- ( void ) flushCache;
// compensate for device/camera/interface orientation
// 适应设备的方向
- ( void ) willRotateToInterfaceOrientation: ( UIInterfaceOrientation ) orient
- ( void ) willRotateToInterfaceOrientation: ( UIInterfaceOrientation ) orient
duration: (
NSTimeInterval
) duration;
//代理
@property ( nonatomic , assign ) id < ZBarReaderViewDelegate > readerDelegate;
// access to image scanner for configuration.
@property ( nonatomic , assign ) id < ZBarReaderViewDelegate > readerDelegate;
// access to image scanner for configuration.
//直接对图片扫描配置
@property ( nonatomic , readonly ) ZBarImageScanner *scanner;
// whether to display the tracking annotation for uncertain barcodes
// (default YES).
@property ( nonatomic , readonly ) ZBarImageScanner *scanner;
// whether to display the tracking annotation for uncertain barcodes
// (default YES).
// 是否为不确定的条形码显示追踪
@property ( nonatomic ) BOOL tracksSymbols;
// color of the tracking box (default green)
@property ( nonatomic ) BOOL tracksSymbols;
// color of the tracking box (default green)
//追踪框的颜色,默认为绿色
@property
(
nonatomic
,
retain
)
UIColor
*trackingColor;
// (default YES).
// 是否能对预览图进行手势缩放,默认是可以的
@property ( nonatomic ) BOOL allowsPinchZoom;
// torch mode to set automatically (default Auto).
@property ( nonatomic ) BOOL allowsPinchZoom;
// torch mode to set automatically (default Auto).
// 0为不闪光,1为闪光
// zoom scale factor applied to video preview *and* scanCrop.
// also updated by pinch-zoom gesture. clipped to range [1,maxZoom],
// defaults to 1.25
@property
(
nonatomic
)
NSInteger
torchMode;
// also updated by pinch-zoom gesture. clipped to range [1,maxZoom],
// defaults to 1.25
// 施加于摄像前景画面和扫描区域的缩放
@property ( nonatomic ) CGFloat zoom;
- ( void ) setZoom: ( CGFloat ) zoom
// the region of the image that will be scanned. normalized coordinates.
@property ( nonatomic ) CGFloat zoom;
- ( void ) setZoom: ( CGFloat ) zoom
animated: (
BOOL
) animated;
// 图片将被扫描的区域,标准坐标
@property
(
nonatomic
)
CGRect
scanCrop;
二维码的扫描
实现代理
ZBarReaderViewDelegate
{
ZBarSymbol *symbol = nil;
for (symbol in symbols)
break;
NSString *text = symbol.data;
NSLog(@"%@",text);
}