When we develop ios applications, especially e-commerce applications, we often encounter bar code scanning business needs. Fortunately, we already have open-source sdks for us-bar code sdks for ios, the following describes how to use this open-source SDK:
The specific implementation process is as follows:
Step 1: Download The zbar sdk.
Step 2: Create a project
Step 3: add the downloaded ZBarSDK to the project.
Step 4: Add. framework to the project.
Fifth, add the header file referenced by the Library
Sixth, add a button to control the scan function.
Code implementation:
1) Add the ZBarReaderDelegate protocol to the. h file.
2) In the. m implementation file, implement the following code:
// Enable scanning and call the iphone camera
-(IBAction) scanButtonTapped
{
ZBarReaderViewController * reader = [ZBarReaderViewController new];
Reader. readerDelegate = self;
Zbarimageworkflow * reader. Category = reader. category;
[Export setSymbology: ZBAR_I25
Config: zbar_assist_enable
To: 0];
[Self presentModalViewController: reader animated: YES];
[Reader release];
}
// Proxy method
-(Void) imagePickerController: (UIImagePickerController *) reader
DidFinishPickingMediaWithInfo: (NSDictionary *) info
{
NSLog (@ "info = % @", info );
// Obtain the bar code result
Id <NSFastEnumeration> results =
[Info objectForKey: ZBarReaderControllerResults];
ZBarSymbol * symbol = nil;
For (symbol in results)
// EXAMPLE: just grab the first barcode
Break;
// Display the obtained barcode to our interface
ResultText. text = symbol. data;
// The scanned image is displayed on our interface.
ResultImage. image =
[Info objectForKey: UIImagePickerControllerOriginalImage];
// Exit the scan interface
[Reader dismissModalViewControllerAnimated: YES];
}