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:
Free bar code is zbar, is: http://zbar.sourceforge.net
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 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];
}
Effect:
1) scanning process
2) result output