scanner.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // 扫描仪操作对象
  2. var Scanner = function() {
  3. };
  4. (function() {
  5. Scanner.prototype = {
  6. construct : Scanner,
  7. // 私有属性
  8. _parameter : {
  9. 'domId' : null, // 控件容器ID
  10. 'locationPath' : null, // 扫描文件的本地存储路径
  11. 'produceFilenameFunc' : null, // 生成扫描文件名称的函数体
  12. 'prefix' : 'XXX', // 生成文件的名称前缀
  13. 'saveType' : 1, // 生成文件的格式{int} 1:JPG, 2:Tif, 3:Pdf
  14. 'dpi' : 200, // 生成文件的dpi设置,默认150
  15. 'colorType' : 3, // 扫描影像色彩{int} 1=黑白, 2=灰度, 3=彩色
  16. 'isDoubleSide' : 2, // 是否开启双面扫描设置{int} 2=开启, 1=关闭
  17. 'isDelNullPage' : 1, // 是否开启去除空白页面{int} 1=开启, 2=关闭
  18. 'isRoteWord' : 1, // 是否开启识别文字方向{int} 1=开启 , 2=关闭
  19. 'isRectification' : 1, // 是否开启纠偏{int} 1=开启 ,2=关闭
  20. //'srcFileFullPathArr' : new Array(), // 存放本次业务所拍摄的所有文档全路径的数组
  21. 'scannerCtrl' : null, // 扫描仪底层控制驱动
  22. 'fso' : new ActiveXObject('Scripting.FileSystemObject'), // JS文件操作对象
  23. 'scannerCtrlInitHtml' : '<%-- ActiveX控件对象 --%>'
  24. + '<object id="scannerCtrl" '
  25. + 'style="width: 600px; height:600px; "'
  26. + 'align="middle" CODEBASE="/web/jsp/shoudan/scanner/DocuScan.CAB#version=1,0,0,9"'
  27. + 'classid="CLSID:3C281245-3378-428E-B15B-9CF5251F1568"> '
  28. + '<PARAM NAME="EnableToolBar" VALUE="false" /> </object>'
  29. },
  30. getParameter : function(key) {
  31. return this._parameter[key];
  32. },
  33. setParameter : function(key, value) {
  34. this._parameter[key] = value;
  35. },
  36. /**
  37. * 【扫描】接口方法
  38. */
  39. scan : function() {
  40. this.setParameter("prefix", this._executeFunc(this.getParameter("produceFilenameFunc")));
  41. // 进行扫描的参数设置
  42. this._changeDPI(this.getParameter("dpi")); // 设置扫描质量dpi
  43. this._changeSaveType(this.getParameter("saveType")); // 设置扫描影像的格式
  44. this._changeColor(this.getParameter("colorType")); // 设置扫描影像色彩
  45. this._changeDoubleSide(this.getParameter("isDoubleSide")); // 设置是否开启双面扫描设置
  46. this._changeDelNullPage(this.getParameter("isDelNullPage")); // 设置是否开启去除空白页面
  47. this._changeRoteWord(this.getParameter("isRoteWord")); // 设置是否开启识别文字方向
  48. this._changeRectification(this.getParameter("isRectification")); // 设置是否开启纠偏
  49. // 进行扫描的路径设置
  50. this._changeSavePath(this.getParameter("locationPath")); // 设置扫描文件的存放路径
  51. this._changePreFix(this.getParameter("prefix")); // 设置扫描文件的名称
  52. var locationPath = this.getParameter("locationPath");
  53. var flag = this._createDestSavePath(locationPath); // 创建文件存放路径是否成功标志
  54. if(flag){
  55. this._startScan();
  56. }else{// 创建文件夹失败,导致目标文件夹不存在
  57. alert("目标存放文件夹不存在!");
  58. }
  59. },
  60. /**
  61. * 【重新扫描】接口方法
  62. * @introduction 点击【重新扫描】后,删除已有的扫描文件后再进行一次扫描。先调用控件提供的全部删除方法,后使用自身的删除私有方法
  63. */
  64. rescan : function() {
  65. this._dellAll(); // 调用控件的全部删除方法
  66. this._deleteLocalTempFile(this._getPath()); // 调用自身的删除方法再次进行删除
  67. // 再次进行扫描
  68. this.scan();
  69. },
  70. /**
  71. * 【获取文件路径】接口方法
  72. *
  73. * @return String[] 返回扫描文件在本地上的储存路径的数组
  74. */
  75. getFilePath : function() {
  76. return this._getPath();
  77. },
  78. /**
  79. * 【设备状态初始化】公用方法
  80. * @introduction 进入页面时触发的初始化动作
  81. */
  82. initialization : function() {
  83. //scanner._initSettingDiv();
  84. },
  85. /**
  86. * 【设备状态反初始化】公用方法
  87. * @introduction 离开页面是触发的反初始化动作
  88. */
  89. deinitialization : function() {
  90. this._dellAll(); // 删除所有的本地临时文件
  91. this._deleteLocalTempFile(this._getPath()); // 调用自身的删除方法再次进行删除
  92. },
  93. /**
  94. * 【打开设置窗口】接口方法
  95. *
  96. * @introduction
  97. */
  98. openSetting : function() {
  99. $('#settingDiv').fwwindow('open');
  100. // <Object>标签会遮挡无窗体元素,所以要在在弹出窗的遮罩层下垫底一块透明的iFrame,让弹出窗高于<Object>,缺点就是<Object>会被完全挡掉。
  101. $(".fwwindow-mask").append(
  102. "<iframe style='position: absolute; z-index: -1; width: 100%; height: 100%; " +
  103. "top: 0;left:0;scrolling:no;' frameborder='0'></iframe>");
  104. },
  105. /**
  106. * 【初始化设置Div窗口】私有方法
  107. */
  108. _initSettingDiv : function() {
  109. $('#settingDiv').fwwindow({
  110. title : '设置',
  111. width : 200,
  112. modal : true,
  113. closed : true,
  114. height : 200
  115. });
  116. },
  117. /**
  118. * 【执行入参方法】私有方法
  119. *
  120. * @param value=入参为一个方法结构体
  121. */
  122. _executeFunc : function(value) {
  123. var produceFilenameFunc = value;
  124. return produceFilenameFunc();
  125. },
  126. /**
  127. * 【扫描】私有方法
  128. */
  129. _startScan : function() {
  130. scannerCtrl.BeginScan();
  131. },
  132. /**
  133. * 【插扫】私有方法
  134. */
  135. _insertScan : function() {
  136. scannerCtrl.InsertScan();
  137. },
  138. /**
  139. * 【重扫】私有方法
  140. */
  141. _reScan : function() {
  142. scannerCtrl.ReScan();
  143. },
  144. /**
  145. * 【扫描设置】私有方法
  146. */
  147. _scanSetting : function() {
  148. scannerCtrl.ScanSetting();
  149. },
  150. /**
  151. * 【删除】私有方法
  152. */
  153. _delItem : function() {
  154. scannerCtrl.DelItem();
  155. },
  156. /**
  157. * 【全部删除】私有方法
  158. */
  159. _dellAll : function() {
  160. scannerCtrl.DellAll();
  161. },
  162. /**
  163. * 【获取文件路径】私有方法
  164. */
  165. _getPath : function() {
  166. var allFileFullPathArr = scannerCtrl.GetALLFile();
  167. allFileFullPathArr = allFileFullPathArr.split("|");
  168. return allFileFullPathArr;
  169. },
  170. /**
  171. * 【打印】私有方法
  172. */
  173. _printFile : function() {
  174. scannerCtrl.PrintFile("D:\\1\\1.bmp|D:\\1\\2.bmp");
  175. // 可以通过
  176. // scannerCtrl.Rectification = 2;
  177. // 这样的方式修改属性
  178. },
  179. /**
  180. * 根据文件夹路径创建该文件夹
  181. * return {boolean} true=创建成功 false=创建失败
  182. */
  183. _createDestSavePath : function(path) {
  184. var flag = null; //创建是否成功标志
  185. //创建JavaScript的本地文件操作对象
  186. var fso = new ActiveXObject("Scripting.FileSystemObject");
  187. //判断该文件夹是否已经存在。若存在则返回创建成功,不存在则创建文件夹(多级文件夹创建)
  188. if (!fso.FolderExists(path)) { //该文件夹不存在
  189. //多级文件夹目录创建
  190. var folderFragment = path.split("\\");
  191. //待创建的文件夹路径,每循环一次则添加一次子级文件夹的名称,从而进入子级文件夹的创建
  192. var folderPath = "";
  193. for ( var index in folderFragment) {
  194. folderPath = folderPath + folderFragment[index] + "\\";
  195. //一级一级判断该级文件夹是否存在,不存在则创建
  196. if (!fso.FolderExists(folderPath)) {
  197. fso.CreateFolder(folderPath);
  198. }
  199. }
  200. }
  201. //判断是否该文件夹是否创建成功
  202. if (fso.FolderExists(path)) {
  203. flag = true;
  204. return flag;
  205. } else {
  206. flag = false;
  207. return flag;
  208. }
  209. },
  210. /**
  211. * 【生成目标文件的全路径】私有方法
  212. *
  213. * return {string} 目标文件的全路径
  214. *//*
  215. _makeFileFullName : function() {
  216. var locationPath = this.getParameter("locationPath"); // 目标文件存放路径
  217. var postfix = this.getParameter("postfix"); // 目标文件生成格式后缀
  218. var filename = this._executeFunc(this.getParameter("produceFilenameFunc")); // 目标文件名称
  219. var fileFullName = locationPath + filename + postfix; // 目标文件全路径
  220. return fileFullName;
  221. },*/
  222. /**
  223. * 【JS删除文件操作】私有方法
  224. * @param fileFullPath=文件的全路径
  225. */
  226. _deleteFile : function(fileFullPath) {
  227. var fso = this.getParameter("fso");
  228. if (fso.FileExists(fileFullPath)) {
  229. fso.DeleteFile(fileFullPath);
  230. }
  231. },
  232. /**
  233. * 【删除所有本地所有的扫描文件】私有方法
  234. */
  235. _deleteLocalTempFile : function(srcFileFullPathArr){
  236. for(var index = 0; index < srcFileFullPathArr.length; index++){
  237. this._deleteFile(srcFileFullPathArr[index]);
  238. this.setParameter("srcFileFullPathArr", []);// 本次业务存放扫描件的数据也要对应清空
  239. }
  240. },
  241. /**
  242. * 【改变扫描文件的存放路径】私有方法
  243. * @param locationPath = 文件的存放文件
  244. */
  245. _changeSavePath : function(locationPath){
  246. scannerCtrl.SavePath = locationPath;
  247. },
  248. /**
  249. * 【改变扫描文件的名称】私有方法
  250. * @param {string} preFix = 文件名称
  251. */
  252. _changePreFix : function(preFix){
  253. scannerCtrl.PreFix = preFix;
  254. },
  255. /**
  256. * 【改变是保存文件的格式】私有方法
  257. * @param {int} 1:JPG 2:Tif 3:Pdf
  258. */
  259. _changeSaveType : function(type){
  260. scannerCtrl.SaveType = type;
  261. },
  262. /**
  263. * 【改变扫描影像色彩】私有方法
  264. * @param {int} 1=黑白 2=灰度 3=彩色
  265. */
  266. _changeColor : function(colorType){
  267. scannerCtrl.Colour = colorType;
  268. },
  269. /**
  270. * 【改变是否开启双面扫描设置】私有方法
  271. * @param {int} 2=开启, 1=关闭
  272. */
  273. _changeDoubleSide : function(isDoubleSide){
  274. scannerCtrl.DoubleSide = isDoubleSide;
  275. },
  276. /**
  277. * 【改变是否开启去除空白页面】私有方法
  278. * @param {int} 1=开启 2=关闭
  279. */
  280. _changeDelNullPage : function(isDelNullPage){
  281. scannerCtrl.DelNullPage = isDelNullPage;
  282. },
  283. /**
  284. * 【改变是否开启识别文字方向】私有方法 #
  285. * @param {int} 1=开启 2=关闭
  286. */
  287. _changeRoteWord : function(isRoteWord){
  288. scannerCtrl.RoteWord = isRoteWord;
  289. },
  290. /**
  291. * 【改变DPI大小】私有方法 #
  292. * @param {int} 1=开启 2=关闭
  293. */
  294. _changeDPI : function(dpi){
  295. scannerCtrl.DPI = dpi;
  296. },
  297. /**
  298. * 【改变是否开启纠偏】私有方法
  299. * @param {int} 1=开启 2=关闭
  300. */
  301. _changeRectification : function(isRectification){
  302. scannerCtrl.Rectification = isRectification;
  303. }
  304. };
  305. // 扫描仪操作对象设置并初始化
  306. Scanner.config = function(initParam) {
  307. var scanner = new Scanner();
  308. scanner.setParameter('domId', initParam['domId']);
  309. scanner.setParameter('locationPath', initParam['locationPath']);
  310. scanner.setParameter('produceFilenameFunc', initParam['produceFilenameFunc']);
  311. /*$('#' + scanner.getParameter('domId')).html(
  312. scanner.getParameter('scannerCtrlInitHtml'));*/
  313. return scanner;
  314. };
  315. })();