2014-09-11 15:52:59 +08:00
|
|
|
package org.cloudsky.cordovaPlugins;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2014-12-06 16:38:00 +08:00
|
|
|
import java.lang.RuntimeException;
|
2014-12-06 15:40:17 +08:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2014-09-11 15:52:59 +08:00
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.hardware.Camera;
|
2014-12-06 16:38:00 +08:00
|
|
|
import android.hardware.Camera.CameraInfo;
|
2014-09-11 15:52:59 +08:00
|
|
|
import android.hardware.Camera.PreviewCallback;
|
|
|
|
import android.hardware.Camera.AutoFocusCallback;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Handler;
|
2014-12-06 16:58:46 +08:00
|
|
|
import android.util.Log;
|
2014-09-11 15:52:59 +08:00
|
|
|
import android.view.Gravity;
|
|
|
|
import android.view.SurfaceHolder;
|
|
|
|
import android.view.SurfaceView;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.FrameLayout;
|
2014-12-06 15:40:17 +08:00
|
|
|
import android.widget.TextView;
|
2014-09-11 15:52:59 +08:00
|
|
|
|
|
|
|
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 {
|
|
|
|
|
|
|
|
// Config ----------------------------------------------------------
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
private static int autoFocusInterval = 500; // Interval between AFcallback and next AF attempt.
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
// Public Constants ------------------------------------------------
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
public static final String EXTRA_QRVALUE = "qrValue";
|
2014-12-06 15:40:17 +08:00
|
|
|
public static final String EXTRA_PARAMS = "params";
|
2014-09-11 15:52:59 +08:00
|
|
|
public static final int RESULT_ERROR = RESULT_FIRST_USER + 1;
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
// State -----------------------------------------------------------
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
private Camera camera;
|
|
|
|
private Handler autoFocusHandler;
|
|
|
|
private SurfaceView scannerSurface;
|
|
|
|
private SurfaceHolder holder;
|
|
|
|
private ImageScanner scanner;
|
|
|
|
private int surfW, surfH;
|
|
|
|
|
2014-12-06 16:38:00 +08:00
|
|
|
// Customisable stuff
|
|
|
|
String whichCamera;
|
2014-12-06 16:58:46 +08:00
|
|
|
String flashMode;
|
2014-12-06 16:38:00 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
// 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) -------------------------------------
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
static {
|
|
|
|
// Needed by ZBar??
|
|
|
|
System.loadLibrary("iconv");
|
|
|
|
}
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
// Activity Lifecycle ----------------------------------------------
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate (Bundle savedInstanceState)
|
|
|
|
{
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
2014-12-06 15:40:17 +08:00
|
|
|
// 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");
|
2014-12-06 16:38:00 +08:00
|
|
|
whichCamera = params.optString("camera");
|
2014-12-06 16:58:46 +08:00
|
|
|
flashMode = params.optString("flash");
|
2014-12-06 15:40:17 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
// Initiate instance variables
|
|
|
|
autoFocusHandler = new Handler();
|
|
|
|
scanner = new ImageScanner();
|
|
|
|
scanner.setConfig(0, Config.X_DENSITY, 3);
|
|
|
|
scanner.setConfig(0, Config.Y_DENSITY, 3);
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
// Set content view
|
|
|
|
setContentView(getResourceId("layout/cszbarscanner"));
|
2014-12-06 15:40:17 +08:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
// 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);
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
// Add preview SurfaceView to the screen
|
|
|
|
((FrameLayout) findViewById(getResourceId("id/csZbarScannerView"))).addView(scannerSurface);
|
|
|
|
findViewById(getResourceId("id/csZbarScannerTitle")).bringToFront();
|
|
|
|
findViewById(getResourceId("id/csZbarScannerInstructions")).bringToFront();
|
|
|
|
}
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
@Override
|
|
|
|
public void onResume ()
|
|
|
|
{
|
|
|
|
super.onResume();
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
try {
|
2014-12-06 16:38:00 +08:00
|
|
|
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) {
|
2014-09-11 15:52:59 +08:00
|
|
|
die("Error: Could not open the camera.");
|
|
|
|
return;
|
2014-12-06 16:38:00 +08:00
|
|
|
} catch (Exception e) {
|
|
|
|
die(e.getMessage());
|
|
|
|
return;
|
2014-09-11 15:52:59 +08:00
|
|
|
}
|
|
|
|
|
2014-12-06 16:58:46 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
try { camera.setParameters(camParams); }
|
|
|
|
catch (RuntimeException e) {
|
|
|
|
Log.d("csZBar", "Unsupported camera parameter reported for flash mode: "+flashMode);
|
|
|
|
}
|
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
tryStartPreview();
|
|
|
|
}
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
@Override
|
|
|
|
public void onPause ()
|
|
|
|
{
|
|
|
|
releaseCamera();
|
|
|
|
super.onPause();
|
|
|
|
}
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
@Override
|
|
|
|
public void onDestroy ()
|
|
|
|
{
|
|
|
|
scanner.destroy();
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Event handlers --------------------------------------------------
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBackPressed ()
|
|
|
|
{
|
|
|
|
setResult(RESULT_CANCELED);
|
|
|
|
super.onBackPressed();
|
|
|
|
}
|
|
|
|
|
|
|
|
// SurfaceHolder.Callback implementation ---------------------------
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
@Override
|
|
|
|
public void surfaceCreated (SurfaceHolder hld)
|
|
|
|
{
|
|
|
|
tryStopPreview();
|
|
|
|
holder = hld;
|
|
|
|
tryStartPreview();
|
|
|
|
}
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
@Override
|
|
|
|
public void surfaceDestroyed (SurfaceHolder holder)
|
|
|
|
{
|
|
|
|
// No surface == no preview == no point being in this Activity.
|
|
|
|
die("The camera surface was destroyed");
|
|
|
|
}
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
@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");
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
surfW = w;
|
|
|
|
surfH = h;
|
|
|
|
matchSurfaceToPreviewRatio();
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
tryStopPreview();
|
|
|
|
holder = hld;
|
|
|
|
tryStartPreview();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Continuously auto-focus -----------------------------------------
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
private AutoFocusCallback autoFocusCb = new AutoFocusCallback()
|
|
|
|
{
|
|
|
|
public void onAutoFocus(boolean success, Camera camera) {
|
|
|
|
autoFocusHandler.postDelayed(doAutoFocus, autoFocusInterval);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private Runnable doAutoFocus = new Runnable()
|
|
|
|
{
|
|
|
|
public void run() {
|
|
|
|
if(camera != null) camera.autoFocus(autoFocusCb);
|
|
|
|
}
|
|
|
|
};
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
// Camera callbacks ------------------------------------------------
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
// 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 = "";
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
SymbolSet syms = scanner.getResults();
|
|
|
|
for (Symbol sym : syms) {
|
|
|
|
qrValue = sym.getData();
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
// 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();
|
|
|
|
}
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
// Misc ------------------------------------------------------------
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
// 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);
|
|
|
|
}
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
// Release the camera resources and state.
|
|
|
|
private void releaseCamera ()
|
|
|
|
{
|
|
|
|
if (camera != null) {
|
|
|
|
autoFocusHandler.removeCallbacks(doAutoFocus);
|
|
|
|
camera.setPreviewCallback(null);
|
|
|
|
camera.release();
|
|
|
|
camera = null;
|
|
|
|
}
|
|
|
|
}
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
// 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;
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
// 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;
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
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
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
// 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.
|
|
|
|
}
|
|
|
|
}
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
// 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);
|
2014-12-06 16:40:14 +08:00
|
|
|
|
2014-09-11 15:52:59 +08:00
|
|
|
camera.setPreviewDisplay(holder);
|
|
|
|
camera.setPreviewCallback(previewCb);
|
|
|
|
camera.startPreview();
|
2014-12-06 17:32:54 +08:00
|
|
|
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.
|
2014-09-11 15:52:59 +08:00
|
|
|
} catch (IOException e) {
|
|
|
|
die("Could not start camera preview: " + e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|