Hi ! 🙂 Most of you may have already worked with “Zxing” library with “CaptureActivity” to develop Bar-code or QR-code Scanner related projects. Today I am going to help you, in initiating a scanner project with new “zxingfragmentlib” project. The difference in this project is instead Activities here use Fragments. The project source is available at GitHub. “zxingfragmentlib” project uses gradle as the building mechanism. But I will be using maven in building the project in this example.

Download Source:
Command: git clone https://github.com/mitoyarzun/zxingfragmentlib
Prerequisites:
Project properties
- Java Compiler should be 1.7
Note: Otherwise you will have to do changes to ensure compatibility
- Android Compiling with: Android 4.4 (Kit Kat)
Create Maven project
Command: mvn archetype:generate -DarchetypeArtifactId=android-quickstart -DarchetypeGroupId=de.akquinet.android.archetypes -DarchetypeVersion=1.1.0 -DgroupId=com.testapp -DartifactId=my-scanner-app -Dversion=1.0
Use the Guide to create the maven project: (Creating Android Project with Maven Archetypes)
Note: You will see errors at pom.xml file in Eclipse, But you can deploy with following command using terminal.
Deploying Command: maven clean install android:deploy android:run

Changes to pom.xml file:
- Add dependencies for zxing core-2.2 and android-support-v4
- Add plugin for maven-compiler-plugin
POM.XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.testapp</groupId>
<artifactId>my-scanner-app</artifactId>
<version>1.0</version>
<packaging>apk</packaging>
<name>my-scanner-app</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<platform.version> 4.1.1.4
</platform.version>
<android.plugin.version>3.8.0</android.plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>${platform.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>android.support</groupId>
<artifactId>compatibility-v4</artifactId>
<version>19.1.0</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>${android.plugin.version}</version>
<extensions>true</extensions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<configuration>
<sdk>
<platform>16</platform>
</sdk>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
</pre>
<pre style="line-height: 13.5pt;"><span style="font-size: 9.0pt; font-family: Consolas; color: #333333;">
Changes to AndroidManifest.xml file:
- Add android:minSdkVersion=”15″
- Add required uses-permissions and uses-features
ANDROIDMANIFEST.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testapp"
android:versionCode="1"
android:versionName="1.0" >
<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-sdk android:minSdkVersion="15" android:targetSdkVersion="19"/>
<!-- Don't require camera, as this requires a rear camera. This allows it to work on the Nexus 7 -->
<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 -->
<supports-screens android:xlargeScreens="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>
<application
android:allowBackup="true"
android:label="@string/app_name" >
<activity
android:name="com.test.scan.ScanActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest></pre>
<pre style="line-height: 13.5pt;"><span style="font-size: 9.0pt; font-family: Consolas; color: #333333;">
Get Content from “zxing-android” to maven project:
1.Goto -> zxingfragmentlib –> zxing-android
This contains the main source and resources required

2. Goto–> zxing-android —> src —>main —>res
Copy the resource folder to the maven project

3. Goto–>zxing-android—>src–>main —>java—->com
Copy the com folder to the maven project

Now your project will contain res and src as follows

Note: The Errors will be gone once you change package names and R.java import
Note: If you haven’t change the Java Compile to 1.7 and Android compile to Android 4.4 the errors will be remained in the project. Therefore make sure they meet the requirements.
4. Create a new package com.test.scan in maven project
Goto–>library –>src–>main–>java–>com–>welcu–>android–>zxingfragmentlib
Copy all the files to the “com.test.scan” package

******Now you project will contain following source files.

ScanActivity.java
package com.test.scan;
import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
import com.testapp.R;
public class ScanActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager
.beginTransaction()
.replace(R.id.content_frame, new SampleFragment(),
getString(R.string.app_name)).commit();
}
}
SampleFragment.java
package com.test.scan;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.google.zxing.Result;
import com.testapp.R;
public class SampleFragment extends BarCodeScannerFragment {
private View mRoot;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("TAG", "Creating View");
mRoot = inflater.inflate(R.layout.scanning, container, false);
this.setmCallBack(new IResultCallback() {
@Override
public void result(Result lastResult) {
Toast.makeText(getActivity(), "Scan: " + lastResult.toString(),
Toast.LENGTH_SHORT).show();
}
});
return mRoot;
}
}
activity_sample.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</RelativeLayout>
scanning.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="fill-parent"
android:layout_height="fill-parent"
android:layout_gravity="center_horizontal" >
<include layout="@layout/capture" />
</FrameLayout>
</LinearLayout>
Create Your own QRCODE:
http://qrcode.kaywa.com/

Deploying….

Good Luck 🙂
Like this:
Like Loading...