iOS 二维码扫描和成像,成像带小图标 自定义色值

来源:互联网 发布:2016淘宝排名权重因素 编辑:程序博客网 时间:2024/05/10 05:24

工具下载:http://download.csdn.net/detail/wsk_123_123/7177635

代码:

////  YYViewController.h//  Dm////  Created by *** on 14-4-11.//  Copyright (c) 2014年 **. All rights reserved.//#import <UIKit/UIKit.h>#import "ZBarSDK.h"#import <QuartzCore/QuartzCore.h>@interface YYViewController : UIViewController<ZBarReaderDelegate,UITextFieldDelegate>-(IBAction)btnyy:(id)sender;-(IBAction)btnsm:(id)sender;@end

////  YYViewController.m//  Dm////  Created by ** on 14-4-11.//  Copyright (c) 2014年 **. All rights reserved.//#import "YYViewController.h"#import "QRCodeGenerator.h"@interface YYViewController (){    IBOutlet UIButton *btnyy;    IBOutlet UIImageView *img;    IBOutlet UITextField *sd;}@end@implementation YYViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];    sd.delegate=self;    // Do any additional setup after loading the view from its nib.}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}-(IBAction)btnsm:(id)sender{    /*扫描二维码部分:     导入ZBarSDK文件并引入一下框架     AVFoundation.framework     CoreMedia.framework     CoreVideo.framework     QuartzCore.framework     libiconv.dylib     引入头文件#import “ZBarSDK.h” 即可使用     当找到条形码时,会执行代理方法          - (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info          最后读取并显示了条形码的图片和内容。*/    [sd resignFirstResponder];    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){    ZBarReaderViewController *reader = [ZBarReaderViewController new];    reader.readerDelegate = self;    reader.supportedOrientationsMask = ZBarOrientationMaskAll;        ZBarImageScanner *scanner = reader.scanner;        [scanner setSymbology: ZBAR_I25                   config: ZBAR_CFG_ENABLE                       to: 0];        [self presentModalViewController: reader                            animated: YES];    }    else    {        UIAlertView *aler=[[UIAlertView alloc]initWithTitle:@"Message" message:@"无法打开设备的摄像头!请授权!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确认", nil];        [aler show];    }     }- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);}- (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info{      [sd resignFirstResponder];    id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];    ZBarSymbol *symbol = nil;    for(symbol in results)        // EXAMPLE: just grab the first barcode        break;        // EXAMPLE: do something useful with the barcode data    NSLog(@"%@",symbol.data);        img.image =    [info objectForKey: UIImagePickerControllerOriginalImage];        [reader dismissModalViewControllerAnimated: YES];        //判断是否包含 头'http:'    NSString *regex = @"http+:[^\\s]*";    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];        //判断是否包含 头'ssid:'    NSString *ssid = @"ssid+:[^\\s]*";;    NSPredicate *ssidPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",ssid];        sd.text =  symbol.data ;        if ([predicate evaluateWithObject:sd.text]) {                UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil                                                        message:@"It will use the browser to this URL。"                                                       delegate:nil                                              cancelButtonTitle:@"Close"                                              otherButtonTitles:@"Ok", nil];        alert.delegate = self;        alert.tag=1;        [alert show];             }    else if([ssidPre evaluateWithObject:sd.text]){                NSArray *arr = [sd.text componentsSeparatedByString:@";"];                NSArray * arrInfoHead = [[arr objectAtIndex:0] componentsSeparatedByString:@":"];                NSArray * arrInfoFoot = [[arr objectAtIndex:1] componentsSeparatedByString:@":"];                        sd.text=        [NSString stringWithFormat:@"ssid: %@ \n password:%@",         [arrInfoHead objectAtIndex:1],[arrInfoFoot objectAtIndex:1]];                        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:sd.text                                                        message:@"The password is copied to the clipboard , it will be redirected to the network settings interface"                                                       delegate:nil                                              cancelButtonTitle:@"Close"                                              otherButtonTitles:@"Ok", nil];                        alert.delegate = self;        alert.tag=2;        [alert show];         UIPasteboard *pasteboard=[UIPasteboard generalPasteboard];        //        然后,可以使用如下代码来把一个字符串放置到剪贴板上:        pasteboard.string = [arrInfoFoot objectAtIndex:1];                    }    }-(BOOL)textFieldShouldReturn:(UITextField *)textField{    [textField resignFirstResponder];    return YES;}-(IBAction)btnyy:(id)sender{    [sd resignFirstResponder];    UIImage *imgs=[UIImage imageNamed:@"1xx.png"];    img.image = [QRCodeGenerator qrImageForString:sd.text imageSize:img.bounds.size.width Topimg:imgs];//    img.image= [QRCodeGenerator  qrImageForString:sd.text imageSize:img.frame.size.width withPointType:0 withPositionType:0 withColor:[UIColor redColor]];}@end


0 0
原创粉丝点击