在iOS上實現二維碼功能

來源:互聯網
上載者:User

標籤:android   blog   http   io   color   ar   os   使用   java   

  http://blog.csdn.net/abcmx/article/details/8011904

  如今二維碼隨處可見,無論是實物商品還是各種禮券都少不了二維碼的身影。而手機等行動裝置又成為二維碼的一個很好的應用平台,不管是產生二維碼還是掃碼二維碼。

下面介紹一下如何在蘋果iOS裝置上使用二維碼:

 

1. 掃描二維碼(解碼)
掃描二維碼的開源庫有很多如 ZBar、ZXing等。在此以ZBar作為例子構建二維碼掃碼應用。
首先在github上下載ZBar SDK
地址https://github.com/bmorton/ZBarSDK
開啟壓縮包,其中有ZBarSDK 檔案夾
將ZBarSDK 檔案夾包含到項目中來
(Finder)
(XCode)
其中包含一個libzbar.a的靜態庫
接著往項目中添加Framework架構及連結庫(動態庫、靜態庫)。在項目屬性TARGETS?Summary 中找到Linked Frameworks and Libraries
添加
AVFoundation.framwork, CoreMedia.framework, CoreVideo.framework, libiconv.dylib 和libzbar.a ()


然後在使用的地方引入標頭檔
#import "ZBarSDK.h"
調用ZBar的類必須實現ZBarReaderDelegate協議
如:UIViewController <ZBarReaderDelegate>
在- (void) imagePickerController: (UIImagePickerController*) reader
didFinishPickingMediaWithInfo: (NSDictionary*) info
最後在協議方法中通過info擷取結果值

[cpp] view plaincopy
  1. id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];  
  2. ZBarSymbol *symbol = nil;  
  3. for(symbol in results)  
  4. // EXAMPLE: just grab the first barcode   
  5. break;  
  6.   
  7. // EXAMPLE: do something useful with the barcode data   
  8. resultText.text = symbol.data;  
[cpp] view plaincopy
  1. id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];  
  2. ZBarSymbol *symbol = nil;  
  3. for(symbol in results)  
  4. // EXAMPLE: just grab the first barcode  
  5. break;  
  6.   
  7. // EXAMPLE: do something useful with the barcode data  
  8. resultText.text = symbol.data;  


文檔地址http://zbar.sourceforge.net/iphone/sdkdoc/
二維碼開源庫ZXing的載入過程與此類似,但是ZXing預設並不支援一維碼的掃描,而ZBar是支援的。

2. 產生二維碼(編碼)
在產生二維碼的庫中QREncoder 比較好用。ZXing庫的android版本是帶java版的編碼庫的但不知為何沒有C++版的解碼庫,故在ios上也沒法解碼,如果應用中既要產生二維碼又要掃描二維碼就有可能要帶兩套不同的庫。
首先同樣是在github 上下載到SDK庫
地址https://github.com/jverkoey/ObjQREncoder
解壓後將源碼庫直接包含到項目中來()

設定標頭檔路徑,在TARGET中building settings ? Search Path

當然也可以直接將庫編譯成靜態庫libQREcoder.a然後在包含進來

接著在Frameworks 中添加靜態庫的引用


然後在使用該庫的地方包含標頭檔
#import <QREncoder/QREncoder.h>
注意:二維碼顯示的時候有可能會因為映像的大小而做調整,所以需要包含系統庫QuartzCore.framework
最後就可以調用編碼庫了:

 

[cpp] view plaincopy
  1. UIImage* image = [QREncoder encode:@"http://www.baidu.com/"];  
[cpp] view plaincopy
  1. UIImage* image = [QREncoder encode:@"http://www.baidu.com/"];  



 

在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.