用ZBar三方库生成和扫描二维码
2015-07-23 16:12
591 查看
用ZBar三方库生成和扫描二维码
在MainViewController中
.h
#import <UIKit/UIKit.h>
#import "ZBarSDK.h"
@interface MainViewController :
UIViewController < ZBarReaderDelegate ,UITextViewDelegate ,UINavigationControllerDelegate,
UIImagePickerControllerDelegate>
@property (strong,
nonatomic) IBOutlet
UITextView *textView_Code;
- (IBAction)ButtonAction_Scan:(id)sender;
@end
.m
#import "MainViewController.h"
@interface
MainViewController ()
@end
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return
self;
}
- (void)viewDidLoad
{
[super
viewDidLoad];
self.textView_Code.text =
@"dsgdsgsdhg";
// Do any additional setup after loading the view.
NSNotificationCenter *defaultCenter = [NSNotificationCenter
defaultCenter];
//UIKeyboardWillShowNotification键盘出现
[defaultCenter
addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
//UIKeyboardWillHideNotification
键盘隐藏
[defaultCenter
addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
// 键盘显示
- (void)keyboardWillShow:(NSNotification *)aNotification
{
//获取键盘的高度
NSDictionary *userInfo = [aNotification
userInfo];
NSValue *aValue = [userInfo
objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue
CGRectValue];
int height = keyboardRect.size.height;
CGRect frame = self.textView_Code.frame;
frame.size.height =
self.view.bounds.size.height -
10 - height;
[self.textView_Code
setFrame:frame];
}
// 键盘消失
- (void)keyboardWillHide:(NSNotification *)aNotification
{
CGRect frame = self.textView_Code.frame;
frame.size.height =
self.view.bounds.size.height -
23;
[self.textView_Code
setFrame:frame];
}
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// 页面传递数据
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UIViewController *destination = segue.destinationViewController;
if ([destination respondsToSelector:@selector(setCodeString:)]) {
[destination
setValue:self.textView_Code.text
forKey:@"codeString"];
}
}
- (IBAction)ButtonAction_Scan:(id)sender{
[self.textView_Code
resignFirstResponder];
/*扫描二维码部分:
导入ZBarSDK文件并引入一下框架
***Foundation.framework
CoreMedia.framework
CoreVideo.framework
QuartzCore.framework
libiconv.dylib
引入头文件#import “ZBarSDK.h”
即可使用
当找到条形码时,会执行代理方法
- (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info
最后读取并显示了条形码的图片和内容。*/
ZBarReaderViewController *reader = [ZBarReaderViewController
new];
reader.readerDelegate =
self;
reader.supportedOrientationsMask =
ZBarOrientationMaskAll;
//设置自己定义的界面
[self
setOverlayPickerView:reader];
ZBarImageScanner *scanner = reader.scanner;
[scanner setSymbology:
ZBAR_I25
config: ZBAR_CFG_ENABLE
to: 0];
[self
presentViewController:reader
animated:YES
completion:^{
}];
}
- (void)setOverlayPickerView:(ZBarReaderViewController *)reader
{
//
清除不需要的控件,并加入自定义的
for (UIView *temp
in [reader.view
subviews]) {
for (UIToolbar *toolbar
in [temp subviews]) {
if ([toolbar isKindOfClass:[UIToolbar
class]])
{
UIBarButtonItem *item1 = [[UIBarButtonItem
alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(dismissOverlayView:)];
UIBarButtonItem *item2 = [[UIBarButtonItem
alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
UIBarButtonItem *item3 = [[UIBarButtonItem
alloc]initWithTitle:@"相册"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(clickPhotoToolBarItem:)];
NSArray *array = [NSArray
arrayWithObjects:item1,item2,item3,nil];
[toolbar
setItems:array animated:YES];
}
}
}
}
// 选取相册
-(void)clickPhotoToolBarItem:(id)sender
{
if ([UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *imagePicker = [[UIImagePickerController
alloc]
init];
imagePicker.delegate =
self;
imagePicker.allowsEditing =
NO;
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
[self
dismissViewControllerAnimated:NO
completion:^{}];
[self
presentViewController:imagePicker animated:YES
completion:^{}];
}
}
//取消按钮方法
- (void)dismissOverlayView:(id)sender{
[self
dismissViewControllerAnimated:YES
completion:^{
}];
}
#pragma mark -
#pragma mark 扫描二维码回调
- (void) imagePickerController: (UIImagePickerController*) reader
didFinishPickingMediaWithInfo: (NSDictionary*) info
{
id<NSFastEnumeration> results = [info
objectForKey: ZBarReaderControllerResults];
if (results != nil)
{
//
是扫描进入的情况
ZBarSymbol *symbol =
nil;
for(symbol in results)
break;
// self.imageView_Code.image = [info objectForKey: UIImagePickerControllerOriginalImage];
[self
dismissViewControllerAnimated:YES
completion:^{
}];
NSString *code = [NSString
stringWithString:symbol.data];
self.textView_Code.text = code;
}
else
{
//
选择图片的情况
UIImage *image = [info
objectForKey:@"UIImagePickerControllerOriginalImage"];
[self
dismissViewControllerAnimated:YES
completion:^{
}];
ZBarReaderController* read = [ZBarReaderController
new];
read.readerDelegate =
self;
CGImageRef cgImageRef = image.CGImage;
ZBarSymbol* symbol =
nil;
for(symbol in [read
scanImage:cgImageRef])
break;
NSString *code = symbol.data;
self.textView_Code.text = code;
}
}
@end
在CreateImageViewController中
.h
#import <UIKit/UIKit.h>
@interface CreateImageViewController :
UIViewController
@property (strong,
nonatomic) NSString *codeString;
//@property (strong, nonatomic) IBOutlet UITextView *textView_Code;
@property (strong,
nonatomic) IBOutlet
UIImageView *imageView_Code;
- (IBAction)buttonAction_SaveToAlbum:(id)sender;
@end
.m
#import "CreateImageViewController.h"
#import "QRCodeGenerator.h"
@interface
CreateImageViewController ()
@end
@implementation CreateImageViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return
self;
}
- (void)viewDidLoad
{
[super
viewDidLoad];
// Do any additional setup after loading the view.
NSLog(@"%@",self.codeString);
NSArray *array = [NSArray
arrayWithObjects:@"1",@"1",@"3",
nil];
NSString *str = @"1";
NSInteger index = [array
indexOfObject:str];
if (index == NSNotFound) {
NSLog(@"不存在数组中");
}
else
{
NSLog(@"第%i个",index);
}
}
-(void)viewDidAppear:(BOOL)animated
{
[super
viewDidAppear:animated];
if (![self.codeString
isEqualToString:@""]) {
/*字符转二维码
导入 libqrencode文件
引入头文件#import "QRCodeGenerator.h"
即可使用
*/
self.imageView_Code.image = [QRCodeGenerator
qrImageForString:self.codeString
imageSize:self.imageView_Code.bounds.size.width];
}else
{
UIAlertView *alert = [[UIAlertView
alloc] initWithTitle:nil
message:@"输入字符为空"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert
show];
}
}
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// 保存到相册按钮
- (IBAction)buttonAction_SaveToAlbum:(id)sender
{
UIImageWriteToSavedPhotosAlbum(self.imageView_Code.image,
self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:),
nil);
}
// 图片存放到相册后的通知
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError
*)error contextInfo:(void *) contextInfo {
NSString *message;
NSString *title;
if (!error) {
title =
@"Success";
message =
@"成功保存到相册。";
}
else {
title =
@"Failure";
#if DEBUG_MODE
message = [error description];
#else
message =
@"保存到相册失败。";
#endif
}
UIAlertView *alert = [[UIAlertView
alloc]
initWithTitle:nil
message:message
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert
show];
}
@end
这是框架结构,需要添加的哭,都在图片上显示了
在MainViewController中
.h
#import <UIKit/UIKit.h>
#import "ZBarSDK.h"
@interface MainViewController :
UIViewController < ZBarReaderDelegate ,UITextViewDelegate ,UINavigationControllerDelegate,
UIImagePickerControllerDelegate>
@property (strong,
nonatomic) IBOutlet
UITextView *textView_Code;
- (IBAction)ButtonAction_Scan:(id)sender;
@end
.m
#import "MainViewController.h"
@interface
MainViewController ()
@end
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return
self;
}
- (void)viewDidLoad
{
[super
viewDidLoad];
self.textView_Code.text =
@"dsgdsgsdhg";
// Do any additional setup after loading the view.
NSNotificationCenter *defaultCenter = [NSNotificationCenter
defaultCenter];
//UIKeyboardWillShowNotification键盘出现
[defaultCenter
addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
//UIKeyboardWillHideNotification
键盘隐藏
[defaultCenter
addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
// 键盘显示
- (void)keyboardWillShow:(NSNotification *)aNotification
{
//获取键盘的高度
NSDictionary *userInfo = [aNotification
userInfo];
NSValue *aValue = [userInfo
objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue
CGRectValue];
int height = keyboardRect.size.height;
CGRect frame = self.textView_Code.frame;
frame.size.height =
self.view.bounds.size.height -
10 - height;
[self.textView_Code
setFrame:frame];
}
// 键盘消失
- (void)keyboardWillHide:(NSNotification *)aNotification
{
CGRect frame = self.textView_Code.frame;
frame.size.height =
self.view.bounds.size.height -
23;
[self.textView_Code
setFrame:frame];
}
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// 页面传递数据
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UIViewController *destination = segue.destinationViewController;
if ([destination respondsToSelector:@selector(setCodeString:)]) {
[destination
setValue:self.textView_Code.text
forKey:@"codeString"];
}
}
- (IBAction)ButtonAction_Scan:(id)sender{
[self.textView_Code
resignFirstResponder];
/*扫描二维码部分:
导入ZBarSDK文件并引入一下框架
***Foundation.framework
CoreMedia.framework
CoreVideo.framework
QuartzCore.framework
libiconv.dylib
引入头文件#import “ZBarSDK.h”
即可使用
当找到条形码时,会执行代理方法
- (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info
最后读取并显示了条形码的图片和内容。*/
ZBarReaderViewController *reader = [ZBarReaderViewController
new];
reader.readerDelegate =
self;
reader.supportedOrientationsMask =
ZBarOrientationMaskAll;
//设置自己定义的界面
[self
setOverlayPickerView:reader];
ZBarImageScanner *scanner = reader.scanner;
[scanner setSymbology:
ZBAR_I25
config: ZBAR_CFG_ENABLE
to: 0];
[self
presentViewController:reader
animated:YES
completion:^{
}];
}
- (void)setOverlayPickerView:(ZBarReaderViewController *)reader
{
//
清除不需要的控件,并加入自定义的
for (UIView *temp
in [reader.view
subviews]) {
for (UIToolbar *toolbar
in [temp subviews]) {
if ([toolbar isKindOfClass:[UIToolbar
class]])
{
UIBarButtonItem *item1 = [[UIBarButtonItem
alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(dismissOverlayView:)];
UIBarButtonItem *item2 = [[UIBarButtonItem
alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
UIBarButtonItem *item3 = [[UIBarButtonItem
alloc]initWithTitle:@"相册"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(clickPhotoToolBarItem:)];
NSArray *array = [NSArray
arrayWithObjects:item1,item2,item3,nil];
[toolbar
setItems:array animated:YES];
}
}
}
}
// 选取相册
-(void)clickPhotoToolBarItem:(id)sender
{
if ([UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *imagePicker = [[UIImagePickerController
alloc]
init];
imagePicker.delegate =
self;
imagePicker.allowsEditing =
NO;
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
[self
dismissViewControllerAnimated:NO
completion:^{}];
[self
presentViewController:imagePicker animated:YES
completion:^{}];
}
}
//取消按钮方法
- (void)dismissOverlayView:(id)sender{
[self
dismissViewControllerAnimated:YES
completion:^{
}];
}
#pragma mark -
#pragma mark 扫描二维码回调
- (void) imagePickerController: (UIImagePickerController*) reader
didFinishPickingMediaWithInfo: (NSDictionary*) info
{
id<NSFastEnumeration> results = [info
objectForKey: ZBarReaderControllerResults];
if (results != nil)
{
//
是扫描进入的情况
ZBarSymbol *symbol =
nil;
for(symbol in results)
break;
// self.imageView_Code.image = [info objectForKey: UIImagePickerControllerOriginalImage];
[self
dismissViewControllerAnimated:YES
completion:^{
}];
NSString *code = [NSString
stringWithString:symbol.data];
self.textView_Code.text = code;
}
else
{
//
选择图片的情况
UIImage *image = [info
objectForKey:@"UIImagePickerControllerOriginalImage"];
[self
dismissViewControllerAnimated:YES
completion:^{
}];
ZBarReaderController* read = [ZBarReaderController
new];
read.readerDelegate =
self;
CGImageRef cgImageRef = image.CGImage;
ZBarSymbol* symbol =
nil;
for(symbol in [read
scanImage:cgImageRef])
break;
NSString *code = symbol.data;
self.textView_Code.text = code;
}
}
@end
在CreateImageViewController中
.h
#import <UIKit/UIKit.h>
@interface CreateImageViewController :
UIViewController
@property (strong,
nonatomic) NSString *codeString;
//@property (strong, nonatomic) IBOutlet UITextView *textView_Code;
@property (strong,
nonatomic) IBOutlet
UIImageView *imageView_Code;
- (IBAction)buttonAction_SaveToAlbum:(id)sender;
@end
.m
#import "CreateImageViewController.h"
#import "QRCodeGenerator.h"
@interface
CreateImageViewController ()
@end
@implementation CreateImageViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return
self;
}
- (void)viewDidLoad
{
[super
viewDidLoad];
// Do any additional setup after loading the view.
NSLog(@"%@",self.codeString);
NSArray *array = [NSArray
arrayWithObjects:@"1",@"1",@"3",
nil];
NSString *str = @"1";
NSInteger index = [array
indexOfObject:str];
if (index == NSNotFound) {
NSLog(@"不存在数组中");
}
else
{
NSLog(@"第%i个",index);
}
}
-(void)viewDidAppear:(BOOL)animated
{
[super
viewDidAppear:animated];
if (![self.codeString
isEqualToString:@""]) {
/*字符转二维码
导入 libqrencode文件
引入头文件#import "QRCodeGenerator.h"
即可使用
*/
self.imageView_Code.image = [QRCodeGenerator
qrImageForString:self.codeString
imageSize:self.imageView_Code.bounds.size.width];
}else
{
UIAlertView *alert = [[UIAlertView
alloc] initWithTitle:nil
message:@"输入字符为空"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert
show];
}
}
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// 保存到相册按钮
- (IBAction)buttonAction_SaveToAlbum:(id)sender
{
UIImageWriteToSavedPhotosAlbum(self.imageView_Code.image,
self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:),
nil);
}
// 图片存放到相册后的通知
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError
*)error contextInfo:(void *) contextInfo {
NSString *message;
NSString *title;
if (!error) {
title =
@"Success";
message =
@"成功保存到相册。";
}
else {
title =
@"Failure";
#if DEBUG_MODE
message = [error description];
#else
message =
@"保存到相册失败。";
#endif
}
UIAlertView *alert = [[UIAlertView
alloc]
initWithTitle:nil
message:message
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert
show];
}
@end
这是框架结构,需要添加的哭,都在图片上显示了
相关文章推荐
- K&R C与ANSI C函数声明的区别
- caffe的基本数据结构
- mysql处理字符串的两个绝招:substring_index,concat
- ActivityManager: Warning: Activity not started, its current task has been brought to the front 的的问题
- gcc 命令
- javascript基础(二)
- 设置Linux下Mysql表名不区分大小写
- 水平凡 创建新的插件
- 定时任务中ifconfig问题
- ORA-15025: could not open disk "/dev/asm***"
- 全选,反选,取消
- 为什么有时候要 encodeURI(url)两次才不会出现乱码
- jquery easyui树形菜单调用点击事件例子
- ZZUOJ - 1241 - 斐波那契数
- 求1~100间的素数.java
- Java程序理解
- red-black tree的删除节点算法
- Android数据保存之SharedPreference
- Linux操作系统源码包安装过程
- JAVA 虚拟机内存