The day before yesterday, I sent an article about the application of bar code recognition software for Android development.ProgramBlog, did not expect to be very popular.
I am also very encouraged. Well, I will continue to write down my blog.
Some people mentioned in the previous article that I have not written any substantive content. I accept the suggestions. Here I will show you the simplest and most direct perfect experience.
Write your first Android barcode recognition program. Simple use of zxing!
Step 1: Download The zxing component: I also briefly introduced this component in the previous article, and it is not too long here.
Download two things
Source code and documentation:
For example
Http://code.google.com/p/zxing/downloads/detail? Name‑zxing-1.6.zip & can = 2 & Q =
Barcodescanner3.5.apk this is a compiled and installable APK program! This will be used later.
The address is as follows:
Http://code.google.com/p/zxing/downloads/detail? Name‑barcodescanner3.51b1.apk & can = 2 & Q =
Step 2:
After the simulator is fully enabled
Install barcodescanner3.5.apk
CD in cmd to SDK directory
Install barcodescanner3.5.apk with the adbcommand
The directory where ADB install barcodescanner3.5.apk is located. Make sure the installation is successful.
Step 3: OK is finally encoded!
Code
ImportAndroid. App. activity;
ImportAndroid. content. intent;
ImportAndroid. OS. Bundle;
ImportAndroid. View. view;
ImportAndroid. widget. Button;
ImportAndroid. widget. textview;
PublicClassMytestExtendsActivity {
/**Called when the activity is first created.*/
PrivateTextview TV;
@ Override
PublicVoidOncreate (bundle savedinstancestate ){
Super. Oncreate (savedinstancestate );
Setcontentview (R. layout. mytest );
TV=(Textview) findviewbyid (R. Id. mytxt );
Findviewbyid (R. Id. mybtn). setonclicklistener (listener );
}
PublicButton. onclicklistener listener=NewButton. onclicklistener (){
PublicVoidOnclick (view v ){
Intent intent = New Intent ( " Com. Google. zxing. Client. Android. Scan " ); // The scan actity is actually transferred to one of the installed barcodescanner3 programs.
Intent. putextra ( " Scan_mode " , " Qr_code_mode " ); // Enter parameters (SCAN type,... QR code)
Startactivityforresult (intent, 0 ); // Start intent
}
};
// The callback function returns the code after the scan is successful.
Public Void Onactivityresult ( Int Requestcode, Int Resultcode, intent ){
If (Requestcode = 0 ){
If (Resultcode = Result_ OK ){
String Contents = Intent. getstringextra ( " Scan_result " );
String format = Intent. getstringextra ( " Scan_result_format " );
// Handle successful Scan
TV. settext ( " The barcode is: " + Contents + " The bar code type is: " + Format ); // Use textveiw on the page to display scan results
} Else If (Resultcode = Result_canceled ){
// Handle cancel
TV. settext ( " Scan failed! " );
}
}
}
}
The corresponding XML is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "@ string/Hello" Android: Id = "@ + ID/mytxt"/> <button Android: TEXT = "click I start scanning" Android: Id = "@ + ID/mybtn" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </button> </linearlayout>
Step 3: OK. Let's check the effect and run it.
You will see the following:
The simulator certainly does not have a camera device and cannot scan the result.
Summary:
Here is just a simple call. The call is simple, but there is a major defect. here we need to install two programs!
This is obviously unacceptable when installing barcodescanner3.5.apk! So I will continue to explore it in the next blog. Stay tuned!