Java tutorial
/** * This file was auto-generated by the Titanium Module SDK helper for Android * Appcelerator Titanium Mobile * Copyright (c) 2009-2013 by Appcelerator, Inc. All Rights Reserved. * Licensed under the terms of the Apache Public License * Please see the LICENSE included with this distribution for details. * */ package com.manateeworks.barcodescanner; import java.util.ArrayList; import java.util.List; import java.util.HashMap; import org.appcelerator.kroll.KrollDict; import org.appcelerator.kroll.KrollFunction; import org.appcelerator.kroll.KrollModule; import org.appcelerator.kroll.KrollObject; import org.appcelerator.kroll.annotations.Kroll; import org.appcelerator.titanium.TiApplication; import org.appcelerator.titanium.TiBlob; import org.appcelerator.titanium.TiContext; import org.appcelerator.titanium.util.TiActivityResultHandler; import org.appcelerator.titanium.util.TiActivitySupport; import org.appcelerator.titanium.util.TiIntentWrapper; import org.appcelerator.kroll.common.Log; import org.json.JSONArray; import com.manateeworks.BarcodeScanner; import android.R.string; import android.app.Activity; import android.content.Intent; @Kroll.module(name = "Mwbarcodescanner", id = "com.manateeworks.barcodescanner") public class MwbarcodescannerModule extends KrollModule { // Standard Debugging variables private static final String TAG = "MwbarcodescannerModule"; // You can define constants with @Kroll.constant, for example: // @Kroll.constant public static final String EXTERNAL_NAME = value; public MwbarcodescannerModule(final TiContext context) { super(context); } @Kroll.onAppCreate public static void onAppCreate(TiApplication app) { Log.d(TAG, "inside onAppCreate"); ScannerActivity.scanningRects = new ArrayList<int[]>(); // put module init code that needs to run when the application is // created // Now you can use registering calls from JS, without recompiling the // modules /* * BarcodeScanner.MWBregisterCode(BarcodeScanner.MWB_CODE_MASK_25, * "username", "key"); * BarcodeScanner.MWBregisterCode(BarcodeScanner.MWB_CODE_MASK_39, * "username", "key"); * BarcodeScanner.MWBregisterCode(BarcodeScanner.MWB_CODE_MASK_93, * "username", "key"); * BarcodeScanner.MWBregisterCode(BarcodeScanner.MWB_CODE_MASK_128, * "username", "key"); * BarcodeScanner.MWBregisterCode(BarcodeScanner.MWB_CODE_MASK_AZTEC, * "username", "key"); * BarcodeScanner.MWBregisterCode(BarcodeScanner.MWB_CODE_MASK_DM, * "username", "key"); * BarcodeScanner.MWBregisterCode(BarcodeScanner.MWB_CODE_MASK_EANUPC, * "username", "key"); * BarcodeScanner.MWBregisterCode(BarcodeScanner.MWB_CODE_MASK_PDF, * "username", "key"); * BarcodeScanner.MWBregisterCode(BarcodeScanner.MWB_CODE_MASK_QR, * "username", "key"); * BarcodeScanner.MWBregisterCode(BarcodeScanner.MWB_CODE_MASK_RSS, * "username", "key"); * BarcodeScanner.MWBregisterCode(BarcodeScanner.MWB_CODE_MASK_CODABAR * ,"username", "key"); */ } // Methods @Kroll.method public String example() { Log.d(TAG, "example called"); return "hello world"; } // Properties @Kroll.getProperty public String getExampleProp() { Log.d(TAG, "get example property"); return "hello world"; } @Kroll.setProperty public void setExampleProp(String value) { Log.d(TAG, "set example property: " + value); } // Methods @Kroll.method public void startScanning(KrollDict options) { Log.d(TAG, "start scanning"); final KrollFunction successCallback = getCallback(options, "success"); final KrollFunction cancelCallback = getCallback(options, "cancel"); final KrollFunction errorCallback = getCallback(options, "error"); final Activity activity = TiApplication.getAppCurrentActivity(); final TiActivitySupport activitySupport = (TiActivitySupport) activity; final TiIntentWrapper barcodeIntent = new TiIntentWrapper(new Intent(activity, ScannerActivity.class)); barcodeIntent.setWindowId(TiIntentWrapper.createActivityName("SCANNER")); BarcodeResultHandler resultHandler = new BarcodeResultHandler(); resultHandler.successCallback = successCallback; resultHandler.cancelCallback = cancelCallback; resultHandler.errorCallback = errorCallback; resultHandler.activitySupport = activitySupport; resultHandler.barcodeIntent = barcodeIntent.getIntent(); activity.runOnUiThread(resultHandler); // activity.runOnUiThread(resultHandler); // activity.startActivity(barcodeIntent); // activity.runOnUiThread(new tia); } private KrollFunction getCallback(final KrollDict options, final String name) { if (options.containsKey(name)) { return (KrollFunction) options.get(name); } else { // logError("Callback not found: " + name); return null; } } protected class BarcodeResultHandler implements TiActivityResultHandler, Runnable { protected int code; protected KrollFunction successCallback, cancelCallback, errorCallback; protected TiActivitySupport activitySupport; protected Intent barcodeIntent; @Override public void run() { int code = activitySupport.getUniqueResultCode(); activitySupport.launchActivityForResult(barcodeIntent, code, this); ScannerActivity.resultHandler = this; } @Override public void onError(Activity arg0, int arg1, Exception arg2) { if (errorCallback != null) { errorCallback.callAsync((KrollObject) errorCallback, createErrorResponse(-1, "error")); } } @Override public void onResult(Activity activity, int requestCode, int resultCode, Intent data) { //Log.i("Scanner result","Result code: " + String.valueOf(resultCode)); if (resultCode == 1) { final KrollDict dict = new KrollDict(); dict.put("code", data.getStringExtra("code")); dict.put("type", data.getStringExtra("type")); dict.put("isGS1", data.getBooleanExtra("isGS1", false)); byte[] bytes = data.getByteArrayExtra("bytes"); Object[] bytes2 = new Object[bytes.length]; for (int i = 0; i < bytes.length; i++) { bytes2[i] = new Integer(bytes[i] & 0xff); } // TiBlob rawArray = TiBlob.blobFromData(data // .getByteArrayExtra("bytes")); dict.put("bytes", bytes2); successCallback.callAsync((KrollObject) successCallback, dict); } else { cancelCallback.callAsync((KrollObject) cancelCallback, new KrollDict()); } } /* * public void run() { code = activitySupport.getUniqueResultCode(); * activitySupport.launchActivityForResult(barcodeIntent, code, this); } * * public void onError(Activity activity, int requestCode, Exception e) * { String msg = "Problem with scanner; " + e.getMessage(); * //logError("error: " + msg); if (errorCallback != null) { * errorCallback * .callAsync((KrollObject)errorCallback,createErrorResponse(-1, msg)); * } } * * public void onResult(Activity activity, int requestCode, int * resultCode, Intent data) { //logDebug("onResult() called"); * * if (resultCode == Activity.RESULT_CANCELED) { * //logDebug("scan canceled"); if (cancelCallback != null) { * cancelCallback.callAsync((KrollObject)cancelCallback,new * KrollDict()); } } else { //logDebug("scan successful"); String result * = data .getStringExtra("123"); //logDebug("scan result: " + result); * successCallback.callAsync((KrollObject)successCallback,new * HashMap<>()); } } */ } @Kroll.method public int MWBregisterCode(int codeMask, String userName, String key) { return BarcodeScanner.MWBregisterCode(codeMask, userName, key); } @Kroll.method public int MWBsetActiveCodes(int codeMask) { ScannerActivity.activeCodes = codeMask; return BarcodeScanner.MWBsetActiveCodes(codeMask); } @Kroll.method public int MWBsetLevel(int level) { return BarcodeScanner.MWBsetLevel(level); } @Kroll.method public int MWBsetActiveSubcodes(int codeMask, int subMask) { return BarcodeScanner.MWBsetActiveSubcodes(codeMask, subMask); } @Kroll.method public int MWBsetFlags(int codeMask, int flags) { return BarcodeScanner.MWBsetFlags(codeMask, flags); } @Kroll.method public int MWBsetDirection(int direction) { ScannerActivity.orientation = direction; return BarcodeScanner.MWBsetDirection(direction); } @Kroll.method public int MWBsetScanningRect(int codeMask, int left, int top, int width, int height) { int rectIndex = -1; for (int i = 0; i < ScannerActivity.scanningRects.size(); i++) { int[] curRect = ScannerActivity.scanningRects.get(i); if (curRect[0] == codeMask) { rectIndex = i; break; } } int[] newRect = new int[5]; newRect[0] = codeMask; newRect[1] = left; newRect[2] = top; newRect[3] = width; newRect[4] = height; if (rectIndex >= 0) { ScannerActivity.scanningRects.set(rectIndex, newRect); } else { ScannerActivity.scanningRects.add(newRect); } return BarcodeScanner.MWBsetScanningRect(codeMask, left, top, width, height); } @Kroll.method public int MWBgetLibVersion() { return BarcodeScanner.MWBgetLibVersion(); } @Kroll.method public String MWBgetLibVersionString() { int ver = BarcodeScanner.MWBgetLibVersion(); int v1 = (ver >> 16); int v2 = (ver >> 8) & 0xff; int v3 = (ver & 0xff); String libVersion = "Lib version: " + String.valueOf(v1) + "." + String.valueOf(v2) + "." + String.valueOf(v3); return libVersion; } @Kroll.method public void MWBuseHiRes(boolean hiRes) { if (hiRes) { ScannerActivity.desiredCameraWidth = 1280; ScannerActivity.desiredCameraHeight = 720; } else { ScannerActivity.desiredCameraWidth = 800; ScannerActivity.desiredCameraHeight = 480; } } @Kroll.method public void MWBsetDesiredResolution(int width, int height) { ScannerActivity.desiredCameraWidth = width; ScannerActivity.desiredCameraHeight = height; } @Kroll.method public void MWBuseBlinkingLineOverlay(boolean blinkingLine) { ScannerActivity.useBLinkingLineOverlay = blinkingLine; } @Kroll.method public void MWBsetCloseCameraOnDetection(boolean closeCamera) { ScannerActivity.closeCameraOnDetection = closeCamera; } @Kroll.method public void MWBsetButtonsVisible(boolean flashVisible, boolean closeVisible) { ScannerActivity.flashVisible = flashVisible; ScannerActivity.closeVisible = closeVisible; } @Kroll.method public void MWBresumeScanning() { ScannerActivity.resumeScanning(); } @Kroll.method public void MWBcloseScanner() { ScannerActivity.closeScanner(); } @Kroll.method public void MWBsetMaxThreads(int maxThreads) { ScannerActivity.param_maxThreads = maxThreads; } @Kroll.method public void MWBenableZoom(boolean enableZoom) { ScannerActivity.param_EnableZoom = enableZoom; } @Kroll.method public void MWBsetZoomLevels(int zoomLevel1, int zoomLevel2, int initialZoomLevel) { ScannerActivity.param_ZoomLevel1 = zoomLevel1; ScannerActivity.param_ZoomLevel2 = zoomLevel2; ScannerActivity.zoomLevel = zoomLevel2; if (ScannerActivity.zoomLevel > 2) { ScannerActivity.zoomLevel = 2; } if (ScannerActivity.zoomLevel < 0) { ScannerActivity.zoomLevel = 0; } } }