iphone 二維碼 使用說明

來源:互聯網
上載者:User

 

轉載請註明出處

 

1、二維碼簡介

二維碼 (2-dimensional bar code) 是用某種特定的幾何圖形按一定規律在平面(二維方向上)分布的黑白相間的圖形記錄資料符號資訊的;在代碼編製上巧妙地利用構成電腦內部邏輯基礎的“0”、“1”位元流的概念,使用若干個與二進位相對應的幾何形體來表示文字數值資訊,通過圖象輸入裝置或光電掃描裝置自動識讀以實現資訊自動處理: 二維條碼/二維碼能夠在橫向和縱向兩個方位同時表達資訊,因此能在很小的面積內表達大量的資訊。

 

2、擷取iphone開原始碼工程

http://code.google.com/p/zxing/

首先我們工具上面的地址直接下載開源的代碼,詳見圖1

 

下載到本地以後解壓進入檔案夾可以看到 

iphone檔案夾-->(在點擊進入)

ScanTest(測試工程) 和 ZXingWidget(串連庫 .a)

 

3、編譯ZXingWidget並且在工程中調用

-------------------------------------------------------------------------------------

因為在4.0以上才有提供擷取網路攝影機資料的類 AVCaptureDeviceInput AVCaptureVideoDataOutput類等 (這樣我們在4.x上面才能自動對焦對二維碼進行解碼

,擷取二維碼的資訊)

 

-------------------------------------------------------------------------------------

 

其中在ZXingWidget靜態庫

ZXingWidgetController.h中為我們提供了 HAS_AVFF的判斷開關(預設為1)

這樣的話 ZXingWidget 工程只能 選擇 Base SDK 4.0 

2

下編譯才能通過

 

編譯完.a後那開啟ScanTest工程 一樣選擇 Base SDK 4.0 編譯工程就可以編譯運行成功

 

 

其中二維碼解碼的調用可以詳見

 

RootViewController.mm的檔案 可以看到 方法

//此方法就是調用 調用網路攝影機資料自動對焦解碼 

- (IBAction)scanPressed:(id)sender

{

//此處代碼 詳見工程

}

 

//以下delegate 方法返回 解碼結果

#pragma mark ZXingDelegateMethods

- (void)zxingController:(ZXingWidgetController*)controller didScanResult:(NSString *)result

{

}

 

/////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////

 

其實到這邊為止我們已經可以編譯的程式可以在4.x以上的機子運行了

但是這樣只能支援4.x 這裡我們難道不能支援3.x的機子碼?

 

其實是可以的但是我們要修改添加一些代碼

 

首先還是確保  靜態庫.a和調用程式 編譯試在 BADE SDK 4.0

 

但是要在調用程式如 ScanTest中要修改工程的設定

3、4

 

修改完設定後然後拷貝以下的代碼 替換 原來

scanPressed的方法

 

///////////////////////////////////////代碼區/////////////////////////////////////////////////

- (IBAction)scanPressed:(id)sender {

if (NSClassFromString(@"AVCaptureSession") && NSClassFromString(@"AVCaptureVideoPreviewLayer"))

{

MLOG(@"__IPHONE_4_0");

ZXingWidgetController *widController = [[ZXingWidgetController alloc] initWithDelegate:self showCancel:YES OneDMode:NO];

widController.view.backgroundColor = [UIColor blueColor];

QRCodeReader* qrcodeReader = [[QRCodeReader alloc] init];

NSSet *readers = [[NSSet alloc ] initWithObjects:qrcodeReader,nil];

[qrcodeReader release];

widController.readers = readers;

[readers release];

NSBundle *mainBundle = [NSBundle mainBundle];

widController.soundToPlay =

[NSURL fileURLWithPath:[mainBundle pathForResource:@"beep-beep" ofType:@"aiff"] isDirectory:NO];

[self presentModalViewController:widController animated:YES];

[widController release];

}

else//3.x以下的韌體系統*/

{

MLOG(@"__IPHONE_3_0");

UIImagePickerController *pickCtr = [[UIImagePickerController alloc] init];

pickCtr.sourceType = UIImagePickerControllerSourceTypeCamera;

pickCtr.delegate = self;

[self presentModalViewController:pickCtr animated:YES];

[pickCtr release];

}

}

 

#pragma mark -

#pragma mark UIImagePickerControllerDelegate

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

{

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

UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

QRCodeReader* qrcodeReader = [[QRCodeReader alloc] init];

NSSet *readers = [[NSSet alloc ] initWithObjects:qrcodeReader,nil];

[qrcodeReader release];

CGRect cropRect = [UIScreen mainScreen].applicationFrame;

Decoder *d = [[Decoder alloc] init];

d.readers = readers;

d.delegate = self;

int res = [d decodeImage:image];

MLOG(@"res === %d", res);

[d release];

[self dismissModalViewControllerAnimated:YES];

}

 

 

- (void)decoder:(Decoder *)decoder didDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset withResult:(TwoDDecoderResult *)twoDResult {

MLOG(@"twoDResult text ==== %@", [twoDResult text]);

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[twoDResult text] message:[twoDResult text] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

[alertView show];

[alertView release];

}

 

- (void)decoder:(Decoder *)decoder failedToDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset reason:(NSString *)reason {

MLOG(@"twoDResult text ==== failedToDecodeImage");

decoder.delegate = nil;

}

 

///////////////////////////////////////代碼區/////////////////////////////////////////////////

 

這樣在4.0編譯完以後就可以自動識別機子韌體調用不同的解碼方式了

其中 在4.0以上的我們試自動對焦解碼二維碼

在4.0以下的我們試調用照相的功能然後對拍出來的圖片進行解碼

 

 

4、自己類比二維碼測試資料

 

我們可以在瀏覽器中輸入

http://chart.apis.google.com/chart?cht=qr&chs=150x150&chl=http://www.hiapk.com

就可以看到看到5所示 google的開源二維碼

 

 

cht就是代表產生二維碼的類型

其中chs代表二維碼產生的大小 

chl就是代表二維碼顯示的資訊

 

******************************************

以上這些資訊都可以自己修改(最好不要修改cht 二維碼類型)然後用剛才編譯出的程式進行測試

正確的結果試會解碼出chl的內容

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.