Implementing a QR reader in a cocos2d engine

2019-02-25 03:58发布

问题:

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!

回答1:

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];


回答2:

In cocos2d-iphone, the CCDirector IS the UIViewController.

So, just do this:

[[CCDirector sharedDirector] presentModalViewController:reader animated:YES]


回答3:

UIView* glView = [CCDirector sharedDirector].view;
UIView* window = glView.superview;

[window addSubview:reader.view];