For those trying to do the same, here is what I did.
-
Copy the files “MTBBarcodeScanner.h” & “MTBBarcodeScanner.m” into the project (drag and drop works fine for me).
-
Change the ‘weak’ words to ‘strong’. In this project, there was only 1 location.
-
To view the live-camera on screen, you will need to use the window in focus (not the runtime.appWindow).
Here is a code snippet, based on the project template ‘App’. There are some commented lines in here for testing / experimental purposes.
Thanks for Sergey for the QR code plugin and the idea as starting point.
#import "PluginLibrary.h" #include "CoronaRuntime.h" #import "MTBBarcodeScanner.h" #import \<UIKit/UIKit.h\> #import \<AVFoundation/AVFoundation.h\> .. Your other code here ... // [Lua] library.show( word ) int PluginLibrary::show( lua\_State \*L ) { NSString \*message = @"Error: Could not display UIReferenceLibraryViewController. This feature requires iOS 5 or later."; if ( [UIReferenceLibraryViewController class] ) { id\<CoronaRuntime\> runtime = (id\<CoronaRuntime\>)CoronaLuaGetContext( L ); const char kDefaultWord[] = "corona"; const char \*word = lua\_tostring( L, 1 ); if ( ! word ) { word = kDefaultWord; } UIReferenceLibraryViewController \*controller = [[[UIReferenceLibraryViewController alloc] initWithTerm:[NSString stringWithUTF8String:word]] autorelease]; // Present the controller modally. // [runtime.appViewController presentModalViewController:controller animated:YES]; CGRect newFrame = runtime.appWindow.frame; newFrame.size.width = 200; newFrame.size.height = 200; [runtime.appWindow setFrame:newFrame]; NSLog(@"Corona. Initialising the scanner...."); MTBBarcodeScanner \*scanner; // scanner = [[MTBBarcodeScanner alloc] initWithPreviewView:runtime.appWindow]; scanner = [[MTBBarcodeScanner alloc] initWithPreviewView:runtime.appViewController.preferredFocusedView]; UIView \*myBox = [[UIView alloc] initWithFrame:CGRectMake(150, 100, 100, 500)]; myBox.backgroundColor = [UIColor yellowColor]; [runtime.appWindow addSubview:myBox]; // Optionally set a rectangle of interest to scan codes. Only codes within this rect will be scanned. // runtime.appViewController.navigationController // scanner.scanRect = runtime.appViewController.viewOfInterest.frame; // // [runtime.appWindow.toggleScanningButton setTitle:@"Stop Scanning" forState:UIControlStateNormal]; // scanner.toggleScanningButton.backgroundColor = [UIColor redColor]; [MTBBarcodeScanner requestCameraPermissionWithSuccess:^(BOOL success) { if (success) { [scanner startScanningWithResultBlock:^(NSArray \*codes) { AVMetadataMachineReadableCodeObject \*code = [codes firstObject]; NSLog(@"Found code: %@", code.stringValue); [scanner stopScanning]; }]; } else { // The user denied access to the camera } }]; // UIImagePickerController \*picker = [[UIImagePickerController alloc] init]; // picker.delegate = controller; // picker.allowsEditing = YES; // picker.sourceType = UIImagePickerControllerSourceTypeCamera; // [runtime.appViewController presentViewController:picker animated:YES completion:NULL]; // [[scanner.previewLayer navigationController] presentModalViewController:controller animated:YES]; // scanner.didStartScanningBlock = ^{ // NSLog(@"Corona. The scanner started scanning! We can now hide any activity spinners."); // // UIReferenceLibraryViewController \*controller = [[[UIReferenceLibraryViewController alloc] initWithTerm:[NSString stringWithUTF8String:word]] autorelease]; // // //// UINavigationController \*navController = (UINavigationController\*)[segue destinationViewController]; //// EventsTableViewController \*eventsController = [navController topViewController]; // // // //// [[scanner.previewLayer runtime.appViewController.] presentModalViewController:controller animated:YES]; //// [[scanner.previewLayer navigationController] presentViewController:runtime.appViewController animated:YES]; //// [runtime.appViewController presentViewController:picker animated:YES completion:NULL]; // // UIView \*myBox = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 500)]; // myBox.backgroundColor = [UIColor magentaColor]; // [runtime.appWindow addSubview:myBox]; // // // // Self \*library = ToLibrary( L ); // // // Create event and add message to it // CoronaLuaNewEvent( L, kEvent ); // lua\_pushstring( L, [message UTF8String] ); // lua\_setfield( L, -2, "message" ); // // // Dispatch event to library's listener // CoronaLuaDispatchEvent( L, library-\>GetListener(), 0 ); // // // }; // scanner.resultBlock = ^(NSArray \*codes){ // NSLog(@"Corona. Found these codes: %@", codes); // }; // scanner.didTapToFocusBlock = ^(CGPoint point){ // NSLog(@"Corona. The user tapped the screen to focus. \ // Here we could present a view at %@", NSStringFromCGPoint(point)); // }; // //// scanner.previewLayer; message = @"Success. Displaying UIReferenceLibraryViewController for 'corona'."; } return 0; }