iOS 二維碼

來源:互聯網
上載者:User

標籤:

基本上我們做二維碼的實現方法有三大途徑:ZBar、ZXing、AVFoundation.

第一種:ZBar

         1.下載ZBarSDK,並且匯入到工程裡面

         2.匯入依賴庫:

             QuartzCore.framework、CoreVideo.framework、CoreMedia.framework、AVFoundation.framework

 

              3.如果報does not contain bitcode,請點擊這裡

              4.Zbar有兩種方式:

                            1>方式是常規的方式,通過ZBarReaderViewController直接開啟一個掃描介面進行掃描。

                                       首先引用標頭檔 #import"ZBarSDK.h",實現代理<ZBarReaderDelegate> 

                                          //初始化掃描二維碼控制器

                                         ZBarReaderViewController *reader = [ZBarReaderViewController new];

                                           //設定代理

                                         reader.readerDelegate = self;

                                          //二維碼/條碼識別設定

                                          ZBarImageScanner *scanner = reader.scanner;

                                           [scanner setSymbology: ZBAR_I25

                                                             config: ZBAR_CFG_ENABLE

                                                                to: 0];

                                        //彈出系統照相機,全屏拍攝

                                        [self presentViewController:reader animated:YES completion:nil];

 

 

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

 

                                        {

 

                                            id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults];

 

                                            ZBarSymbol * symbol;

 

                                             for(symbol in results)

 

                                              break;

 

                                            self.imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];

 

                                           [picker dismissViewControllerAnimated:YES completion:nil];  

 

                                           self.label.text = symbol.data;

                                        註:symbol.data就是掃描二維碼之後的值,[info objectForKey:UIImagePickerControllerOriginalImage]是掃描二維碼之後的圖片

 

                                           }

 

 

 

                                     2>方式是自訂掃描介面的方式。即通過自訂ZBarReaderView的介面達到自訂的效果。

                                     首先引用標頭檔 #import"ZBarSDK.h",實現代理<ZBarReaderViewDelegate>

                                     初始化掃描二維碼對象ZBarReaderView

                                      _readview = [[ZBarReaderView alloc] init];

                                      //設定掃描代理

                                    _readview.readerDelegate = self;

                                     //關閉閃光燈

                                    _readview.torchMode = 0;

                                  //顯示幀率

                                   _readview.showsFPS = NO;

                                 //將其照相機拍攝視圖添加到要顯示的視圖上

                                 [self.view addSubview:_readview];

                               //二維碼/條碼識別設定

                              ZBarImageScanner *scanner = _readview.scanner;

                               [scanner setSymbology: ZBAR_I25

                                                     config: ZBAR_CFG_ENABLE

                                                     to: 0];

                              

                            //掃描二維碼的時候,識別成功會進入此方法,讀取二維碼內容 

                         - (void) readerView: (ZBarReaderView*) readerView  didReadSymbols: (ZBarSymbolSet*) symbols   fromImage: (UIImage*) image

                      {

                       ZBarSymbol * symbol =nil;

                       for(symbol in symbols)

                     break;

                       註:symbol.data就是掃描二維碼之後的值,image是掃描二維碼之後的圖片

                      }

 

 當產生二維碼的時候:

    首先匯入libqrencode這個架構,將#import "QRCodeGenerator.h"導進來

             返回的image = [QRCodeGenerator qrImageForString:@‘‘字串"imageSize:testImageView.bounds.size.width];

當然也可以進行儲存到相簿

//儲存二維碼圖片到相簿

-(void)saveImageToAlbum

{

    UIImageWriteToSavedPhotosAlbum(返回的image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);

}

 

//儲存圖片回調方法

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *) error contextInfo:(void *)contextInfo

{

    if(error != NULL)

    {

        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"請開啟應用的相簿許可權" message:nil preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

        }];

        [alertVC addAction:action];

        [self presentViewController:alertVC animated:YES completion:^{

        }];

    }

    else

    {

        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"已儲存到相簿" message:nil preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

        }];

        [alertVC addAction:action];

        [self presentViewController:alertVC animated:YES completion:^{

        }];

    }

    

}

 

 

 

iOS 二維碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.