环境: I5、4G Win7旗舰版 64 Vm 8.0 Mac OS X Lion 10.7.2 VMware Image Xcode 4.3 IOS SDK 5.0 Ipad2 5.1.1 参考: We will develop a very simple app that presents a button the user can tap to invoke the barcode reader and then displays the results. Interface Builder will be used to create the interface. The completed project is also available with the distributed SDK under Examples/ReaderSample. 1.2.1. Create the App Open Xcode; you must have version 3.2.3 or later. Create a new project using the “View-based Application” template. Name the project “ReaderSample”. Save it wherever you like. Open ReaderSampleViewController.xib Drag a Round Rect Button onto the view and title it “Scan”. Customize the placement and appearance as you like. Drag an Image View onto the view. Size it to fill about half of the remaining space. Change the view mode to Aspect Fit. Drag a Text View onto the view and size it to fill the remaining space. Change the default text to “No barcode scanned” or something. De-select “Editable” Add connections to the interface elements in the code; open ReaderSampleViewController.h and change the interface to: @interface ReaderSampleViewController : UIViewController { UIImageView *resultimage; UITextView *resultText; } @property (nonatomic,retain) IBOutlet UIImageView *resultimage; @property (nonatomic,retain) IBOutlet UITextView *resultText; - (IBAction) scanButtonTapped; @end Now we can finish the interface connections - open ReaderSampleViewController.xib and make these connections: Connect ReaderSampleViewController resultimage outlet to the ImageView. Connect ReaderSampleViewController resultText outlet to the TextView. Connect ReaderSampleViewController scanButtonTapped action to the RoundedRectButton(Scan) event TouchUpInside. Consult the Xcode documentation if you need help making these connections. Make sure you save the XIB once they are finished. Finish the implementation in ReaderSampleViewController.m: @synthesize resultimage,resultText; - (IBAction) scanButtonTapped { NSLog(@"TBD: scan barcode here..."); } - (void) dealloc { self.resultimage = nil; self.resultText = nil; [super dealloc]; } - (BOOL) shouldAutorotatetoInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation { return(YES); } This stub for scanButtonTapped is temporary,we’ll fix it in a minute... Although it doesn’t do much yet,you should Now have a working skeleton app that you can build and run. 1.2.2. Integrate the Reader Now for the exciting part - let’s add a barcode reader! If you have not done so already,download the latest SDK from http://zbar.sourceforge.net/iphone Double-click the disk image,ZBarSDK-1.2.dmg in the Finder to open it. Drag the ZBarSDK folder into your Xcode project. Make sure that the “copy Items into destination group’s folder” checkBox is checked. Open the target build settings and find Link Binary With Libraries. Click the + and add each of these (NB hold down command for multiple selection): AVFoundation.framework CoreMedia.framework CoreVideo.framework QuartzCore.framework libiconv.dylib Warning Link order may be important for some versions of Xcode; the libraries referenced above should be listed before libzbar.a in the link order. Import the SDK header. You will usually want to prefix it,so add it to ReaderSample-prefix.pch: // ADD: import barcode reader APIs #import "ZBarSDK.h" Declare support for the delegate protocol in ReaderSampleViewController.h: @interface ReaderSampleViewController : UIViewController // ADD: delegate protocol < ZBarReaderDelegate > { ... Re-implement scanButtonTapped to present a barcode reader when the user taps the Scan button. In ReaderSampleViewController.m: - (IBAction) scanButtonTapped { // ADD: present a barcode reader that scans from the camera Feed ZBarReaderViewController *reader = [ZBarReaderViewController new]; reader.readerDelegate = self; reader.supportedOrientationsMask = ZBarOrientationMaskAll; 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]; } Finally,implement the delegate method to do something useful with the results. Still in ReaderSampleViewController.m: - (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediawithInfo: (NSDictionary*) info { // ADD: get the decode results id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults]; ZBarSymbol *symbol = nil; for(symbol in results) // EXAMPLE: just grab the first barcode break; // EXAMPLE: do something useful with the barcode data resultText.text = symbol.data; // EXAMPLE: do something useful with the barcode image resultimage.image = [info objectForKey: UIImagePickerControllerOriginalImage]; // ADD: dismiss the controller (NB dismiss from the *reader*!) [reader dismissModalViewControllerAnimated: YES]; } And that’s it! 1.2.3. Testing Save everything (don’t forget to save MyAppViewController.xib). Build and Run the project. Tap the Scan button. aim at barcode. Enjoy the sweet fruits of your minimal labor 1.2.4. Where to go from here You can learn more about using the reader APIs to scan barcodes from Scanning From the Camera Feed or Scanning a User-Selected Image. Use the API Reference to find details about a particular interface. 1.2.5. Troubleshooting We take great care to ensure this tutorial is working as described. However,if you do have a problem Make sure you followed the instructions exactly - every detail is important. Start from scratch with a new project and follow the instructions exactly. Try the ReaderSample distributed with the SDK and compare your work with that. If you are unable to get things working,you may post your frustrations in the project iPhone Developers Forum. Please be very specific about your problem,post the complete text of any errors,etc.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。