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

工具下载: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
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值