среда, 18 мая 2016 г.

QR code scanner in your Android app

If you wont to use QR code scanner in your Android app you facing with many problems and errors. Now I show how to use the QR code scanner "zxing" library project and avoiding errors.

This is 12 simple steps how to use "zxing". ( I use the Eclipse because it is fastest and flexible than AS)

1. Create the basic Android project.

2. Download "zixing" project from there:  https://github.com/zxing/zxing  and extract this:


3. Import the Capture activity from zxing-master\android  folder:


Now you can see the CaptureActivity in you workspace with a lot of errors, lets go to fix this:

4. Create the libs (not lib!!!) folder in CaptureActivity project:

5. Mark this folder as source folder:

6. find the com folder in  zxing-master\core\src\main\java folder and drag to the libs folder:

7. Now you can see a lot of errors, lets fix this: open CaptureActivity project properties and change Java compiler settings to 1.7:

now all errors is disappeared except one:

lets go to fix this:
8. find the com folder in  zxing-master\android-core\src\main\java folder and drag to the libs folder:

now you have no errors!
9. Go to the CaptureActivity project properties and mark this as a library:
now you can use this lib project as QR code scanner library in your app:

10. Connect this lib to your project:

11. Now you can call this lib from you project like this:

     Intent intent = new Intent(this, CaptureActivity.class);
     intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
     startActivityForResult(intent, 0);

results is appearing after capturing in onActivityResult method:


12. also manifes file need some little changes:

           <uses-permission android:name="android.permission.CAMERA" />

          <intent-filter>
                <action android:name="com.google.zxing.client.android.SCAN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

       <activity
            android:name="com.google.zxing.client.android.CaptureActivity"
            android:label="@string/app_name"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.DeviceDefault.NoActionBar.Fullscreen" >
        </activity>


that's all!

-Alex  (6 years Android developer experience)