capturer.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. // 高拍仪操作对象
  2. var Capturer = function() {
  3. };
  4. (function() {
  5. Capturer.prototype = {
  6. construct : Capturer,
  7. // 私有属性
  8. _parameter : {
  9. 'domId' : null, // 控件容器ID
  10. 'locationPath' : null, // 拍摄文件的本地存储路径
  11. 'produceFilenameFunc' : null, // 生成拍摄文件名称的函数体
  12. 'filename' : null, // 拍摄文件名称
  13. 'capturerCtrl' : null, // 高拍仪底层控制驱动
  14. 'iDeviceIndex' : null, // 高拍仪的摄像头编号
  15. 'postfix' : '.jpg', // 生成文件的格式
  16. 'strFileNames' : new Array(), // 双摄像头模式下,存放三张图片全路径的数组
  17. 'strMergeSource1' : null, // 合并图像的源文件1
  18. 'strMergeSource2' : null, // 合并图像的源文件2
  19. 'srcFileFullPathArr' : new Array(), // 存放本次业务所拍摄的所有文档全路径的数组
  20. 'fso' : new ActiveXObject('Scripting.FileSystemObject'), // JS文件操作对象
  21. 'capturerCtrlInitHtml' : '<%-- ActiveX控件对象 --%>'
  22. + '<object id="capturerCtrl" class="capturerCtrl" style="width: 100%;height: 100%;" '
  23. + 'classid="clsid:9A73DB73-2CA3-478D-9A3F-7E9D6A8D327C" codebase="/web/jsp/shoudan/capturer/CaptureVideo.CAB#V1,1,1,4"></object>'
  24. },
  25. getParameter : function(key) {
  26. return this._parameter[key];
  27. },
  28. setParameter : function(key, value) {
  29. this._parameter[key] = value;
  30. },
  31. /**
  32. * 【扫描】接口方法
  33. * @introduction 该方法用于调用高拍仪拍摄文档
  34. * @return flag {boolean} true=拍摄成功 false=拍摄失败
  35. */
  36. scan : function() {
  37. if( 0 == this._getDeviceState()){ //设备处于正常状态
  38. // 执行该函数,获得本次扫描的文件名称
  39. var filename = this._executeFunc(this.getParameter("produceFilenameFunc"));
  40. this.setParameter("filename", filename);
  41. var flag = this._captureToFile();
  42. return flag;
  43. }else{ // 设备处于异常状态
  44. alert("设备处于异常状态!");
  45. }
  46. },
  47. /**
  48. * 【重新扫描】接口方法
  49. * @introduction 重新扫描,删除已拍摄的文件,再次调用拍摄
  50. * @return flag {boolean} true=拍摄成功 false=拍摄失败
  51. */
  52. rescan : function() {
  53. this._deleteLocalTempFile();
  54. var flag = this.scan(); //删除所有图像后徐重新再获取一张当前的界面图片
  55. return flag;
  56. },
  57. /**
  58. * 【切换镜头】接口方法
  59. * @introduction 切换镜头,方法可以切换文档摄像头和人性摄像头。 {int} 0=文档摄像头 1=人像摄像头
  60. */
  61. switchCamera : function() {
  62. this._closeDevice();
  63. var iDeviceIndex = this.getParameter('iDeviceIndex');
  64. this._changeColorType(0); // 设置拍摄图片的颜色:彩色
  65. if(0 == iDeviceIndex){ // 文档摄像头
  66. iDeviceIndex = 1; // 修改为“人像摄像头”
  67. this._openDevice(iDeviceIndex);
  68. this._changePersonCameraDpi(3);
  69. }else{ // 人像摄像头
  70. iDeviceIndex = 0; // 修改为“文档摄像头”
  71. this._openDevice(iDeviceIndex);
  72. this._changeDocumentCameraDpi(0);
  73. }
  74. },
  75. /**
  76. * 【获取文件路径】接口方法
  77. * @introduction 获取扫描文件在本地上的储存路径的数组
  78. * @return String[] 返回扫描文件在本地上的储存路径的数组
  79. */
  80. getFilePath : function() {
  81. return this.getParameter("srcFileFullPathArr");
  82. },
  83. /**
  84. * 【旋转镜头】接口方法
  85. * @introduction 依次对高拍仪的镜头进行旋转
  86. */
  87. rotateCamera : function(){
  88. this._setDeviceRotation(90);
  89. },
  90. /**
  91. * 【设备状态初始化】公用方法
  92. * @introduction 进入页面时触发的初始化动作
  93. */
  94. initialization : function() {
  95. this.setParameter("capturerCtrl", capturerCtrl);
  96. this._changeColorType(0); // 设置拍摄图片的颜色:彩色
  97. this._openDevice(0);
  98. },
  99. /**
  100. * 【设备状态反初始化】公用方法
  101. * @introduction 离开页面是触发的反初始化动作
  102. */
  103. deinitialization : function() {
  104. this._closeDevice();
  105. this._deleteLocalTempFile();//删除所有的本地临时文件
  106. },
  107. /**
  108. * 【打开设备】私有方法
  109. *
  110. * @param value=设备的编号(0=文档摄像头,1=人像摄像头,2=双摄像头}
  111. */
  112. _openDevice : function(value) {
  113. iDeviceIndex = parseInt(value); // 选择待启动的设备
  114. if(0 != capturerCtrl.GetDeviceState(iDeviceIndex)){ // 若设备异常关闭,首次进来时先关闭后再打开
  115. capturerCtrl.CloseDeviceEx();
  116. }
  117. this.setParameter('iDeviceIndex',iDeviceIndex); // 设置当前的摄像头
  118. if (capturerCtrl.OpenDevice(iDeviceIndex) == 0) {
  119. capturerCtrl.SetCameraExposure(iDeviceIndex, 10); // 设置自动曝光
  120. }
  121. },
  122. /**
  123. * 【关闭设备】私有方法
  124. */
  125. _closeDevice : function() {
  126. capturerCtrl.CloseDeviceEx();
  127. },
  128. /**
  129. * 【拍照并且存档】私有方法
  130. * @return flag {boolean} true=拍摄成功 false=拍摄失败
  131. */
  132. _captureToFile : function() {
  133. // 获取strFileNames数组对象用于存放“操作后”的图片的全路径
  134. var strFileNames = this.getParameter("strFileNames");
  135. // 新建一个strFileName数组对象用于存放”操作前“的图片全路径
  136. var strFileNameArr = new Array();
  137. // 所有拍摄后的图片都要放在数组,即该数组存放本次业务的所有扫描件路径
  138. var srcFileFullPathArr = this.getParameter("srcFileFullPathArr");
  139. var result = false; // 拍摄是否成功结果,默认为false
  140. if (iDeviceIndex != 2) {// 文档摄像头或人像摄像头
  141. // 扫描文档
  142. var strFileName = this.getParameter("locationPath")
  143. + this.getParameter("filename")
  144. + this.getParameter("postfix");
  145. // 拍摄图片
  146. result = capturerCtrl.GrabToFile(strFileName);
  147. strFileNameArr.push(strFileName);
  148. // 所有的扫描件的全路径都要记录在这个数组中
  149. srcFileFullPathArr.push(strFileName);
  150. } else { // 双摄像头模式[暂时废弃不用,没有做预览功能]
  151. // 数组第”1“个元素放置的是”两个摄像头组合而成“的图片
  152. strFileNameArr[0] = this.getParameter("locationPath")
  153. + this.getParameter("filename") + "-Combined"
  154. + this.getParameter("postfix"); // 组合摄像
  155. var strDocFileName = this.getParameter("locationPath")
  156. + this.getParameter("filename") + "-SplitedDoc"
  157. + this.getParameter("postfix"); // 文档摄像
  158. var strPerFileName = this.getParameter("locationPath")
  159. + this.getParameter("filename") + "-SplitedPer"
  160. + this.getParameter("postfix"); // 人像摄像
  161. // 数组第”2“个元素放置的是”两个摄像头分别拍摄的“的图片
  162. strFileNameArr[1] = strDocFileName + ";" + strPerFileName;
  163. for ( var index = 0; index < 2; index++) {
  164. if (capturerCtrl.GrabToFile(strFileNameArr[index]) == 0) {// 拍摄成功
  165. if (index == 0) {
  166. capturerCtrl.ImagesToMultiPageFile(
  167. strFileNameArr[index], 0);
  168. strFileNames.push(strFileNameArr[index]);
  169. // 只保存最后2次拍摄的文件名作为合并图像参数文件名
  170. //strMergeSource1 = strMergeSource2;
  171. //strMergeSource2 = strFileName[index];
  172. } else {
  173. capturerCtrl.ImagesToMultiPageFile(strDocFileName,
  174. 0);
  175. capturerCtrl.ImagesToMultiPageFile(strPerFileName,
  176. 0);
  177. strFileNames.push(strDocFileName);
  178. strFileNames.push(strPerFileName);
  179. // 摄像头拍摄模式下,保存最后2次拍摄的文件名(两个摄像头分别拍摄的文件)作为合并图像参数文件名
  180. this.setParameter("strMergeSource1", strDocFileName);
  181. this.setParameter("strMergeSource2", strPerFileName);
  182. }
  183. }
  184. }
  185. }
  186. var flag = this._changeResultFlag(result);
  187. return flag;
  188. },
  189. /**
  190. * 【摄像旋转】私有方法
  191. *
  192. * @param rotation=旋转角度
  193. */
  194. _setDeviceRotation : function(rotation) {
  195. var rotationScope = parseInt(rotation);
  196. if (rotationScope % 90 != 0) {
  197. capturerCtrl.SetDeviceRotate(iDeviceIndex, 0); // 不能被90整除的默认不旋转
  198. }
  199. capturerCtrl.SetDeviceRotate(iDeviceIndex, rotationScope);
  200. },
  201. /**
  202. * 【判断内容是否为数字】私有方法
  203. *
  204. * @param value=校验的内容
  205. */
  206. _isDigit : function(value) {
  207. var patrn = /^-?\d+(\.\d+)?$/;
  208. if (!patrn.exec(value)) {
  209. return false
  210. }
  211. return true;
  212. },
  213. /**
  214. * 【自动对焦(只对自动对焦的高拍仪有效)】私有方法
  215. */
  216. _triggerAutoFocuse : function() {
  217. capturerCtrl.TriggerAutoFocuse();
  218. },
  219. /**
  220. * 【去除阴影】私有方法
  221. */
  222. _reduceShadow : function() {
  223. if (checkReduceShadow.checked) {
  224. capturerCtrl.ReduceShadow(0);
  225. } else {
  226. capturerCtrl.ReduceShadow(1);
  227. }
  228. },
  229. /**
  230. * 【文档形变修补功能】私有方法
  231. */
  232. _repairDistortion : function() {
  233. if (checkRepairDistortion.checked) {
  234. capturerCtrl.RepairDistortion(0);
  235. } else {
  236. capturerCtrl.RepairDistortion(1);
  237. }
  238. },
  239. /**
  240. * 【连续拍摄】私有方法,暂时没用,故未完善
  241. */
  242. _startContinuousShooting : function() {
  243. var strFileName = "D:\\DocImage";
  244. capturerCtrl.StartContinuous(strFileName, 0);
  245. },
  246. /**
  247. * 【拍摄界面缩放】私有方法
  248. *
  249. * @param value=(0=缩小,1=放大)
  250. */
  251. _zoomVideo : function(value) {
  252. var iZoomType = parseInt(value);
  253. capturerCtrl.ZoomInOut(iZoomType);
  254. },
  255. /**
  256. * 【设置拍照存档的DPI】私有方法
  257. *
  258. * @param dpiValue=待设置的dpi值
  259. */
  260. _setDPI : function(dpiValue) {
  261. if (dpiValue == "") {
  262. alert("DPI值都不能为空!");
  263. return;
  264. }
  265. if (this._isDigit(dpiValue)) {
  266. iDpi = parseInt(dpiValue);
  267. capturerCtrl.SetGrabbedDPIEx(iDpi);
  268. } else {
  269. alert("含有非法字符,请重新输入数字!");
  270. }
  271. },
  272. /**
  273. * 【获取设备状态】私有方法
  274. * @return 返回设备状态的代号
  275. */
  276. _getDeviceState : function() {
  277. //alert(capturerCtrl.GetDeviceState(iDeviceIndex));
  278. switch (capturerCtrl.GetDeviceState(iDeviceIndex)) {
  279. case -1: {
  280. return -1;
  281. }
  282. case 0: {
  283. return 0;
  284. }
  285. case 1: {
  286. return 1;
  287. }
  288. case 2: {
  289. return 2;
  290. }
  291. default: {
  292. break;
  293. }
  294. }
  295. },
  296. /**
  297. * 【执行入参方法】私有方法
  298. * @param value=入参为一个方法结构体
  299. */
  300. _executeFunc : function(value) {
  301. var produceFilenameFunc = value;
  302. return produceFilenameFunc();
  303. },
  304. /**
  305. * 【JS删除文件操作】私有方法
  306. * @param fileFullPath=文件的全路径
  307. */
  308. _deleteFile : function(fileFullPath) {
  309. var fso = this.getParameter("fso");
  310. if (fso.FileExists(fileFullPath)) {
  311. fso.DeleteFile(fileFullPath);
  312. }
  313. },
  314. /**
  315. * 【删除所有本地所有的扫描文件】私有方法
  316. */
  317. _deleteLocalTempFile : function(){
  318. var srcFileFullPathArr = this.getParameter("srcFileFullPathArr");
  319. for(var index = 0; index < srcFileFullPathArr.length; index++){
  320. this._deleteFile(srcFileFullPathArr[index]);
  321. this.setParameter("srcFileFullPathArr", []); // 本次业务存放扫描件的数据也要对应清空
  322. }
  323. },
  324. /**
  325. * 【改变拍摄结果返回值方法】私有方法
  326. * @introduction 由于高拍仪返回结果是数字代码,0表示的是成功,其它数字表示其它错误
  327. * @return flag {boolean} true=拍摄成功 false=拍摄失败
  328. */
  329. _changeResultFlag : function(resultFlag){
  330. return resultFlag == 0 ? true : false;
  331. },
  332. /**
  333. * 【改变拍摄图片颜色】私有方法
  334. * @param colorType {int} 0=彩色 1=灰度
  335. */
  336. _changeColorType : function(colorType){
  337. capturerCtrl.SetColorMode(colorType);
  338. },
  339. /**
  340. * 【改变文档摄像头的分辨率】私有方法
  341. * @param value {int}
  342. * 0 = 2592*1944
  343. * 1 = 1280*960
  344. * 2 = 1920*1080
  345. * 3 = 1600*1200
  346. * 4 = 2048*1536
  347. */
  348. _changeDocumentCameraDpi : function(value){
  349. var iIndex = parseInt(value);
  350. var iW = capturerCtrl.GetResolutionWidth(iDeviceIndex, iIndex);
  351. var iH = capturerCtrl.GetResolutionHeight(iDeviceIndex, iIndex);
  352. //console.log(iW + "*" + iH );
  353. var test = capturerCtrl.SetResolutionByWH(this.getParameter("iDeviceIndex"), iW, iH);
  354. },
  355. /**
  356. * 【改变人像摄像头的分辨率】私有方法
  357. * @param value {int}
  358. * 0 = 640*480
  359. * 1 = 800*600
  360. * 2 = 1280*720
  361. * 3 = 1280*960
  362. * 4 = 1600*1200
  363. * 5 = 320*240
  364. */
  365. _changePersonCameraDpi : function(value){
  366. var iIndex = parseInt(value);
  367. var iW = capturerCtrl.GetResolutionWidth(iDeviceIndex, iIndex);
  368. var iH = capturerCtrl.GetResolutionHeight(iDeviceIndex, iIndex);
  369. //console.log(iW + "*" + iH );
  370. var test = capturerCtrl.SetResolutionByWH(this.getParameter("iDeviceIndex"), iW, iH);
  371. }
  372. };
  373. // 扫描仪操作对象设置并初始化
  374. Capturer.config = function(initParam) {
  375. var capturer = new Capturer();
  376. capturer.setParameter('domId', initParam['domId']);
  377. capturer.setParameter('locationPath', initParam['locationPath']);
  378. capturer.setParameter('produceFilenameFunc', initParam['produceFilenameFunc']);
  379. $('#' + capturer.getParameter('domId')).html(
  380. capturer.getParameter('capturerCtrlInitHtml'));
  381. return capturer;
  382. };
  383. })();