二维码扫描

来源:互联网 发布:国服lol mac版 编辑:程序博客网 时间:2024/04/28 22:50

 

//根据输入的字符生成二维码

- (IBAction)generateQRCodeAction:(UIButton *)sender {

    /*字符转二维码

     导入 libqrencode文件

     引入头文件#import "QRCodeGenerator.h"即可使用

     */

    self.QRCodeImageView.image = [QRCodeGeneratorqrImageForString:self.showResultLabel.textimageSize:self.QRCodeImageView.bounds.size.width];

}

//开始扫描

- (IBAction)startScanAction:(UIButton *)sender {

    /*扫描二维码部分:

     导入ZBarSDK文件并引入一下框架

     AVFoundation.framework

     CoreMedia.framework

     CoreVideo.framework

     QuartzCore.framework

     libiconv.dylib

     引入头文件#import “ZBarSDK.h” 即可使用

     当找到条形码时,会执行代理方法

     

     - (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info

     

     最后读取并显示了条形码的图片和内容。*/

    

    ZBarReaderViewController * readerVC = [ZBarReaderViewControllernew];//[[ZBarReaderViewController alloc] init];

    

    readerVC.readerDelegate =self;

    readerVC.supportedOrientationsMask =ZBarOrientationMaskAll;

    

   ZBarImageScanner * scanner = readerVC.scanner;

    [scanner setSymbology:ZBAR_I25config:ZBAR_CFG_ENABLEto:0];

    [selfpresentViewController:readerVC animated:YEScompletion:nil    ];

}


//扫描完成

- (void) imagePickerController: (UIImagePickerController*) reader

 didFinishPickingMediaWithInfo: (NSDictionary*) info

{

    id<NSFastEnumeration> results =

    [info objectForKey:ZBarReaderControllerResults];

   ZBarSymbol *symbol = nil;

   for(symbol in results)

       break;

   NSLog(@"info = %@", info);

    //给二维码的imageView赋值

    self.QRCodeImageView.image =

    [info objectForKey:UIImagePickerControllerOriginalImage];

    

//    [reader dismissModalViewControllerAnimated: YES];//被弃用

    [reader dismissViewControllerAnimated:YEScompletion:nil];

    

    //判断是否包含'http:'

    NSString *regex =@"http+:[^\\s]*";

   //谓语

   NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex];

    

    //判断是否包含'ssid:'

    NSString *ssid =@"ssid+:[^\\s]*";;

   NSPredicate *ssidPre = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",ssid];

    

   self.showResultLabel.text =  symbol.data ;

    NSLog(@"symbol.data = %@", symbol.data);

    if ([predicateevaluateWithObject:self.showResultLabel.text]) {//Returns a Boolean value that indicates whether a given object matches the conditions specified by the receiver.

        

       UIAlertView * alert = [[UIAlertViewalloc]initWithTitle:nil

                                                       message:@"It will use the browser to this URL"

                                                      delegate:nil

                                             cancelButtonTitle:@"Close"

                                             otherButtonTitles:@"Ok",nil];

        alert.delegate =self;

        alert.tag=1;//设置是那个alertView

        [alertshow];

    }

   else if([ssidPreevaluateWithObject:self.showResultLabel.text]){

        

        NSArray *arr = [self.showResultLabel.textcomponentsSeparatedByString:@";"];

        

        NSArray * arrInfoHead = [[arrobjectAtIndex:0]componentsSeparatedByString:@":"];

        

        NSArray * arrInfoFoot = [[arrobjectAtIndex:1]componentsSeparatedByString:@":"];

        

        

        self.showResultLabel.text=

        [NSStringstringWithFormat:@"ssid: %@ \n password:%@",

         [arrInfoHeadobjectAtIndex:1],[arrInfoFootobjectAtIndex:1]];

        

        

       UIAlertView * alert = [[UIAlertViewalloc]initWithTitle:self.showResultLabel.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;

        [alertshow];

        

       UIPasteboard *pasteboard=[UIPasteboardgeneralPasteboard];

        //       然后,可以使用如下代码来把一个字符串放置到剪贴板上:

        pasteboard.string = [arrInfoFootobjectAtIndex:1];

    }

}


0 0