iOS開發(fā)-二維碼識別與生成-QRCode

前言

有關(guān)二維碼的介紹玷室,我這里不做過多說明, 可以直接去基維百科查看压状,附上鏈接QR code.
IOS7之前碉输,開發(fā)者進行掃碼編程時,一般會借助第三方庫裙顽。常用的是ZBarSDKaZXingObjC付燥,IOS7之后,系統(tǒng)的AVMetadataObject類中愈犹,為我們提供了解析二維碼的接口键科。經(jīng)過測試,使用原生API掃描和處理的效率非常高甘萧,遠遠高于第三方庫萝嘁。

掃描

官方提供的接口非常簡單,直接看代碼扬卷,主要使用的是AVFoundation牙言。

@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate>//用于處理采集信息的代理
{
    AVCaptureSession * session;//輸入輸出的中間橋梁
}
@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //獲取攝像設(shè)備
    AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    //創(chuàng)建輸入流
    AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
    if (!input) return;
    //創(chuàng)建輸出流
    AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc]init];
    //設(shè)置代理 在主線程里刷新
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    //設(shè)置有效掃描區(qū)域
    CGRect scanCrop=[self getScanCrop:_scanWindow.bounds readerViewBounds:self.view.frame];
     output.rectOfInterest = scanCrop;
    //初始化鏈接對象
    _session = [[AVCaptureSession alloc]init];
    //高質(zhì)量采集率
    [_session setSessionPreset:AVCaptureSessionPresetHigh];

    [_session addInput:input];
    [_session addOutput:output];
    //設(shè)置掃碼支持的編碼格式(如下設(shè)置條形碼和二維碼兼容)
    output.metadataObjectTypes=@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];

    AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
    layer.videoGravity=AVLayerVideoGravityResizeAspectFill;
    layer.frame=self.view.layer.bounds;
    [self.view.layer insertSublayer:layer atIndex:0];
    //開始捕獲
    [_session startRunning];
}

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
    if (metadataObjects.count>0) {
        //[session stopRunning];
        AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ];
        //輸出掃描字符串
        NSLog(@"%@",metadataObject.stringValue);
    }
}

一些初始化的代碼加上實現(xiàn)代理方法便完成了二維碼掃描的工作,這里我們需要注意的是怪得, 在二維碼掃描的時候咱枉, 我們一般都會在屏幕中間放一個方框卑硫,用來顯示二維碼掃描的大小區(qū)間,這里我們在個AVCaptureMetadataOutput類中有一個rectOfInterest屬性蚕断,它的作用就是設(shè)置掃描范圍欢伏。

這個CGRect參數(shù)和普通的Rect范圍不太一樣,它的四個值的范圍都是0-1亿乳,表示比例硝拧。
rectOfInterest都是按照橫屏來計算的 所以當豎屏的情況下 x軸和y軸要交換一下。
寬度和高度設(shè)置的情況也是類似葛假。

我們在上面設(shè)置有效掃描區(qū)域的方法如下

#pragma mark-> 獲取掃描區(qū)域的比例關(guān)系
-(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)readerViewBounds
{

    CGFloat x,y,width,height;

    x = (CGRectGetHeight(readerViewBounds)-CGRectGetHeight(rect))/2/CGRectGetHeight(readerViewBounds);
    y = (CGRectGetWidth(readerViewBounds)-CGRectGetWidth(rect))/2/CGRectGetWidth(readerViewBounds);
    width = CGRectGetHeight(rect)/CGRectGetHeight(readerViewBounds);
    height = CGRectGetWidth(rect)/CGRectGetWidth(readerViewBounds);

    return CGRectMake(x, y, width, height);

}

讀取

讀取主要用到CoreImage 不過要強調(diào)的是讀取二維碼的功能只有在iOS8之后才支持障陶,我們需要在相冊中調(diào)用一個二維碼,將其讀取聊训,代碼如下

#pragma mark-> 我的相冊
-(void)myAlbum{

    NSLog(@"我的相冊");
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
        //1.初始化相冊拾取器
        UIImagePickerController *controller = [[UIImagePickerController alloc] init];
        //2.設(shè)置代理
        controller.delegate = self;
        //3.設(shè)置資源:
        /**
         UIImagePickerControllerSourceTypePhotoLibrary,相冊
         UIImagePickerControllerSourceTypeCamera,相機
         UIImagePickerControllerSourceTypeSavedPhotosAlbum,照片庫
         */
        controller.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        //4.隨便給他一個轉(zhuǎn)場動畫
        controller.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
        [self presentViewController:controller animated:YES completion:NULL];

    }else{

        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"設(shè)備不支持訪問相冊抱究,請在設(shè)置->隱私->照片中進行設(shè)置!" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
        [alert show];
    }

}

完成相冊代理带斑, 我們在代理中添加讀取二維碼方法

#pragma mark-> imagePickerController delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //1.獲取選擇的圖片
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    //2.初始化一個監(jiān)測器
    CIDetector*detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{ CIDetectorAccuracy : CIDetectorAccuracyHigh }];

    [picker dismissViewControllerAnimated:YES completion:^{
        //監(jiān)測到的結(jié)果數(shù)組
        NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:image.CGImage]];
        if (features.count >=1) {
            /**結(jié)果對象 */
            CIQRCodeFeature *feature = [features objectAtIndex:0];
            NSString *scannedResult = feature.messageString;
            UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"掃描結(jié)果" message:scannedResult delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
            [alertView show];

        }
        else{
            UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"該圖片沒有包含一個二維碼鼓寺!" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
            [alertView show];

        }

    }];

}

生成

生成二維碼,其實也是用到CoreImage勋磕,但是步驟繁瑣一些妈候,代碼如下

#pragma mark-> 二維碼生成
-(void)create{

    UIImage *image=[UIImage imageNamed:@"6824500_006_thumb.jpg"];
    NSString*tempStr;
    if(self.textField.text.length==0){

        tempStr=@"ddddddddd";

    }else{

        tempStr=self.textField.text;

    }
    UIImage*tempImage=[QRCodeGenerator qrImageForString:tempStr imageSize:360 Topimg:image withColor:RandomColor];

    _outImageView.image=tempImage;

}
+(UIImage*)qrImageForString:(NSString *)string imageSize:(CGFloat)size Topimg:(UIImage *)topimg withColor:(UIColor*)color{

    if (![string length]) {
        return nil;
    }

    QRcode *code = QRcode_encodeString([string UTF8String], 0, QR_ECLEVEL_L, QR_MODE_8, 1);
    if (!code) {
        return nil;
    }

    // create context
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef ctx = CGBitmapContextCreate(0, size, size, 8, size * 4, colorSpace, kCGImageAlphaPremultipliedLast);

    CGAffineTransform translateTransform = CGAffineTransformMakeTranslation(0, -size);
    CGAffineTransform scaleTransform = CGAffineTransformMakeScale(1, -1);
    CGContextConcatCTM(ctx, CGAffineTransformConcat(translateTransform, scaleTransform));

    // draw QR on this context
    [QRCodeGenerator drawQRCode:code context:ctx size:size withPointType:0 withPositionType:0 withColor:color];

    // get image
    CGImageRef qrCGImage = CGBitmapContextCreateImage(ctx);
    UIImage * qrImage = [UIImage imageWithCGImage:qrCGImage];

    if(topimg)
    {
        UIGraphicsBeginImageContext(qrImage.size);

        //Draw image2
        [qrImage drawInRect:CGRectMake(0, 0, qrImage.size.width, qrImage.size.height)];

        //Draw image1
        float r=qrImage.size.width*35/240;
        [topimg drawInRect:CGRectMake((qrImage.size.width-r)/2, (qrImage.size.height-r)/2 ,r, r)];

        qrImage=UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();
    }

    // some releases
    CGContextRelease(ctx);
    CGImageRelease(qrCGImage);
    CGColorSpaceRelease(colorSpace);
    QRcode_free(code);

    return qrImage;

}
+ (void)drawQRCode:(QRcode *)code context:(CGContextRef)ctx size:(CGFloat)size withPointType:(QRPointType)pointType withPositionType:(QRPositionType)positionType withColor:(UIColor *)color {
    unsigned char *data = 0;
    int width;
    data = code->data;
    width = code->width;
    float zoom = (double)size / (code->width + 2.0 * qr_margin);
    CGRect rectDraw = CGRectMake(0, 0, zoom, zoom);

    // draw
    const CGFloat *components;
    if (color) {
        components = CGColorGetComponents(color.CGColor);
    }else {
        components = CGColorGetComponents([UIColor blackColor].CGColor);
    }
    CGContextSetRGBFillColor(ctx, components[0], components[1], components[2], 1.0);
    NSLog(@"aad :%f  bbd :%f   ccd:%f",components[0],components[1],components[2]);

    for(int i = 0; i < width; ++i) {
        for(int j = 0; j < width; ++j) {
            if(*data & 1) {
                rectDraw.origin = CGPointMake((j + qr_margin) * zoom,(i + qr_margin) * zoom);
                if (positionType == QRPositionNormal) {
                    switch (pointType) {
                        case QRPointRect:
                            CGContextAddRect(ctx, rectDraw);
                            break;
                        case QRPointRound:
                            CGContextAddEllipseInRect(ctx, rectDraw);
                            break;
                        default:
                            break;
                    }
                }else if(positionType == QRPositionRound) {
                    switch (pointType) {
                        case QRPointRect:
                            CGContextAddRect(ctx, rectDraw);
                            break;
                        case QRPointRound:
                            if ((i>=0 && i<=6 && j>=0 && j<=6) || (i>=0 && i<=6 && j>=width-7-1 && j<=width-1) || (i>=width-7-1 && i<=width-1 && j>=0 && j<=6)) {
                                CGContextAddRect(ctx, rectDraw);
                            }else {
                                CGContextAddEllipseInRect(ctx, rectDraw);
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
            ++data;
        }
    }
    CGContextFillPath(ctx);
}

長按二維碼識別

這個功能有很多的地方在用, 最讓人熟知的我想便是微信了挂滓,其實實現(xiàn)方法還是很簡單的州丹。

 #pragma mark-> 長按識別二維碼
-(void)dealLongPress:(UIGestureRecognizer*)gesture{

    if(gesture.state==UIGestureRecognizerStateBegan){

        _timer.fireDate=[NSDate distantFuture];

        UIImageView*tempImageView=(UIImageView*)gesture.view;
        if(tempImageView.image){
            //1\. 初始化掃描儀,設(shè)置設(shè)別類型和識別質(zhì)量
            CIDetector*detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{ CIDetectorAccuracy : CIDetectorAccuracyHigh }];
            //2\. 掃描獲取的特征組
            NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:tempImageView.image.CGImage]];
            //3\. 獲取掃描結(jié)果
            CIQRCodeFeature *feature = [features objectAtIndex:0];
            NSString *scannedResult = feature.messageString;
            UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"掃描結(jié)果" message:scannedResult delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
            [alertView show];
        }else {

            UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"掃描結(jié)果" message:@"您還沒有生成二維碼" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
            [alertView show];
        }

    }else if (gesture.state==UIGestureRecognizerStateEnded){

        _timer.fireDate=[NSDate distantPast];
    }

}

結(jié)語

轉(zhuǎn)自mokey1422所寫的仿支付寶二維碼杂彭。
系統(tǒng)原生的二維碼掃描掃描識別速度,要比第三方好用得多吓揪,在沒有特殊原因的情況下亲怠,比如7.0系統(tǒng)以下,我希望大家都能用系統(tǒng)原生的方法柠辞。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末团秽,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子叭首,更是在濱河造成了極大的恐慌习勤,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,681評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件焙格,死亡現(xiàn)場離奇詭異图毕,居然都是意外死亡,警方通過查閱死者的電腦和手機眷唉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,205評論 3 399
  • 文/潘曉璐 我一進店門予颤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來囤官,“玉大人,你說我怎么就攤上這事蛤虐〉骋” “怎么了?”我有些...
    開封第一講書人閱讀 169,421評論 0 362
  • 文/不壞的土叔 我叫張陵驳庭,是天一觀的道長刑顺。 經(jīng)常有香客問我,道長饲常,這世上最難降的妖魔是什么蹲堂? 我笑而不...
    開封第一講書人閱讀 60,114評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮不皆,結(jié)果婚禮上贯城,老公的妹妹穿的比我還像新娘。我一直安慰自己霹娄,他們只是感情好能犯,可當我...
    茶點故事閱讀 69,116評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著犬耻,像睡著了一般踩晶。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上枕磁,一...
    開封第一講書人閱讀 52,713評論 1 312
  • 那天渡蜻,我揣著相機與錄音,去河邊找鬼计济。 笑死茸苇,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的沦寂。 我是一名探鬼主播学密,決...
    沈念sama閱讀 41,170評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼传藏!你這毒婦竟也來了腻暮?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,116評論 0 277
  • 序言:老撾萬榮一對情侶失蹤毯侦,失蹤者是張志新(化名)和其女友劉穎哭靖,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體侈离,經(jīng)...
    沈念sama閱讀 46,651評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡试幽,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,714評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了霍狰。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片抡草。...
    茶點故事閱讀 40,865評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡饰及,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出康震,到底是詐尸還是另有隱情燎含,我是刑警寧澤,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布腿短,位于F島的核電站屏箍,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏橘忱。R本人自食惡果不足惜赴魁,卻給世界環(huán)境...
    茶點故事閱讀 42,211評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望钝诚。 院中可真熱鬧颖御,春花似錦、人聲如沸凝颇。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,699評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽拧略。三九已至芦岂,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間垫蛆,已是汗流浹背禽最。 一陣腳步聲響...
    開封第一講書人閱讀 33,814評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留袱饭,地道東北人川无。 一個月前我還...
    沈念sama閱讀 49,299評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像虑乖,于是被迫代替她去往敵國和親舀透。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,870評論 2 361

推薦閱讀更多精彩內(nèi)容

  • 序言 前面我們已經(jīng)調(diào)到過怎么制作二維碼决左,在我們能夠生成二維碼之后,如何對二維碼進行掃描呢走贪? 在iOS7之前佛猛,大部分...
    sindri的小巢閱讀 31,510評論 44 112
  • 178 money is not the prime asset in life ,time is
    123逍遙游閱讀 245評論 0 0
  • 或許因為年輕,我曾一次次不珍惜自己的生命坠狡,喜歡玩继找,追劇,浪費時間逃沿。老天對我很好婴渡,沒有因此奪走我年輕的生命幻锁。2017...
    燃后呢閱讀 275評論 0 1