IOS 中实现自定义扫描二维码和生成二维码

首先分享一下我当时下载SDK的地址:

http://code4app.com/ios/%E4%BA%8C%E7%BB%B4%E7%A0%81%E6%89%AB%E6%8F%8F%E5%92%8C%E7%94%9F%E6%88%90/501a70f16803fa1c73000000/


步骤如下:


1.下载下来后解压压缩包,这里我们只需要把两个文件夹导入我们的工程里分别是:

2.导入工程需要的框架:

IOS 中实现自定义扫描二维码和生成二维码_第1张图片

3.最后就是建一个类:

类的.h :

#import
#import "ZBarSDK.h"

@interface ViewController : UIViewController< ZBarReaderDelegate,UIAlertViewDelegate >


@property (weak, nonatomic) IBOutlet UILabel *label;

@property (weak, nonatomic) IBOutlet UIImageView *imageview;
@property (weak, nonatomic) IBOutlet UITextField *text;

@end


类的.m:

#import "ViewController.h"
#import "ZBarSDK.h"
#import "QRCodeGenerator.h"
#import "ZBarReaderController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [self setLabel:nil];
    [self setImageview:nil];
    [self setText:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}


- (IBAction)saomiaoBtnDown:(UIButton *)sender {
    /*扫描二维码部分:
     导入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;
    
    ZBarImageScanner *scanner = reader.scanner;
    
    [scanner setSymbology: ZBAR_I25
                   config: ZBAR_CFG_ENABLE
                       to: 0];
    
    [self presentModalViewController: reader
                            animated: YES];
}

- (IBAction)getCodeBtnDown:(UIButton *)sender {
    /*字符转二维码
     导入 libqrencode文件
     引入头文件#import "QRCodeGenerator.h" 即可使用
     */
    _imageview.image = [QRCodeGenerator qrImageForString:_text.text imageSize:_imageview.bounds.size.width];
}

- (IBAction)textFieldBtnDown:(UITextField *)sender {
    
    //键盘释放
    [_text resignFirstResponder];
    

}

- (void) imagePickerController: (UIImagePickerController*) reader
 didFinishPickingMediaWithInfo: (NSDictionary*) info
{
    id results =
    [info objectForKey: ZBarReaderControllerResults];
    ZBarSymbol *symbol = nil;
    for(symbol in results)
        break;
    
    _imageview.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];
    
    _label.text =  symbol.data ;
    
    
    if ([predicate evaluateWithObject:_label.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:_label.text]){
        
        NSArray *arr = [_label.text componentsSeparatedByString:@";"];
        
        NSArray * arrInfoHead = [[arr objectAtIndex:0] componentsSeparatedByString:@":"];
        
        NSArray * arrInfoFoot = [[arr objectAtIndex:1] componentsSeparatedByString:@":"];
        
        
        _label.text=
        [NSString stringWithFormat:@"ssid: %@ \n password:%@",
         [arrInfoHead objectAtIndex:1],[arrInfoFoot objectAtIndex:1]];
        
        
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:_label.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];
        
        
    }
    
    
}

最后如果编译运行过程中遇到报的错是在IOS 中实现自定义扫描二维码和生成二维码_第2张图片下面的,也就是说编译通不过是这个texts的原因,我们可以直接删掉它,我当时就是直接删了,就没有问题了。

以上分享的东西仅仅是我开发过程中总结的一些东西,希望可以帮到大家。



你可能感兴趣的:(IOS,扫描二维码和生成二维码)