用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文件并引入一下框架
AVFoundation.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