Android-based Barcode upload Applet

Source: Internet
Author: User

Recently want to develop a simple program based on Android Barcode plugin, but in the network can not find a simple Demo, so in English website https://code.google.com/p/zxing/) Find ah, find Ah ~ I finally figured it out. It was so simple, but the troubleshooting process was quite tangled ~ So remember it here.


1. Download required resources

110000barcodescanner4.4.apk this software is usually pre-installed on our devices, but if not, it needs to be installed. Note that you may not be able to install the pre-installed barcodepipeline because it is for this reason that the pre-installed barcodepipeline cannot be uninstalled or overwritten, I was so depressed that I couldn't test it on a real machine for a long time.

2、ZXing-2.2.zip contains the zxing source code and some demo programs. Our program will need to use the classes in it.


2. Create an Android Application Project

This process is very simple, and two components are required in the interface: TextView is responsible for displaying scan results, and Button is responsible for generating scan actions.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/0635302950-0.png "title =" 5554Test_2013-07-26_15-06-15.png "/>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".BarCodeScannActivity" >    <TextView        android:id="@+id/tv_result"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello_world" />                                                                                                                                                                                                 <Button        android:id="@+id/btn_scann"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/tv_result"        android:text="Scanning" /></RelativeLayout>

3. introduce tools

We will introduce two tool classes to download the zxing-2.2.zip file:

E: \ TOOLS \ ZXING \ zxing-1.6 \ android-integration \ src \ com \ google \ zxing \ integration \ android \IntentIntegrator. java

E: \ TOOLS \ ZXING \ zxing-1.6 \ android-integration \ src \ com \ google \ zxing \ integration \ android \IntentResult. java

These two classes are officially encapsulated for us. We can simply copy these two classes to the src directory of the newly created Project according to the package level:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/0635302313-1.png "title =" Java-MyBarCodeScannerreslayoutactivity_bar_code_scann.png "/>

4. Main class for accessing the application portal: BarCodeScannActivity. java

Public class BarCodeScannActivity extends Activity {private TextView tvResult; private Button btnScann; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_bar_code_scann); this. tvResult = (TextView) findViewById (R. id. TV _result); this. btnScann = (Button) findViewById (R. id. btn_scann); this. btnScann. setOnClickListener (ne W View. onClickListener () {@ Override public void onClick (View v) {// by observing the IntentIntegrator code, it is found that the constructor is private, at the same time, initiateScan of the "Trigger" scanner is also a static method, so it can be called directly. IntentIntegrator. initiateScan (BarCodeScannActivity. this) ;}}) ;}@ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {// return and parse the scan result IntentResult scanResult = IntentIntegrator. parseActivityResult (requestCode, resultCode, data); if (scanResult! = Null) {this. tvResult. setText ("Contents:" + scanResult. getContents () + "\ n" + "Format:" + scanResult. getFormatName ());}}}

5. I also directly Copy the official document to add permissions and features)

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.panny.mybarcodescanner"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <uses-permission android:name="android.permission.CAMERA" />    <uses-permission android:name="android.permission.INTERNET" />    <uses-permission android:name="android.permission.VIBRATE" />    <uses-permission android:name="android.permission.FLASHLIGHT" />    <uses-permission android:name="android.permission.READ_CONTACTS" />    <uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />    <uses-feature        android:name="android.hardware.camera"        android:required="false" />    <uses-feature        android:name="android.hardware.camera.front"        android:required="false" />    <!-- TODO replace above two with next line after Android 4.2 -->    <!-- <uses-feature android:name="android.hardware.camera.any"/> -->    <uses-feature        android:name="android.hardware.camera.autofocus"        android:required="false" />    <uses-feature        android:name="android.hardware.camera.flash"        android:required="false" />    <uses-feature android:name="android.hardware.screen.landscape" />    <uses-feature        android:name="android.hardware.wifi"        android:required="false" />    <!-- This excludes Google TV, which is unfortunately included by virtue of not requiring a camera -->    <uses-feature android:name="android.hardware.touchscreen" />    <!-- TODO make this not required again after android.hardware.camera.any is available -->    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.panny.mybarcodescanner.BarCodeScannActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

6. The code is complete and cannot run on the simulator. Release it to the device. Scan this book to see it ~


Summary:

This Demo can only be used as a simple demonstration. If you want to comply with the production scenario, you need to carefully read other important source code, such:

E: \ TOOLS \ ZXING \ zxing-1.6 \ android-integration \ src \ com \ google \ zxing \ integration \ android \IntentIntegrator. java

E: \ TOOLS \ ZXING \ zxing-1.6 \ android-integration \ src \ com \ google \ zxing \ integration \ android \IntentResult. java

E: \ TOOLS \ ZXING \ zxing-1.6 \ android \ src \ com \ google \ zxing \ client \ android \Intents. java

E: \ TOOLS \ ZXING \ zxing-1.6 \ android \ src \ com \ google \ zxing \ client \ android \CaptureActivity. java

This article from "Good Lonely Mr" blog, please be sure to keep this source http://pannyhjm.blog.51cto.com/4473736/1258193

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.