diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..32abb38
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,39 @@
+## Adopted from https://github.com/github/gitignore/blob/master/Android.gitignore
+
+# Built application files
+*.apk
+*.ap_
+
+# Files for the Dalvik VM
+*.dex
+
+# Java class files
+*.class
+
+# Generated files
+bin/
+gen/
+
+# Local configuration file (sdk path, etc)
+local.properties
+# Our Custom properties file
+config.properties
+
+# Eclipse generated files
+.classpath
+.project
+.directory
+.settings/
+.metadata/
+
+# Idea generated files
+*.iml
+.idea
+out
+
+# Netbeans generated files
+private
+nbandroid
+
+# MacOSX files
+.DS_Store
diff --git a/examples/ZBarScannerDemo/AndroidManifest.xml b/examples/ZBarScannerDemo/AndroidManifest.xml
new file mode 100644
index 0000000..266007c
--- /dev/null
+++ b/examples/ZBarScannerDemo/AndroidManifest.xml
@@ -0,0 +1,24 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/ZBarScannerDemo/ant.properties b/examples/ZBarScannerDemo/ant.properties
new file mode 100644
index 0000000..b0971e8
--- /dev/null
+++ b/examples/ZBarScannerDemo/ant.properties
@@ -0,0 +1,17 @@
+# This file is used to override default values used by the Ant build system.
+#
+# This file must be checked into Version Control Systems, as it is
+# integral to the build system of your project.
+
+# This file is only used by the Ant script.
+
+# You can use this to override default values such as
+# 'source.dir' for the location of your java source folder and
+# 'out.dir' for the location of your output folder.
+
+# You can also use it define how the release builds are signed by declaring
+# the following properties:
+# 'key.store' for the location of your keystore and
+# 'key.alias' for the name of the key to use.
+# The password will be asked during the build when you use the 'release' target.
+
diff --git a/examples/ZBarScannerDemo/build.properties b/examples/ZBarScannerDemo/build.properties
new file mode 100644
index 0000000..e69de29
diff --git a/examples/ZBarScannerDemo/build.xml b/examples/ZBarScannerDemo/build.xml
new file mode 100644
index 0000000..64a8e0f
--- /dev/null
+++ b/examples/ZBarScannerDemo/build.xml
@@ -0,0 +1,92 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/ZBarScannerDemo/default.properties b/examples/ZBarScannerDemo/default.properties
new file mode 100644
index 0000000..e69de29
diff --git a/examples/ZBarScannerDemo/proguard-project.txt b/examples/ZBarScannerDemo/proguard-project.txt
new file mode 100644
index 0000000..f2fe155
--- /dev/null
+++ b/examples/ZBarScannerDemo/proguard-project.txt
@@ -0,0 +1,20 @@
+# To enable ProGuard in your project, edit project.properties
+# to define the proguard.config property as described in that file.
+#
+# Add project specific ProGuard rules here.
+# By default, the flags in this file are appended to flags specified
+# in ${sdk.dir}/tools/proguard/proguard-android.txt
+# You can edit the include path and order by changing the ProGuard
+# include property in project.properties.
+#
+# For more details, see
+# http://developer.android.com/guide/developing/tools/proguard.html
+
+# Add any project specific keep options here:
+
+# If your project uses WebView with JS, uncomment the following
+# and specify the fully qualified class name to the JavaScript interface
+# class:
+#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
+# public *;
+#}
diff --git a/examples/ZBarScannerDemo/project.properties b/examples/ZBarScannerDemo/project.properties
new file mode 100644
index 0000000..1d519f2
--- /dev/null
+++ b/examples/ZBarScannerDemo/project.properties
@@ -0,0 +1,15 @@
+# This file is automatically generated by Android Tools.
+# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
+#
+# This file must be checked in Version Control Systems.
+#
+# To customize properties used by the Ant build system edit
+# "ant.properties", and override values to adapt the script to your
+# project structure.
+#
+# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
+#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
+
+# Project target.
+target=android-16
+android.library.reference.1=../..
diff --git a/examples/ZBarScannerDemo/res/drawable-hdpi/ic_launcher.png b/examples/ZBarScannerDemo/res/drawable-hdpi/ic_launcher.png
new file mode 100644
index 0000000..96a442e
Binary files /dev/null and b/examples/ZBarScannerDemo/res/drawable-hdpi/ic_launcher.png differ
diff --git a/examples/ZBarScannerDemo/res/drawable-ldpi/ic_launcher.png b/examples/ZBarScannerDemo/res/drawable-ldpi/ic_launcher.png
new file mode 100644
index 0000000..9923872
Binary files /dev/null and b/examples/ZBarScannerDemo/res/drawable-ldpi/ic_launcher.png differ
diff --git a/examples/ZBarScannerDemo/res/drawable-mdpi/ic_launcher.png b/examples/ZBarScannerDemo/res/drawable-mdpi/ic_launcher.png
new file mode 100644
index 0000000..359047d
Binary files /dev/null and b/examples/ZBarScannerDemo/res/drawable-mdpi/ic_launcher.png differ
diff --git a/examples/ZBarScannerDemo/res/drawable-xhdpi/ic_launcher.png b/examples/ZBarScannerDemo/res/drawable-xhdpi/ic_launcher.png
new file mode 100644
index 0000000..71c6d76
Binary files /dev/null and b/examples/ZBarScannerDemo/res/drawable-xhdpi/ic_launcher.png differ
diff --git a/examples/ZBarScannerDemo/res/layout/main.xml b/examples/ZBarScannerDemo/res/layout/main.xml
new file mode 100644
index 0000000..cccca70
--- /dev/null
+++ b/examples/ZBarScannerDemo/res/layout/main.xml
@@ -0,0 +1,22 @@
+
+
+
+
+
+
+
diff --git a/examples/ZBarScannerDemo/res/values/strings.xml b/examples/ZBarScannerDemo/res/values/strings.xml
new file mode 100644
index 0000000..bfd4a31
--- /dev/null
+++ b/examples/ZBarScannerDemo/res/values/strings.xml
@@ -0,0 +1,6 @@
+
+
+ ZBar Scanner Demo
+ Scan
+ Scan only QR Codes
+
diff --git a/examples/ZBarScannerDemo/src/com/dm/zbar/android/examples/MainActivity.java b/examples/ZBarScannerDemo/src/com/dm/zbar/android/examples/MainActivity.java
new file mode 100644
index 0000000..d6807fd
--- /dev/null
+++ b/examples/ZBarScannerDemo/src/com/dm/zbar/android/examples/MainActivity.java
@@ -0,0 +1,58 @@
+package com.dm.zbar.android.examples;
+
+import android.app.Activity;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.os.Bundle;
+import android.view.View;
+import android.widget.Toast;
+import com.dm.zbar.android.scanner.ZBarConstants;
+import com.dm.zbar.android.scanner.ZBarScannerActivity;
+
+public class MainActivity extends Activity {
+
+ private static final int ZBAR_SCANNER_REQUEST = 0;
+ private static final int ZBAR_QR_SCANNER_REQUEST = 1;
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.main);
+ }
+
+ public void launchScanner(View v) {
+ if (isCameraAvailable()) {
+ Intent intent = new Intent(this, ZBarScannerActivity.class);
+ startActivityForResult(intent, ZBAR_SCANNER_REQUEST);
+ } else {
+ Toast.makeText(this, "Rear Facing Camera Unavailable", Toast.LENGTH_SHORT).show();
+ }
+ }
+
+ public void launchQRScanner(View v) {
+ if (isCameraAvailable()) {
+ Intent intent = new Intent(this, ZBarScannerActivity.class);
+ intent.putExtra(ZBarScannerActivity.SCAN_MODES, new int[]{ZBarConstants.QRCODE});
+ startActivityForResult(intent, ZBAR_SCANNER_REQUEST);
+ } else {
+ Toast.makeText(this, "Rear Facing Camera Unavailable", Toast.LENGTH_SHORT).show();
+ }
+ }
+
+ public boolean isCameraAvailable() {
+ PackageManager pm = getPackageManager();
+ return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
+ }
+
+ @Override
+ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+ switch (requestCode) {
+ case ZBAR_SCANNER_REQUEST:
+ case ZBAR_QR_SCANNER_REQUEST:
+ if (resultCode == RESULT_OK) {
+ Toast.makeText(this, "Scan Result = " + data.getStringExtra(ZBarConstants.SCAN_RESULT), Toast.LENGTH_SHORT).show();
+ }
+ break;
+ }
+ }
+}
diff --git a/library/AndroidManifest.xml b/library/AndroidManifest.xml
new file mode 100644
index 0000000..5d5925b
--- /dev/null
+++ b/library/AndroidManifest.xml
@@ -0,0 +1,16 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/library/ant.properties b/library/ant.properties
new file mode 100644
index 0000000..b0971e8
--- /dev/null
+++ b/library/ant.properties
@@ -0,0 +1,17 @@
+# This file is used to override default values used by the Ant build system.
+#
+# This file must be checked into Version Control Systems, as it is
+# integral to the build system of your project.
+
+# This file is only used by the Ant script.
+
+# You can use this to override default values such as
+# 'source.dir' for the location of your java source folder and
+# 'out.dir' for the location of your output folder.
+
+# You can also use it define how the release builds are signed by declaring
+# the following properties:
+# 'key.store' for the location of your keystore and
+# 'key.alias' for the name of the key to use.
+# The password will be asked during the build when you use the 'release' target.
+
diff --git a/library/build.properties b/library/build.properties
new file mode 100644
index 0000000..e69de29
diff --git a/library/build.xml b/library/build.xml
new file mode 100644
index 0000000..b72a8d3
--- /dev/null
+++ b/library/build.xml
@@ -0,0 +1,92 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/library/default.properties b/library/default.properties
new file mode 100644
index 0000000..e69de29
diff --git a/library/libs/armeabi-v7a/libiconv.so b/library/libs/armeabi-v7a/libiconv.so
new file mode 100644
index 0000000..2bcbb70
Binary files /dev/null and b/library/libs/armeabi-v7a/libiconv.so differ
diff --git a/library/libs/armeabi-v7a/libzbarjni.so b/library/libs/armeabi-v7a/libzbarjni.so
new file mode 100644
index 0000000..2693dbb
Binary files /dev/null and b/library/libs/armeabi-v7a/libzbarjni.so differ
diff --git a/library/libs/armeabi/libiconv.so b/library/libs/armeabi/libiconv.so
new file mode 100644
index 0000000..9c7150d
Binary files /dev/null and b/library/libs/armeabi/libiconv.so differ
diff --git a/library/libs/armeabi/libzbarjni.so b/library/libs/armeabi/libzbarjni.so
new file mode 100644
index 0000000..3d4a8ac
Binary files /dev/null and b/library/libs/armeabi/libzbarjni.so differ
diff --git a/library/libs/x86/libiconv.so b/library/libs/x86/libiconv.so
new file mode 100644
index 0000000..6ab43e5
Binary files /dev/null and b/library/libs/x86/libiconv.so differ
diff --git a/library/libs/x86/libzbarjni.so b/library/libs/x86/libzbarjni.so
new file mode 100644
index 0000000..d64f517
Binary files /dev/null and b/library/libs/x86/libzbarjni.so differ
diff --git a/library/libs/zbar.jar b/library/libs/zbar.jar
new file mode 100644
index 0000000..7d50b95
Binary files /dev/null and b/library/libs/zbar.jar differ
diff --git a/library/proguard-project.txt b/library/proguard-project.txt
new file mode 100644
index 0000000..f2fe155
--- /dev/null
+++ b/library/proguard-project.txt
@@ -0,0 +1,20 @@
+# To enable ProGuard in your project, edit project.properties
+# to define the proguard.config property as described in that file.
+#
+# Add project specific ProGuard rules here.
+# By default, the flags in this file are appended to flags specified
+# in ${sdk.dir}/tools/proguard/proguard-android.txt
+# You can edit the include path and order by changing the ProGuard
+# include property in project.properties.
+#
+# For more details, see
+# http://developer.android.com/guide/developing/tools/proguard.html
+
+# Add any project specific keep options here:
+
+# If your project uses WebView with JS, uncomment the following
+# and specify the fully qualified class name to the JavaScript interface
+# class:
+#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
+# public *;
+#}
diff --git a/library/project.properties b/library/project.properties
new file mode 100644
index 0000000..dfa4dd0
--- /dev/null
+++ b/library/project.properties
@@ -0,0 +1,15 @@
+# This file is automatically generated by Android Tools.
+# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
+#
+# This file must be checked in Version Control Systems.
+#
+# To customize properties used by the Ant build system edit
+# "ant.properties", and override values to adapt the script to your
+# project structure.
+#
+# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
+#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
+
+# Project target.
+target=android-16
+android.library=true
diff --git a/library/res/drawable-hdpi/ic_launcher.png b/library/res/drawable-hdpi/ic_launcher.png
new file mode 100644
index 0000000..96a442e
Binary files /dev/null and b/library/res/drawable-hdpi/ic_launcher.png differ
diff --git a/library/res/drawable-ldpi/ic_launcher.png b/library/res/drawable-ldpi/ic_launcher.png
new file mode 100644
index 0000000..9923872
Binary files /dev/null and b/library/res/drawable-ldpi/ic_launcher.png differ
diff --git a/library/res/drawable-mdpi/ic_launcher.png b/library/res/drawable-mdpi/ic_launcher.png
new file mode 100644
index 0000000..359047d
Binary files /dev/null and b/library/res/drawable-mdpi/ic_launcher.png differ
diff --git a/library/res/drawable-xhdpi/ic_launcher.png b/library/res/drawable-xhdpi/ic_launcher.png
new file mode 100644
index 0000000..71c6d76
Binary files /dev/null and b/library/res/drawable-xhdpi/ic_launcher.png differ
diff --git a/library/res/values/strings.xml b/library/res/values/strings.xml
new file mode 100644
index 0000000..e73122b
--- /dev/null
+++ b/library/res/values/strings.xml
@@ -0,0 +1,4 @@
+
+
+ ZBar Scanner
+
diff --git a/library/src/com/dm/zbar/android/scanner/CameraPreview.java b/library/src/com/dm/zbar/android/scanner/CameraPreview.java
new file mode 100644
index 0000000..75dc92f
--- /dev/null
+++ b/library/src/com/dm/zbar/android/scanner/CameraPreview.java
@@ -0,0 +1,173 @@
+package com.dm.zbar.android.scanner;
+
+import android.content.Context;
+import android.hardware.Camera;
+import android.hardware.Camera.PreviewCallback;
+import android.hardware.Camera.Size;
+import android.util.Log;
+import android.view.SurfaceHolder;
+import android.view.SurfaceView;
+import android.view.View;
+import android.view.ViewGroup;
+import java.io.IOException;
+import java.util.List;
+
+class CameraPreview extends ViewGroup implements SurfaceHolder.Callback {
+ private final String TAG = "CameraPreview";
+
+ SurfaceView mSurfaceView;
+ SurfaceHolder mHolder;
+ Size mPreviewSize;
+ List mSupportedPreviewSizes;
+ Camera mCamera;
+ PreviewCallback mPreviewCallback;
+
+ CameraPreview(Context context, PreviewCallback previewCallback) {
+ super(context);
+
+ mPreviewCallback = previewCallback;
+ mSurfaceView = new SurfaceView(context);
+ addView(mSurfaceView);
+
+ // Install a SurfaceHolder.Callback so we get notified when the
+ // underlying surface is created and destroyed.
+ mHolder = mSurfaceView.getHolder();
+ mHolder.addCallback(this);
+ mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
+ }
+
+ public void setCamera(Camera camera) {
+ mCamera = camera;
+ if (mCamera != null) {
+ mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
+ requestLayout();
+ }
+ }
+
+ public void switchCamera(Camera camera) {
+ setCamera(camera);
+ try {
+ camera.setPreviewDisplay(mHolder);
+ } catch (IOException exception) {
+ Log.e(TAG, "IOException caused by setPreviewDisplay()", exception);
+ }
+ Camera.Parameters parameters = camera.getParameters();
+ parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
+ requestLayout();
+
+ camera.setParameters(parameters);
+ }
+
+ @Override
+ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+ // We purposely disregard child measurements because act as a
+ // wrapper to a SurfaceView that centers the camera preview instead
+ // of stretching it.
+ final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
+ final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
+ setMeasuredDimension(width, height);
+
+ if (mSupportedPreviewSizes != null) {
+ mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
+ }
+ }
+
+ @Override
+ protected void onLayout(boolean changed, int l, int t, int r, int b) {
+ if (changed && getChildCount() > 0) {
+ final View child = getChildAt(0);
+
+ final int width = r - l;
+ final int height = b - t;
+
+ int previewWidth = width;
+ int previewHeight = height;
+ if (mPreviewSize != null) {
+ previewWidth = mPreviewSize.width;
+ previewHeight = mPreviewSize.height;
+ }
+
+ // Center the child SurfaceView within the parent.
+ if (width * previewHeight > height * previewWidth) {
+ final int scaledChildWidth = previewWidth * height / previewHeight;
+ child.layout((width - scaledChildWidth) / 2, 0,
+ (width + scaledChildWidth) / 2, height);
+ } else {
+ final int scaledChildHeight = previewHeight * width / previewWidth;
+ child.layout(0, (height - scaledChildHeight) / 2,
+ width, (height + scaledChildHeight) / 2);
+ }
+ }
+ }
+
+ public void surfaceCreated(SurfaceHolder holder) {
+ // The Surface has been created, acquire the camera and tell it where
+ // to draw.
+ try {
+ if (mCamera != null) {
+ mCamera.setPreviewDisplay(holder);
+ }
+ } catch (IOException exception) {
+ Log.e(TAG, "IOException caused by setPreviewDisplay()", exception);
+ }
+ }
+
+ public void surfaceDestroyed(SurfaceHolder holder) {
+ // Surface will be destroyed when we return, so stop the preview.
+ if (mCamera != null) {
+ mCamera.stopPreview();
+ }
+ }
+
+
+ private Size getOptimalPreviewSize(List sizes, int w, int h) {
+ final double ASPECT_TOLERANCE = 0.1;
+ double targetRatio = (double) w / h;
+ if (sizes == null) return null;
+
+ Size optimalSize = null;
+ double minDiff = Double.MAX_VALUE;
+
+ int targetHeight = h;
+
+ // Try to find an size match aspect ratio and size
+ for (Size size : sizes) {
+ double ratio = (double) size.width / size.height;
+ if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
+ if (Math.abs(size.height - targetHeight) < minDiff) {
+ optimalSize = size;
+ minDiff = Math.abs(size.height - targetHeight);
+ }
+ }
+
+ // Cannot find the one match the aspect ratio, ignore the requirement
+ if (optimalSize == null) {
+ minDiff = Double.MAX_VALUE;
+ for (Size size : sizes) {
+ if (Math.abs(size.height - targetHeight) < minDiff) {
+ optimalSize = size;
+ minDiff = Math.abs(size.height - targetHeight);
+ }
+ }
+ }
+ return optimalSize;
+ }
+
+ public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
+ if (holder.getSurface() == null){
+ // preview surface does not exist
+ return;
+ }
+
+ // Now that the size is known, set up the camera parameters and begin
+ // the preview.
+ Camera.Parameters parameters = mCamera.getParameters();
+ parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
+ requestLayout();
+
+ mCamera.setParameters(parameters);
+ mCamera.setPreviewCallback(mPreviewCallback);
+ mCamera.startPreview();
+ }
+
+}
diff --git a/library/src/com/dm/zbar/android/scanner/ZBarConstants.java b/library/src/com/dm/zbar/android/scanner/ZBarConstants.java
new file mode 100644
index 0000000..11a9b7e
--- /dev/null
+++ b/library/src/com/dm/zbar/android/scanner/ZBarConstants.java
@@ -0,0 +1,26 @@
+package com.dm.zbar.android.scanner;
+
+public interface ZBarConstants {
+ public static final String SCAN_MODES = "SCAN_MODES";
+ public static final String SCAN_RESULT = "SCAN_RESULT";
+ public static final String SCAN_RESULT_TYPE = "SCAN_RESULT_TYPE";
+
+ // NOTE: Constants directly copied from net.sourceforge.zbar.Symbol
+ public static final int NONE = 0;
+ public static final int PARTIAL = 1;
+ public static final int EAN8 = 8;
+ public static final int UPCE = 9;
+ public static final int ISBN10 = 10;
+ public static final int UPCA = 12;
+ public static final int EAN13 = 13;
+ public static final int ISBN13 = 14;
+ public static final int I25 = 25;
+ public static final int DATABAR = 34;
+ public static final int DATABAR_EXP = 35;
+ public static final int CODABAR = 38;
+ public static final int CODE39 = 39;
+ public static final int PDF417 = 57;
+ public static final int QRCODE = 64;
+ public static final int CODE93 = 93;
+ public static final int CODE128 = 128;
+}
diff --git a/library/src/com/dm/zbar/android/scanner/ZBarScannerActivity.java b/library/src/com/dm/zbar/android/scanner/ZBarScannerActivity.java
new file mode 100644
index 0000000..dbc4525
--- /dev/null
+++ b/library/src/com/dm/zbar/android/scanner/ZBarScannerActivity.java
@@ -0,0 +1,109 @@
+package com.dm.zbar.android.scanner;
+
+import android.app.Activity;
+import android.content.Intent;
+import android.hardware.Camera;
+import android.os.Bundle;
+import android.text.TextUtils;
+import android.view.Window;
+import android.view.WindowManager;
+import net.sourceforge.zbar.Config;
+import net.sourceforge.zbar.Image;
+import net.sourceforge.zbar.ImageScanner;
+import net.sourceforge.zbar.Symbol;
+import net.sourceforge.zbar.SymbolSet;
+
+public class ZBarScannerActivity extends Activity implements Camera.PreviewCallback, ZBarConstants {
+
+ private static final String TAG = "ZBarScannerActivity";
+
+ private CameraPreview mPreview;
+ private Camera mCamera;
+ private ImageScanner mScanner;
+
+ static {
+ System.loadLibrary("iconv");
+ }
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ // Hide the window title.
+ requestWindowFeature(Window.FEATURE_NO_TITLE);
+ getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
+
+ // Create and configure the ImageScanner;
+ setupScanner();
+
+ // Create a RelativeLayout container that will hold a SurfaceView,
+ // and set it as the content of our activity.
+ mPreview = new CameraPreview(this, this);
+ setContentView(mPreview);
+ }
+
+ public void setupScanner() {
+ mScanner = new ImageScanner();
+ mScanner.setConfig(0, Config.X_DENSITY, 3);
+ mScanner.setConfig(0, Config.Y_DENSITY, 3);
+
+ int[] symbols = getIntent().getIntArrayExtra(SCAN_MODES);
+ if (symbols != null) {
+ mScanner.setConfig(Symbol.NONE, Config.ENABLE, 0);
+ for (int symbol : symbols) {
+ mScanner.setConfig(symbol, Config.ENABLE, 1);
+ }
+ }
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+
+ // Open the default i.e. the first rear facing camera.
+ mCamera = Camera.open();
+ mPreview.setCamera(mCamera);
+ }
+
+ @Override
+ protected void onPause() {
+ super.onPause();
+
+ // Because the Camera object is a shared resource, it's very
+ // important to release it when the activity is paused.
+ if (mCamera != null) {
+ mPreview.setCamera(null);
+ mCamera.setPreviewCallback(null);
+ mCamera.release();
+ mCamera = null;
+ }
+ }
+
+ public void onPreviewFrame(byte[] data, Camera camera) {
+ Camera.Parameters parameters = camera.getParameters();
+ Camera.Size size = parameters.getPreviewSize();
+
+ Image barcode = new Image(size.width, size.height, "Y800");
+ barcode.setData(data);
+
+ int result = mScanner.scanImage(barcode);
+
+ if (result != 0) {
+ mCamera.setPreviewCallback(null);
+ mCamera.stopPreview();
+
+ SymbolSet syms = mScanner.getResults();
+ for (Symbol sym : syms) {
+ String symData = sym.getData();
+ if (!TextUtils.isEmpty(symData)) {
+ Intent dataIntent = new Intent();
+ dataIntent.putExtra(SCAN_RESULT, symData);
+ dataIntent.putExtra(SCAN_RESULT_TYPE, sym.getType());
+ setResult(Activity.RESULT_OK, dataIntent);
+ finish();
+ break;
+ }
+ }
+ }
+ }
+}