From 8d0261b650b27fd2044cf3fa2cbb19779d7532a6 Mon Sep 17 00:00:00 2001 From: Woon Tien Jing Date: Sat, 6 Dec 2014 15:40:17 +0800 Subject: [PATCH] Added text_title and text_instructions parameters for customising the scanner view's title and instructions texts (Android only in this commit) --- README.md | 10 +++++++++- android/ZBar.java | 6 ++++-- android/ZBarScannerActivity.java | 22 ++++++++++++++++++++-- www/zBar.js | 5 +++++ 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index efe8b66..8d2e249 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,15 @@ This plugin is released under the Apache 2.0 license, but the ZBar library on wh Arguments: -- **params**: _Unused for now. Will be an object in the future if we extend this plugin with options to specify scannable barcode types, etc._ +- **params**: Optional parameters: + + ```javascript + { + text_title: "Title Text - default = 'Scan QR Code'", + text_instructions: "Instruction Text - default = 'Please point your camera at the QR code.'", + } + ``` + - **onSuccess**: function (s) {...} _Callback for successful scan._ - **onFailure**: function (s) {...} _Callback for cancelled scan or error._ diff --git a/android/ZBar.java b/android/ZBar.java index 519fd60..2d45cea 100644 --- a/android/ZBar.java +++ b/android/ZBar.java @@ -1,11 +1,10 @@ package org.cloudsky.cordovaPlugins; -import org.apache.cordova.CordovaInterface; import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CallbackContext; -import org.apache.cordova.PluginResult; import org.json.JSONArray; import org.json.JSONException; +import org.json.JSONObject; import android.app.Activity; import android.content.Intent; @@ -38,8 +37,11 @@ public class ZBar extends CordovaPlugin { } 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; diff --git a/android/ZBarScannerActivity.java b/android/ZBarScannerActivity.java index a972d85..be6a2c3 100644 --- a/android/ZBarScannerActivity.java +++ b/android/ZBarScannerActivity.java @@ -1,6 +1,8 @@ package org.cloudsky.cordovaPlugins; import java.io.IOException; +import org.json.JSONException; +import org.json.JSONObject; import android.app.Activity; import android.content.Intent; @@ -15,7 +17,7 @@ import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.ViewGroup; import android.widget.FrameLayout; -import android.widget.Toast; +import android.widget.TextView; import net.sourceforge.zbar.ImageScanner; import net.sourceforge.zbar.Image; @@ -33,6 +35,7 @@ implements SurfaceHolder.Callback { // 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 ----------------------------------------------------------- @@ -64,6 +67,15 @@ implements SurfaceHolder.Callback { { 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"); + // Initiate instance variables autoFocusHandler = new Handler(); scanner = new ImageScanner(); @@ -72,7 +84,13 @@ implements SurfaceHolder.Callback { // 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); + // Create preview SurfaceView scannerSurface = new SurfaceView (this) { @Override diff --git a/www/zBar.js b/www/zBar.js index c46e40f..b423957 100644 --- a/www/zBar.js +++ b/www/zBar.js @@ -8,6 +8,11 @@ ZBar.prototype = { scan: function (params, success, failure) { argscheck.checkArgs('*fF', 'CsZBar.scan', arguments); + + params = params || {}; + if(params.text_title === undefined) params.text_title = "Scan QR Code"; + if(params.text_instructions === undefined) params.text_instructions = "Please point your camera at the QR code."; + exec(success, failure, 'CsZBar', 'scan', [params]); },