@blog.justoneplanet.info

日々勉強

ZXingをXcodeプロジェクトに組み込む

■組込

zxing-x.x/iphone/ZXingWidget/を自身のプロジェクトのプロジェクトナビゲーターにドロップする。

Targets > Build Phases

Target DependenciesでZXingWidgetを追加した後、Link Binary With Librariesで以下を追加する。

  • libZXingWidget.a
  • libiconv.dylib
  • CoreMedia.framework
  • CoreVideo.framework
  • AVFoundation.framework
  • AudioToolbox.framework
  • AddressBook.framework
  • AddressBookUI.framework
  • CoreGraphics.framework

Project > Build Settings

Header Search Pathsに以下の項目を追加する。

  • /User/[your name]/zxing-x.x/iphone/ZXingWidget/Classes
  • /User/[your name]/zxing-x.x/cpp/core/src

前者はrecursiveにチェックを入れて、後者はチェックを入れない。

参考

■実装

ScanViewController.h

#import <UIKit/UIKit.h>
#import "ZXingWidgetController.h"

@interface ScanViewController : UIViewController<ZXingDelegate>{
    UITextView *resultView;
    NSString *resultStr;
}
@property (nonatomic, retain) UITextView *resultView;
@property (nonatomic, copy) NSString *resultStr;
- (void)scanPressed:(id)sender;
@end

ScanViewController.mm

ファイルの拡張子を.mmとしないとコンパイルできない。

#import "ScanViewController.h"
#import "QRCodeReader.h"

@implementation ScanViewController
@synthesize resultView;
@synthesize resultStr;

- (void)scanPressed:(id)sender
{
    ZXingWidgetController *widController = [[ZXingWidgetController alloc] initWithDelegate:self showCancel:YES OneDMode:NO];
    QRCodeReader* qrcodeReader = [[QRCodeReader alloc] init];
    NSSet *readers = [[NSSet alloc ] initWithObjects:qrcodeReader,nil];
    [qrcodeReader release];
    widController.readers = readers;
    [readers release];

    [self presentModalViewController:widController animated:YES];
    [widController release];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}
- (void)dealloc {
    [resultView release];
}

#pragma mark - View lifecycle
- (void)loadView
{
    [super loadView];
    
    UIButton *btnStamp = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btnStamp setFrame:CGRectMake(40, 220, 240, 50)];
    [btnStamp setTitle:@"QRコードを読み取る" forState:UIControlStateNormal];
    [btnStamp setTag:1];
    [btnStamp addTarget:self action:@selector(scanPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnStamp];
    
    resultView = [[UITextView alloc] initWithFrame:CGRectMake(40 , 280, 240, 50)];
    [self.view addSubview:resultView];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark ZXingDelegateMethods
- (void)zxingController:(ZXingWidgetController*)controller didScanResult:(NSString *)result {
    self.resultStr = result;
    if (self.isViewLoaded) {
        [resultView setText:resultStr];
        [resultView setNeedsDisplay];
    }
    [self dismissModalViewControllerAnimated:NO];
}
- (void)zxingControllerDidCancel:(ZXingWidgetController*)controller {
    [self dismissModalViewControllerAnimated:YES];
}
@end

こんな感じで。

1件のコメント»

RSS feed for comments on this post.TrackBack URL

Leave a comment