Implementing a QR reader in a cocos2d engine

2019-02-25 03:10发布

So far I am using the cocos2d engine to create a game and I want to implement a QR reader.

So far I've created a CCScene, the CCScene has a menu button and it leads to a onScan Method:

-(void) onScan:(id)sender{
    // ADD: present a barcode reader that scans from the camera feed
    ZBarReaderViewController *reader = [ZBarReaderViewController new];
    reader.readerDelegate = self;

    ZBarImageScanner *scanner = reader.scanner;
    // TODO: (optional) additional reader configuration here

    // EXAMPLE: disable rarely used I2/5 to improve performance
    [scanner setSymbology: ZBAR_I25
                   config: ZBAR_CFG_ENABLE
                       to: 0];

    // present and release the controller
    [self presentModalViewController: reader
                            animated: YES];
    [reader release];
}

I understand that this works on a standard ViewController class but I am wondering how to best implement this to work with the cocos2d engine on my CCScene

Any help would be appreciated Thanks!

3条回答
smile是对你的礼貌
2楼-- · 2019-02-25 03:47
UIView* glView = [CCDirector sharedDirector].view;
UIView* window = glView.superview;

[window addSubview:reader.view];
查看更多
走好不送
3楼-- · 2019-02-25 04:02

If you come from a typical cocos2d-iphone project which was created using the Xcode template, I think you should have a RootViewController class in your app. The only instance of the RootViewController is created in applicationDidFinishLaunching: in AppDelegate.m.

viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];

You can somehow save a global reference of your root UIViewController, for example, to implement a class method in RootViewController:

static RootViewController *sharedInstance_ = nil;

@implementaion RootViewController

+ (RootViewController *)sharedInstance {
    if (!sharedInstance_) {
        sharedInstance_ = [[RootViewController alloc] initWithNibName:nil bundle:nil];
    }
    return sharedInstance_;
}

and change the initialization in AppDelegate.m to:

viewController = [[RootViewController sharedInstance] retain];

then use it to present your UIViewController:

[[RootViewController sharedInstance] presentModalViewController:reader
                                     animated:YES];
查看更多
姐就是有狂的资本
4楼-- · 2019-02-25 04:08

In cocos2d-iphone, the CCDirector IS the UIViewController.

So, just do this:

[[CCDirector sharedDirector] presentModalViewController:reader animated:YES]
查看更多
登录 后发表回答