我是iOS开发人员,只有很少的Javascript经验。但是,我遇到了一个问题,就是要运行一个应该扫描条形码的手机间隙应用程序。
调用条形码扫描代码时出错: “未捕获的TypeError:undefined不是函数”
MWBScanner.js
文件非常简单。它有以下定义&调用var BarcodeScanner
var BarcodeScanner = {
/**
* Init decoder with default params.
*/
MWBinitDecoder: function(callback) {
cordova.exec(callback, function(){}, "MWBarcodeScanner", "initDecoder", []);
},
/**
* Call the scanner screen. Result are returned in callback function as:
* result.code - string representation of barcode result
* result.type - type of barcode detected or 'Cancel' if scanning is canceled
* result.bytes - bytes array of raw barcode result
*/
MWBstartScanning: function(callback) {
cordova.exec(callback, function(err) {
callback('Error: ' + err);
}, "MWBarcodeScanner", "startScanner", []);
},
/**
* Registers licensing information with single selected decoder type.
* If registering information is correct, enables full support for selected
* decoder type.
* It should be called once per decoder type.
*
* @param[in] codeMask Single decoder type selector (MWB_CODE_MASK_...)
* @param[in] userName User name string
* @param[in] key License key string
*
* @retval MWB_RT_OK Registration successful
* @retval MWB_RT_FAIL Registration failed
* @retval MWB_RT_BAD_PARAM More than one decoder flag selected
* @retval MWB_RT_NOT_SUPPORTED Selected decoder type or its registration
* is not supported
*/
MWBregisterCode: function(codeMask, userName, key) {
cordova.exec(function(){}, function(){}, "MWBarcodeScanner", "registerCode", [codeMask, userName, key]);
},
/**
* Sets active or inactive status of decoder types
*
* @param[in] activeCodes ORed bit flags (MWB_CODE_MASK_...) of decoder types
* to be activated.
*/
MWBsetActiveCodes: function(activeCodes) {
cordova.exec(function(){}, function(){}, "MWBarcodeScanner", "setActiveCodes", [activeCodes]);
},
/**
* Set active subcodes for given code group flag.
* Subcodes under some decoder type are all activated by default.
*
* @param[in] codeMask Single decoder type/group (MWB_CODE_MASK_...)
* @param[in] subMask ORed bit flags of requested decoder subtypes (MWB_SUBC_MASK_)
*/
MWBsetActiveSubcodes: function(codeMask, activeSubcodes) {
cordova.exec(function(){}, function(){}, "MWBarcodeScanner", "setActiveSubcodes", [codeMask, activeSubcodes]);
},
/**
* MWBsetFlags configures options (if any) for decoder type specified in codeMask.
* Options are given in flags as bitwise OR of option bits. Available options depend on selected decoder type.
*
* @param[in] codeMask Single decoder type (MWB_CODE_MASK_...)
* @param[in] flags ORed bit mask of selected decoder type options (MWB_FLAG_...)
*/
MWBsetFlags: function(codeMask, flags) {
cordova.exec(function(){}, function(){}, "MWBarcodeScanner", "setFlags", [codeMask, flags]);
},
/**
* This function enables some control over scanning lines choice for 1D barcodes. By ORing
* available bit-masks user can add one or more direction options to scanning lines set.
* @n - MWB_SCANDIRECTION_HORIZONTAL - horizontal lines
* @n - MWB_SCANDIRECTION_VERTICAL - vertical lines
* @n - MWB_SCANDIRECTION_OMNI - omnidirectional lines
* @n - MWB_SCANDIRECTION_AUTODETECT - enables BarcodeScanner's
* autodetection of barcode direction
*
* @param[in] direction ORed bit mask of direction modes given with
* MWB_SCANDIRECTION_... bit-masks
*/
MWBsetDirection: function(direction) {
cordova.exec(function(){}, function(){}, "MWBarcodeScanner", "setDirection", [direction]);
},
/**
* Sets rectangular area for barcode scanning with selected single decoder type.
* After MWBsetScanningRect() call, all subseqent scans will be restricted
* to this region. If rectangle is not set, whole image is scanned.
* Also, if width or height is zero, whole image is scanned.
*
* Parameters are interpreted as percentage of image dimensions, i.e. ranges are
* 0 - 100 for all parameters.
*
* @param[in] codeMask Single decoder type selector (MWB_CODE_MASK_...)
* @param[in] left X coordinate of left edge (percentage)
* @param[in] top Y coordinate of top edge (percentage)
* @param[in] width Rectangle witdh (x axis) (percentage)
* @param[in] height Rectangle height (y axis) (percentage)
*/
MWBsetScanningRect: function(codeMask, left, top, width, height) {
cordova.exec(function(){}, function(){}, "MWBarcodeScanner", "setScanningRect", [codeMask, left, top, width, height]);
},
/**
* Barcode detector relies on image processing and geometry inerpolation for
* extracting optimal data for decoding. Higher effort level involves more processing
* and intermediate parameter values, thus increasing probability of successful
* detection with low quality images, but also consuming more CPU time.
*
* @param[in] level Effort level - available values are 1, 2, 3, 4 and 5.
* Levels greater than 3 are not suitable fro real-time decoding
*/
MWBsetLevel: function(level) {
cordova.exec(function(){}, function(){}, "MWBarcodeScanner", "setLevel", [level]);
},
/**
* Sets prefered User Interface orientation of scanner screen
* Choose one fo the available values:
* OrientationPortrait
* OrientationLandscapeLeft
* OrientationLandscapeRight
*
* Default value is OrientationLandscapeLeft
*/
MWBsetInterfaceOrientation: function(interfaceOrientation) {
cordova.exec(function(){}, function(){}, "MWBarcodeScanner", "setInterfaceOrientation", [interfaceOrientation]);
},
/**
* Choose overlay graphics type for scanning screen:
* OverlayModeNone - No overlay is displayed
* OverlayModeMW - Use MW Dynamic Viewfinder with blinking line (you can customize display options
* in native class by changing defaults)
* OverlayModeImage - Show image on top of camera preview
*
* Default value is OverlayModeMW
*/
MWBsetOverlayMode: function(overlayMode) {
cordova.exec(function(){}, function(){}, "MWBarcodeScanner", "setOverlayMode", [overlayMode]);
},
/**
* Enable or disable high resolution scanning. It's recommended to enable it when target barcodes
* are of high density or small footprint. If device doesn't support high resolution param will be ignored
*
* Default value is true (enabled)
*/
MWBenableHiRes: function(enableHiRes) {
cordova.exec(function(){}, function(){}, "MWBarcodeScanner", "enableHiRes", [enableHiRes]);
},
/**
* Enable or disable flash toggle button on scanning screen. If device doesn't support flash mode
* button will be hidden regardles of param
*
* Default value is true (enabled)
*/
MWBenableFlash: function(enableFlash) {
cordova.exec(function(){}, function(){}, "MWBarcodeScanner", "enableFlash", [enableFlash]);
},
/**
* Set default state of flash (torch) when scanner activity is started
*
* Default value is false (disabled)
*/
MWBturnFlashOn: function(flashOn) {
cordova.exec(function(){}, function(){}, "MWBarcodeScanner", "turnFlashOn", [flashOn]);
},
/**
* Enable or disable zoom button on scanning screen. If device doesn't support zoom,
* button will be hidden regardles of param. Zoom is not supported on Windows Phone 8
* as there's no zooming api available!
*
* Default value is true (enabled)
*/
MWBenableZoom: function(enableZoom) {
cordova.exec(function(){}, function(){}, "MWBarcodeScanner", "enableZoom", [enableZoom]);
},
/**
* Set two desired zoom levels in percentage and initial level. Set first two params to zero for default
* levels. On iOS, first zoom level is set to maximum non-interpolated level available on device, and
* second is double of first level. On Android, default first zoom is 150% and second is 300%. Zoom is
* not supported on Windows Phone 8 as there's no zooming api available!
* Initial zoom level can be 0 (100% - non zoomed), 1 (zoomLevel1) or 2 (zoomLevel2). Default is 0.
*
*/
MWBsetZoomLevels: function(zoomLevel1, zoomLevel2, initialZoomLevel) {
cordova.exec(function(){}, function(){}, "MWBarcodeScanner", "setZoomLevels", [zoomLevel1, zoomLevel2, initialZoomLevel]);
},
/**
* Set custom key:value pair which is accesible from native code.
*/
MWBsetCustomParam: function(key, value) {
cordova.exec(function(){}, function(){}, "MWBarcodeScanner", "setCustomParam", [key, value]);
}
};
从文件startScreen.js调用此函数:
function startScanning()
{
//alert ('Hello world!');
// cordova.exec(function()
// {
// alert ('Success');
// },
// function()
// {
// alert ('Failure');
// },
// "RPGBarcodeScanner",
// "startScanning",
// []);
scanner.startScanning(MWBSInitSpace.init,MWBSInitSpace.callback);
}
$(document).off("pagebeforeshow", "#startScreen").on("pagebeforeshow", "#startScreen", function(event, ui) {
Apperyio.CurrentScreen = "startScreen";
_.chain(Apperyio.mappings).filter(function(m) {
return m.homeScreen === Apperyio.CurrentScreen;
}).each(Apperyio.UIHandler.hideTemplateComponents);
});
startScreen_onLoad();
};
我确保在startScreen.html文件中加载脚本MWBScanner.js和MWBConfig.js(MWBSInitSpace):
<head>
<title>
startScreen
</title>
.
.
.
<!-- Raj -->
<script type="text/javascript" src="files/views/assets/js/MWBConfig.js">
</script>
<script type="text/javascript" src="files/views/assets/js/MWBScanner.js">
</script>
.
.
.
</head>
我该如何解决?
修改
加载startScreen.js文件: StartScreen.js文件正在其中一个块中的StartScreen.html文件中加载:
<div data-role="content" id="startScreen_mobilecontainer" name="mobilecontainer"
class="startScreen_mobilecontainer ui-content" dsid="mobilecontainer" data-theme="b">
<link href="startScreen.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="startScreen.js">
</script>
<!-- mobilebutton_2 --><!--
--><a data-role="button" name="mobilebutton_2" dsid="mobilebutton_2" class="startScreen_mobilebutton_2"
id="startScreen_mobilebutton_2" data-corners="true" data-icon="none" data-iconpos='nowhere'
x-apple-data-detectors="false" data-mini="false" data-theme="b" tabindex="1">
Scan
</a>
</div>
我可以确认在断点的帮助下调用scanner.startScanning函数,如附图中所示。
我无法附加整个MWBScanner.js文件,因为它超出了文本限制,因此我只添加了MWBScanner中感兴趣的部分代码。
答案 0 :(得分:0)
原来,&#34; BarcodeScanner&#34; 已经在项目的其他地方定义了,我很难搞清楚这一点!