Android now uses auto focus, assigns and enables all barcode types

This commit is contained in:
Nick German
2015-03-26 07:22:39 +10:00
commit bb3b82aace
41 changed files with 5422 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
package org.cloudsky.cordovaPlugins;
import net.sourceforge.zbar.Symbol;
import java.util.List;
import java.util.ArrayList;
public class BarcodeFormat {
private int mId;
private String mName;
public static final BarcodeFormat NONE = new BarcodeFormat(Symbol.NONE, "NONE");
public static final BarcodeFormat PARTIAL = new BarcodeFormat(Symbol.PARTIAL, "PARTIAL");
public static final BarcodeFormat EAN8 = new BarcodeFormat(Symbol.EAN8, "EAN8");
public static final BarcodeFormat UPCE = new BarcodeFormat(Symbol.UPCE, "UPCE");
public static final BarcodeFormat ISBN10 = new BarcodeFormat(Symbol.ISBN10, "ISBN10");
public static final BarcodeFormat UPCA = new BarcodeFormat(Symbol.UPCA, "UPCA");
public static final BarcodeFormat EAN13 = new BarcodeFormat(Symbol.EAN13, "EAN13");
public static final BarcodeFormat ISBN13 = new BarcodeFormat(Symbol.ISBN13, "ISBN13");
public static final BarcodeFormat I25 = new BarcodeFormat(Symbol.I25, "I25");
public static final BarcodeFormat DATABAR = new BarcodeFormat(Symbol.DATABAR, "DATABAR");
public static final BarcodeFormat DATABAR_EXP = new BarcodeFormat(Symbol.DATABAR_EXP, "DATABAR_EXP");
public static final BarcodeFormat CODABAR = new BarcodeFormat(Symbol.CODABAR, "CODABAR");
public static final BarcodeFormat CODE39 = new BarcodeFormat(Symbol.CODE39, "CODE39");
public static final BarcodeFormat PDF417 = new BarcodeFormat(Symbol.PDF417, "PDF417");
public static final BarcodeFormat QRCODE = new BarcodeFormat(Symbol.QRCODE, "QRCODE");
public static final BarcodeFormat CODE93 = new BarcodeFormat(Symbol.CODE93, "CODE93");
public static final BarcodeFormat CODE128 = new BarcodeFormat(Symbol.CODE128, "CODE128");
public static final List<BarcodeFormat> ALL_FORMATS = new ArrayList<BarcodeFormat>();
static {
ALL_FORMATS.add(BarcodeFormat.PARTIAL);
ALL_FORMATS.add(BarcodeFormat.EAN8);
ALL_FORMATS.add(BarcodeFormat.UPCE);
ALL_FORMATS.add(BarcodeFormat.ISBN10);
ALL_FORMATS.add(BarcodeFormat.UPCA);
ALL_FORMATS.add(BarcodeFormat.EAN13);
ALL_FORMATS.add(BarcodeFormat.ISBN13);
ALL_FORMATS.add(BarcodeFormat.I25);
ALL_FORMATS.add(BarcodeFormat.DATABAR);
ALL_FORMATS.add(BarcodeFormat.DATABAR_EXP);
ALL_FORMATS.add(BarcodeFormat.CODABAR);
ALL_FORMATS.add(BarcodeFormat.CODE39);
ALL_FORMATS.add(BarcodeFormat.PDF417);
ALL_FORMATS.add(BarcodeFormat.QRCODE);
ALL_FORMATS.add(BarcodeFormat.CODE93);
ALL_FORMATS.add(BarcodeFormat.CODE128);
}
public BarcodeFormat(int id, String name) {
mId = id;
mName = name;
}
public int getId() {
return mId;
}
public String getName() {
return mName;
}
public static BarcodeFormat getFormatById(int id) {
for(BarcodeFormat format : ALL_FORMATS) {
if(format.getId() == id) {
return format;
}
}
return BarcodeFormat.NONE;
}
}

78
android/ZBar.java Normal file
View File

@@ -0,0 +1,78 @@
package org.cloudsky.cordovaPlugins;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.content.Context;
import org.cloudsky.cordovaPlugins.ZBarScannerActivity;
public class ZBar extends CordovaPlugin {
// Configuration ---------------------------------------------------
private static int SCAN_CODE = 1;
// State -----------------------------------------------------------
private boolean isInProgress = false;
private CallbackContext scanCallbackContext;
// Plugin API ------------------------------------------------------
@Override
public boolean execute (String action, JSONArray args, CallbackContext callbackContext)
throws JSONException
{
if(action.equals("scan")) {
if(isInProgress) {
callbackContext.error("A scan is already in progress!");
} else {
isInProgress = true;
scanCallbackContext = callbackContext;
JSONObject params = args.optJSONObject(0);
Context appCtx = cordova.getActivity().getApplicationContext();
Intent scanIntent = new Intent(appCtx, ZBarScannerActivity.class);
scanIntent.putExtra(ZBarScannerActivity.EXTRA_PARAMS, params.toString());
cordova.startActivityForResult(this, scanIntent, SCAN_CODE);
}
return true;
} else {
return false;
}
}
// External results handler ----------------------------------------
@Override
public void onActivityResult (int requestCode, int resultCode, Intent result)
{
if(requestCode == SCAN_CODE) {
switch(resultCode) {
case Activity.RESULT_OK:
String barcodeValue = result.getStringExtra(ZBarScannerActivity.EXTRA_QRVALUE);
scanCallbackContext.success(barcodeValue);
break;
case Activity.RESULT_CANCELED:
scanCallbackContext.error("cancelled");
break;
case ZBarScannerActivity.RESULT_ERROR:
scanCallbackContext.error("Scan failed due to an error");
break;
default:
scanCallbackContext.error("Unknown error");
}
isInProgress = false;
scanCallbackContext = null;
}
}
}

View File

@@ -0,0 +1,392 @@
package org.cloudsky.cordovaPlugins;
import java.io.IOException;
import java.lang.RuntimeException;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.PreviewCallback;
import android.hardware.Camera.AutoFocusCallback;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import net.sourceforge.zbar.ImageScanner;
import net.sourceforge.zbar.Image;
import net.sourceforge.zbar.Symbol;
import net.sourceforge.zbar.SymbolSet;
import net.sourceforge.zbar.Config;
public class ZBarScannerActivity extends Activity
implements SurfaceHolder.Callback {
private Collection<BarcodeFormat> mFormats = null;
// Config ----------------------------------------------------------
private static int autoFocusInterval = 500; // Interval between AFcallback and next AF attempt.
// Public Constants ------------------------------------------------
public static final String EXTRA_QRVALUE = "qrValue";
public static final String EXTRA_PARAMS = "params";
public static final int RESULT_ERROR = RESULT_FIRST_USER + 1;
// State -----------------------------------------------------------
private Camera camera;
private Handler autoFocusHandler;
private SurfaceView scannerSurface;
private SurfaceHolder holder;
private ImageScanner scanner;
private int surfW, surfH;
// Customisable stuff
String whichCamera;
String flashMode;
// For retrieving R.* resources, from the actual app package
// (we can't use actual.application.package.R.* in our code as we
// don't know the applciation package name when writing this plugin).
private String package_name;
private Resources resources;
// Static initialisers (class) -------------------------------------
static {
// Needed by ZBar??
System.loadLibrary("iconv");
}
// Activity Lifecycle ----------------------------------------------
@Override
public void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Get parameters from JS
Intent startIntent = getIntent();
String paramStr = startIntent.getStringExtra(EXTRA_PARAMS);
JSONObject params;
try { params = new JSONObject(paramStr); }
catch (JSONException e) { params = new JSONObject(); }
String textTitle = params.optString("text_title");
String textInstructions = params.optString("text_instructions");
Boolean drawSight = params.optBoolean("drawSight", true);
whichCamera = params.optString("camera");
flashMode = params.optString("flash");
// Initiate instance variables
autoFocusHandler = new Handler();
scanner = new ImageScanner();
scanner.setConfig(0, Config.X_DENSITY, 3);
scanner.setConfig(0, Config.Y_DENSITY, 3);
for(BarcodeFormat format : getFormats()) {
scanner.setConfig(format.getId(), Config.ENABLE, 1);
}
// Set content view
setContentView(getResourceId("layout/cszbarscanner"));
// Update view with customisable strings
//TextView view_textTitle = (TextView) findViewById(getResourceId("id/csZbarScannerTitle"));
TextView view_textInstructions = (TextView) findViewById(getResourceId("id/csZbarScannerInstructions"));
//view_textTitle.setText(textTitle);
view_textInstructions.setText(textInstructions);
// Draw/hide the sight
if(!drawSight) {
findViewById(getResourceId("id/csZbarScannerSight")).setVisibility(View.INVISIBLE);
}
// Create preview SurfaceView
scannerSurface = new SurfaceView (this) {
@Override
public void onSizeChanged (int w, int h, int oldW, int oldH) {
surfW = w;
surfH = h;
matchSurfaceToPreviewRatio();
}
};
scannerSurface.setLayoutParams(new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
Gravity.CENTER
));
scannerSurface.getHolder().addCallback(this);
// Add preview SurfaceView to the screen
FrameLayout scannerView = (FrameLayout) findViewById(getResourceId("id/csZbarScannerView"));
scannerView.addView(scannerSurface);
//findViewById(getResourceId("id/csZbarScannerTitle")).bringToFront();
findViewById(getResourceId("id/csZbarScannerInstructions")).bringToFront();
findViewById(getResourceId("id/csZbarScannerSightContainer")).bringToFront();
findViewById(getResourceId("id/csZbarScannerSight")).bringToFront();
scannerView.requestLayout();
scannerView.invalidate();
}
@Override
public void onResume ()
{
super.onResume();
try {
if(whichCamera.equals("front")) {
int numCams = Camera.getNumberOfCameras();
CameraInfo cameraInfo = new CameraInfo();
for(int i=0; i<numCams; i++) {
Camera.getCameraInfo(i, cameraInfo);
if(cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {
camera = Camera.open(i);
}
}
} else {
camera = Camera.open();
}
if(camera == null) throw new Exception ("Error: No suitable camera found.");
} catch (RuntimeException e) {
die("Error: Could not open the camera.");
return;
} catch (Exception e) {
die(e.getMessage());
return;
}
Camera.Parameters camParams = camera.getParameters();
if(flashMode.equals("on")) {
camParams.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
} else if(flashMode.equals("off")) {
camParams.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
} else {
camParams.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
}
camParams.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
try { camera.setParameters(camParams); }
catch (RuntimeException e) {
Log.d("csZBar", "Unsupported camera parameter reported for flash mode: "+flashMode);
}
tryStartPreview();
}
@Override
public void onPause ()
{
releaseCamera();
super.onPause();
}
@Override
public void onDestroy ()
{
scanner.destroy();
super.onDestroy();
}
// Event handlers --------------------------------------------------
@Override
public void onBackPressed ()
{
setResult(RESULT_CANCELED);
super.onBackPressed();
}
// SurfaceHolder.Callback implementation ---------------------------
@Override
public void surfaceCreated (SurfaceHolder hld)
{
tryStopPreview();
holder = hld;
tryStartPreview();
}
@Override
public void surfaceDestroyed (SurfaceHolder holder)
{
// No surface == no preview == no point being in this Activity.
die("The camera surface was destroyed");
}
@Override
public void surfaceChanged (SurfaceHolder hld, int fmt, int w, int h)
{
// Sanity check - holder must have a surface...
if(hld.getSurface() == null) die("There is no camera surface");
surfW = w;
surfH = h;
matchSurfaceToPreviewRatio();
tryStopPreview();
holder = hld;
tryStartPreview();
}
// Continuously auto-focus -----------------------------------------
private AutoFocusCallback autoFocusCb = new AutoFocusCallback()
{
public void onAutoFocus(boolean success, Camera camera) {
// some devices crash without this try/catch and cancelAutoFocus()... (#9)
try {
camera.cancelAutoFocus();
autoFocusHandler.postDelayed(doAutoFocus, autoFocusInterval);
} catch (Exception e) {}
}
};
private Runnable doAutoFocus = new Runnable()
{
public void run() {
if(camera != null) camera.autoFocus(autoFocusCb);
}
};
// Camera callbacks ------------------------------------------------
// Receives frames from the camera and checks for barcodes.
private PreviewCallback previewCb = new PreviewCallback()
{
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);
if (scanner.scanImage(barcode) != 0) {
String qrValue = "";
SymbolSet syms = scanner.getResults();
for (Symbol sym : syms) {
qrValue = sym.getData();
// Return 1st found QR code value to the calling Activity.
Intent result = new Intent ();
result.putExtra(EXTRA_QRVALUE, qrValue);
setResult(Activity.RESULT_OK, result);
finish();
}
}
}
};
// Misc ------------------------------------------------------------
// finish() due to error
private void die (String msg)
{
setResult(RESULT_ERROR);
finish();
}
private int getResourceId (String typeAndName)
{
if(package_name == null) package_name = getApplication().getPackageName();
if(resources == null) resources = getApplication().getResources();
return resources.getIdentifier(typeAndName, null, package_name);
}
// Release the camera resources and state.
private void releaseCamera ()
{
if (camera != null) {
autoFocusHandler.removeCallbacks(doAutoFocus);
camera.setPreviewCallback(null);
camera.release();
camera = null;
}
}
// Match the aspect ratio of the preview SurfaceView with the camera's preview aspect ratio,
// so that the displayed preview is not stretched/squashed.
private void matchSurfaceToPreviewRatio () {
if(camera == null) return;
if(surfW == 0 || surfH == 0) return;
// Resize SurfaceView to match camera preview ratio (avoid stretching).
Camera.Parameters params = camera.getParameters();
Camera.Size size = params.getPreviewSize();
float previewRatio = (float) size.height / size.width; // swap h and w as the preview is rotated 90 degrees
float surfaceRatio = (float) surfW / surfH;
if(previewRatio > surfaceRatio) {
scannerSurface.setLayoutParams(new FrameLayout.LayoutParams(
surfW,
Math.round((float) surfW / previewRatio),
Gravity.CENTER
));
} else if(previewRatio < surfaceRatio) {
scannerSurface.setLayoutParams(new FrameLayout.LayoutParams(
Math.round((float) surfH * previewRatio),
surfH,
Gravity.CENTER
));
}
}
// Stop the camera preview safely.
private void tryStopPreview () {
// Stop camera preview before making changes.
try {
camera.stopPreview();
} catch (Exception e){
// Preview was not running. Ignore the error.
}
}
public Collection<BarcodeFormat> getFormats() {
if(mFormats == null) {
return BarcodeFormat.ALL_FORMATS;
}
return mFormats;
}
// Start the camera preview if possible.
// If start is attempted but fails, exit with error message.
private void tryStartPreview () {
if(holder != null) {
try {
// 90 degrees rotation for Portrait orientation Activity.
camera.setDisplayOrientation(90);
camera.setPreviewDisplay(holder);
camera.setPreviewCallback(previewCb);
camera.startPreview();
//camera.autoFocus(autoFocusCb); // We are not using any of the
// continuous autofocus modes as that does not seem to work
// well with flash setting of "on"... At least with this
// simple and stupid focus method, we get to turn the flash
// on during autofocus.
} catch (IOException e) {
die("Could not start camera preview: " + e.getMessage());
}
}
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
android/libs/zbar.jar Normal file

Binary file not shown.

View File

@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/csZbarScannerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/csZbarScannerBackground" >
<TextView android:id="@+id/csZbarScannerInstructions"
android:layout_gravity="bottom|center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingBottom="15dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:gravity="center"
android:textSize="12pt"
android:textColor="@color/csZbarScannerTextColor"
android:background="@color/csZbarScannerTextBackground"
android:fontFamily="sans-serif-light"
android:text="@string/csZbarScannerInstructions" />
<RelativeLayout android:id="@+id/csZbarScannerSightContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<View android:id="@+id/csZbarScannerSight"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_centerInParent="true"
android:gravity="center_vertical"
android:background="#ff0000" />
</RelativeLayout>
</FrameLayout>