|
|
AIWROKÈí¼þÕÒ×ÖÓëOCR·½·¨»ã×ÜʾÀý
- // ÕÒ×ÖÓëOCR·½·¨»ã×ÜʾÀý
- // ¼æÈÝES5ºÍRhinoÒýÇæ
- // ÊÊÓÃÓÚ°²×¿AIWROK»·¾³
- // ¼¼Êõ½»Á÷QQȺ711841924Ⱥһ£¬Æ»¹ûÄÚ²âȺ£¬528816639
- /**
- * Ä£¿é£º»ù´¡ÕÒ×Ö·½·¨
- * ¹¦ÄÜ£ºÌṩ¶àÖÖÎÄ×Ö²éÕÒ·½·¨
- */
- var FindTextMethods = {
- /**
- * ·½·¨1£º»ù´¡ÕÒ×Ö·½·¨
- * @param {string} text - Òª²éÕÒµÄÎÄ×Ö
- * @param {number} timeout - ³¬Ê±Ê±¼ä£¨ºÁÃ룩
- * @returns {Object} ÕÒµ½µÄÎÄ×ÖλÖÃÐÅÏ¢
- */
- findTextBasic: function(text, timeout) {
- timeout = timeout || 5000;
- var start = new Date().getTime();
- var result;
-
- while (new Date().getTime() - start < timeout) {
- try {
- if (typeof auto !== 'undefined' && typeof auto.findText === 'function') {
- result = auto.findText(text, 0.8);
- if (result) {
- return result;
- }
- }
- } catch (e) {
- // ºöÂÔ´íÎ󣬼ÌÐø³¢ÊÔ
- }
- sleep.millisecond(100);
- }
-
- return null;
- },
-
- /**
- * ·½·¨2£ºÇøÓòÕÒ×Ö·½·¨
- * @param {string} text - Òª²éÕÒµÄÎÄ×Ö
- * @param {number} x1 - ÇøÓò×óÉϽÇX×ø±ê
- * @param {number} y1 - ÇøÓò×óÉϽÇY×ø±ê
- * @param {number} x2 - ÇøÓòÓÒϽÇX×ø±ê
- * @param {number} y2 - ÇøÓòÓÒϽÇY×ø±ê
- * @returns {Object} ÕÒµ½µÄÎÄ×ÖλÖÃÐÅÏ¢
- */
- findTextInRegion: function(text, x1, y1, x2, y2) {
- try {
- if (typeof auto !== 'undefined' && typeof auto.findText === 'function') {
- var region = [x1, y1, x2, y2];
- var result = auto.findText(text, 0.8, region);
- return result;
- }
- } catch (e) {
- // ºöÂÔ´íÎó
- }
- return null;
- },
-
- /**
- * ·½·¨3£ºÄ£ºýÕÒ×Ö·½·¨
- * @param {string} text - Òª²éÕÒµÄÎÄ×Ö
- * @param {number} similarity - ÏàËÆ¶È£¨0-1£©
- * @returns {Object} ÕÒµ½µÄÎÄ×ÖλÖÃÐÅÏ¢
- */
- findTextFuzzy: function(text, similarity) {
- similarity = similarity || 0.8;
- try {
- if (typeof auto !== 'undefined' && typeof auto.findText === 'function') {
- var result = auto.findText(text, similarity);
- return result;
- }
- } catch (e) {
- // ºöÂÔ´íÎó
- }
- return null;
- },
-
- /**
- * ·½·¨4£º¶à´Î³¢ÊÔÕÒ×Ö·½·¨
- * @param {string} text - Òª²éÕÒµÄÎÄ×Ö
- * @param {number} maxAttempts - ×î´ó³¢ÊÔ´ÎÊý
- * @param {number} interval - ³¢ÊÔ¼ä¸ô£¨ºÁÃ룩
- * @returns {Object} ÕÒµ½µÄÎÄ×ÖλÖÃÐÅÏ¢
- */
- findTextWithRetry: function(text, maxAttempts, interval) {
- maxAttempts = maxAttempts || 5;
- interval = interval || 1000;
-
- for (var i = 0; i < maxAttempts; i++) {
- try {
- if (typeof auto !== 'undefined' && typeof auto.findText === 'function') {
- var result = auto.findText(text, 0.8);
- if (result) {
- return result;
- }
- }
- } catch (e) {
- // ºöÂÔ´íÎ󣬼ÌÐø³¢ÊÔ
- }
- sleep.millisecond(interval);
- }
-
- return null;
- },
-
- /**
- * ·½·¨5£ºÅúÁ¿ÕÒ×Ö·½·¨
- * @param {Array} texts - Òª²éÕÒµÄÎÄ×ÖÊý×é
- * @returns {Object} ÕÒµ½µÄµÚÒ»¸öÎÄ×ÖλÖÃÐÅÏ¢
- */
- findTextBatch: function(texts) {
- for (var i = 0; i < texts.length; i++) {
- try {
- if (typeof auto !== 'undefined' && typeof auto.findText === 'function') {
- var result = auto.findText(texts[i], 0.8);
- if (result) {
- return {
- text: texts[i],
- position: result
- };
- }
- }
- } catch (e) {
- // ºöÂÔ´íÎ󣬼ÌÐø³¢ÊÔÏÂÒ»¸öÎÄ×Ö
- }
- }
-
- return null;
- },
-
- /**
- * ·½·¨6£ºÕÒ×Ö²¢µã»÷·½·¨
- * @param {string} text - Òª²éÕÒ²¢µã»÷µÄÎÄ×Ö
- * @param {number} timeout - ³¬Ê±Ê±¼ä£¨ºÁÃ룩
- * @returns {boolean} ÊÇ·ñ³É¹¦µã»÷
- */
- findTextAndClick: function(text, timeout) {
- var result = this.findTextBasic(text, timeout);
- if (result) {
- var x = result.x + result.width / 2;
- var y = result.y + result.height / 2;
- // Ìí¼ÓËæ»úÆ«ÒÆÄ£ÄâÕæÊµµã»÷
- var randomX = x + (Math.random() - 0.5) * 10;
- var randomY = y + (Math.random() - 0.5) * 10;
- try {
- if (typeof touch !== 'undefined' && typeof touch.tap === 'function') {
- touch.tap(randomX, randomY);
- return true;
- }
- } catch (e) {
- // ºöÂÔ´íÎó
- }
- }
- return false;
- },
-
- /**
- * ·½·¨7£ºµÈ´ýÎÄ×Ö³öÏÖ·½·¨
- * @param {string} text - ÒªµÈ´ýµÄÎÄ×Ö
- * @param {number} timeout - ³¬Ê±Ê±¼ä£¨Ã룩
- * @returns {boolean} ÊÇ·ñÔÚ³¬Ê±Ç°ÕÒµ½ÎÄ×Ö
- */
- waitForText: function(text, timeout) {
- timeout = timeout || 30;
- var startTime = Date.now();
- var timeoutMs = timeout * 1000;
-
- while (Date.now() - startTime < timeoutMs) {
- try {
- if (typeof auto !== 'undefined' && typeof auto.findText === 'function') {
- var result = auto.findText(text, 0.8);
- if (result) {
- return true;
- }
- }
- } catch (e) {
- // ºöÂÔ´íÎ󣬼ÌÐø³¢ÊÔ
- }
- sleep.millisecond(1000);
- }
-
- return false;
- }
- };
- /**
- * Ä£¿é£ºOCR·½·¨
- * ¹¦ÄÜ£ºÌṩ¶àÖÖOCRÎÄ×Öʶ±ð·½·¨
- */
- var OCRMethods = {
- /**
- * ·½·¨1£º»ù´¡OCRʶ±ð
- * @param {number} x1 - ÇøÓò×óÉϽÇX×ø±ê
- * @param {number} y1 - ÇøÓò×óÉϽÇY×ø±ê
- * @param {number} x2 - ÇøÓòÓÒϽÇX×ø±ê
- * @param {number} y2 - ÇøÓòÓÒϽÇY×ø±ê
- * @returns {string} ʶ±ðµ½µÄÎÄ×Ö
- */
- ocrBasic: function(x1, y1, x2, y2) {
- try {
- if (typeof ocr === 'function') {
- var result = ocr(x1, y1, x2, y2);
- return result;
- }
- } catch (e) {
- // ºöÂÔ´íÎ󣬷µ»Ø¿Õ×Ö·û´®
- }
- return "";
- },
-
- /**
- * ·½·¨2£º´øÓïÑÔÉèÖõÄOCRʶ±ð
- * @param {number} x1 - ÇøÓò×óÉϽÇX×ø±ê
- * @param {number} y1 - ÇøÓò×óÉϽÇY×ø±ê
- * @param {number} x2 - ÇøÓòÓÒϽÇX×ø±ê
- * @param {number} y2 - ÇøÓòÓÒϽÇY×ø±ê
- * @param {string} lang - ÓïÑÔÉèÖÃ
- * @returns {string} ʶ±ðµ½µÄÎÄ×Ö
- */
- ocrWithLanguage: function(x1, y1, x2, y2, lang) {
- try {
- if (typeof ocr === 'function') {
- lang = lang || "chi_sim";
- var result = ocr(x1, y1, x2, y2, lang);
- return result;
- }
- } catch (e) {
- // ºöÂÔ´íÎ󣬷µ»Ø¿Õ×Ö·û´®
- }
- return "";
- },
-
- /**
- * ·½·¨3£ºÈ«ÆÁOCRʶ±ð
- * @returns {string} ʶ±ðµ½µÄÎÄ×Ö
- */
- ocrFullScreen: function() {
- try {
- if (typeof ocr === 'function') {
- var screenWidth = 0;
- var screenHeight = 0;
- try {
- if (typeof screen !== 'undefined' && typeof screen.getScreenWidth === 'function') {
- screenWidth = screen.getScreenWidth();
- screenHeight = screen.getScreenHeight();
- }
- } catch (e) {
- // ºöÂÔ´íÎó£¬Ê¹ÓÃĬÈÏÖµ
- }
- var result = ocr(0, 0, screenWidth, screenHeight);
- return result;
- }
- } catch (e) {
- // ºöÂÔ´íÎ󣬷µ»Ø¿Õ×Ö·û´®
- }
- return "";
- },
-
- /**
- * ·½·¨4£ºOCRʶ±ð²¢²éÕÒÌØ¶¨ÎÄ×Ö
- * @param {number} x1 - ÇøÓò×óÉϽÇX×ø±ê
- * @param {number} y1 - ÇøÓò×óÉϽÇY×ø±ê
- * @param {number} x2 - ÇøÓòÓÒϽÇX×ø±ê
- * @param {number} y2 - ÇøÓòÓÒϽÇY×ø±ê
- * @param {string} keyword - Òª²éÕҵĹؼü×Ö
- * @returns {boolean} ÊÇ·ñÕÒµ½¹Ø¼ü×Ö
- */
- ocrAndFindKeyword: function(x1, y1, x2, y2, keyword) {
- try {
- var text = this.ocrBasic(x1, y1, x2, y2);
- return text.indexOf(keyword) !== -1;
- } catch (e) {
- // ºöÂÔ´íÎ󣬷µ»Øfalse
- }
- return false;
- },
-
- /**
- * ·½·¨5£ºOCRʶ±ð²¢ÌáÈ¡Êý×Ö
- * @param {number} x1 - ÇøÓò×óÉϽÇX×ø±ê
- * @param {number} y1 - ÇøÓò×óÉϽÇY×ø±ê
- * @param {number} x2 - ÇøÓòÓÒϽÇX×ø±ê
- * @param {number} y2 - ÇøÓòÓÒϽÇY×ø±ê
- * @returns {Array} ÌáÈ¡µ½µÄÊý×ÖÊý×é
- */
- ocrExtractNumbers: function(x1, y1, x2, y2) {
- try {
- var text = this.ocrBasic(x1, y1, x2, y2);
- var numbers = text.match(/\d+/g) || [];
- return numbers;
- } catch (e) {
- // ºöÂÔ´íÎ󣬷µ»Ø¿ÕÊý×é
- }
- return [];
- },
-
- /**
- * ·½·¨6£ºOCRʶ±ð²¢½âÎö×ø±ê
- * @param {number} x1 - ÇøÓò×óÉϽÇX×ø±ê
- * @param {number} y1 - ÇøÓò×óÉϽÇY×ø±ê
- * @param {number} x2 - ÇøÓòÓÒϽÇX×ø±ê
- * @param {number} y2 - ÇøÓòÓÒϽÇY×ø±ê
- * @returns {Array} ½âÎöµ½µÄ×ø±êÊý×é
- */
- ocrExtractCoordinates: function(x1, y1, x2, y2) {
- try {
- var text = this.ocrBasic(x1, y1, x2, y2);
- var coords = [];
- var regex = /(\d+),(\d+)/g;
- var match;
-
- while ((match = regex.exec(text)) !== null) {
- coords.push({
- x: parseInt(match[1], 10),
- y: parseInt(match[2], 10)
- });
- }
-
- return coords;
- } catch (e) {
- // ºöÂÔ´íÎ󣬷µ»Ø¿ÕÊý×é
- }
- return [];
- },
-
- /**
- * ·½·¨7£ºOCRʶ±ð²¢ÌáÈ¡ÓÊÏä
- * @param {number} x1 - ÇøÓò×óÉϽÇX×ø±ê
- * @param {number} y1 - ÇøÓò×óÉϽÇY×ø±ê
- * @param {number} x2 - ÇøÓòÓÒϽÇX×ø±ê
- * @param {number} y2 - ÇøÓòÓÒϽÇY×ø±ê
- * @returns {Array} ÌáÈ¡µ½µÄÓÊÏäÊý×é
- */
- ocrExtractEmails: function(x1, y1, x2, y2) {
- try {
- var text = this.ocrBasic(x1, y1, x2, y2);
- var emails = text.match(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g) || [];
- return emails;
- } catch (e) {
- // ºöÂÔ´íÎ󣬷µ»Ø¿ÕÊý×é
- }
- return [];
- },
-
- /**
- * ·½·¨8£ºOCRʶ±ð²¢ÌáÈ¡URL
- * @param {number} x1 - ÇøÓò×óÉϽÇX×ø±ê
- * @param {number} y1 - ÇøÓò×óÉϽÇY×ø±ê
- * @param {number} x2 - ÇøÓòÓÒϽÇX×ø±ê
- * @param {number} y2 - ÇøÓòÓÒϽÇY×ø±ê
- * @returns {Array} ÌáÈ¡µ½µÄURLÊý×é
- */
- ocrExtractURLs: function(x1, y1, x2, y2) {
- try {
- var text = this.ocrBasic(x1, y1, x2, y2);
- var urls = text.match(/https?:\/\/[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/g) || [];
- return urls;
- } catch (e) {
- // ºöÂÔ´íÎ󣬷µ»Ø¿ÕÊý×é
- }
- return [];
- },
-
- /**
- * ·½·¨9£º»ù´¡ÎÄ×Ö²éѯ·½·¨ - ²éѯ°üº¬ÎÄ×Ö
- * @param {string} text - Òª²éѯµÄÎÄ×Ö
- * @returns {object|null} ʶ±ðµ½µÄÄ¿±ê¶ÔÏó»ònull
- */
- findIncludeText: function(text) {
- try {
- var result = new ocrResult().findIncludeText(text);
- return result;
- } catch (e) {
- // ºöÂÔ´íÎ󣬷µ»Ønull
- }
- return null;
- },
-
- /**
- * ·½·¨10£º»ù´¡ÎÄ×Ö²éѯ·½·¨ - ²éѯ°üº¬ÎÄ×Ö£¨¶àÄ¿±ê£©
- * @param {string} text - Òª²éѯµÄÎÄ×Ö
- * @returns {Array} ʶ±ðµ½µÄÄ¿±ê¶ÔÏóÊý×é
- */
- findIncludeTexts: function(text) {
- try {
- var result = new ocrResult().findIncludeTexts(text);
- return result;
- } catch (e) {
- // ºöÂÔ´íÎ󣬷µ»Ø¿ÕÊý×é
- }
- return [];
- },
-
- /**
- * ·½·¨11£º»ù´¡ÎÄ×Ö²éѯ·½·¨ - Ä£ºý²éѯ£¨½üËÆÆ¥Å䣩
- * @param {string} text - Òª²éѯµÄÎÄ×Ö
- * @returns {object|null} ʶ±ðµ½µÄÄ£ºýÆ¥ÅäÄ¿±ê»ònull
- */
- findSimText: function(text) {
- try {
- var img = screen.screenShotFull();
- var ocrres = img.MLKitOcr('zh');
- var result = ocrres.findSimText(text);
- img.recycle();
- return result;
- } catch (e) {
- // ºöÂÔ´íÎ󣬷µ»Ønull
- }
- return null;
- },
-
- /**
- * ·½·¨12£º»ù´¡ÎÄ×Ö²éѯ·½·¨ - ¾«È·²éѯÎÄ×Ö
- * @param {string} text - Òª²éѯµÄÎÄ×Ö
- * @returns {object|null} ʶ±ðµ½µÄ¾«È·Ä¿±ê»ònull
- */
- findText: function(text) {
- try {
- var img = screen.screenShotFull();
- var ocrres = img.MLKitOcr('zh');
- var result = ocrres.findText(text);
- img.recycle();
- return result;
- } catch (e) {
- // ºöÂÔ´íÎ󣬷µ»Ønull
- }
- return null;
- },
-
- /**
- * ·½·¨13£º»ù´¡ÎÄ×Ö²éѯ·½·¨ - »ñÈ¡ËùÓÐʶ±ðÄ¿±ê
- * @returns {Array} ËùÓÐʶ±ðÄ¿±êµÄÊý×é
- */
- getAllDetect: function() {
- try {
- var result = new ocrResult().getAllDetect();
- return result;
- } catch (e) {
- // ºöÂÔ´íÎ󣬷µ»Ø¿ÕÊý×é
- }
- return [];
- },
-
- /**
- * ·½·¨14£º»ù´¡ÎÄ×Ö²éѯ·½·¨ - »ñÈ¡ËùÓÐʶ±ðÎÄ×Ö
- * @returns {string} ËùÓÐʶ±ðÎÄ×ֵį´½Ó½á¹û
- */
- getAllString: function() {
- try {
- var result = new ocrResult().getAllString();
- return result;
- } catch (e) {
- // ºöÂÔ´íÎ󣬷µ»Ø¿Õ×Ö·û´®
- }
- return "";
- },
-
- /**
- * ·½·¨15£º»ù´¡ÎÄ×Ö²éѯ·½·¨ - »ñÈ¡JSON¸ñʽ½á¹û
- * @returns {string} OCRʶ±ð½á¹ûµÄJSON×Ö·û´®
- */
- getJson: function() {
- try {
- var result = new ocrResult().getJson();
- return result;
- } catch (e) {
- // ºöÂÔ´íÎ󣬷µ»Ø¿Õ×Ö·û´®
- }
- return "";
- },
-
- /**
- * ·½·¨16£º»ù´¡ÎÄ×Ö²éѯ·½·¨ - »ñÈ¡JSON×Ö·û´®
- * @returns {string} OCRʶ±ð½á¹ûµÄJSON×Ö·û´®
- */
- getJsonString: function() {
- try {
- var result = new ocrResult().getJsonString();
- return result;
- } catch (e) {
- // ºöÂÔ´íÎ󣬷µ»Ø¿Õ×Ö·û´®
- }
- return "";
- },
-
- /**
- * ·½·¨17£ººËÐÄʶ±ð·½·¨ - MLKitOcrÎÄ×Öʶ±ð£¨Google MLKitÒýÇæ£©
- * @param {string} lang - ʶ±ðÓïÑÔ£¬Ä¬ÈÏ"zh"
- * @returns {object|null} OCRʶ±ð½á¹û¶ÔÏó»ònull
- */
- MLKitOcr: function(lang) {
- try {
- lang = lang || "zh";
- var img = screen.screenShotFull();
- var result = img.MLKitOcr(lang);
- img.recycle();
- return result;
- } catch (e) {
- console.log("MLKitOcr error:", e.message);
- return null;
- }
- },
-
- /**
- * ·½·¨18£ººËÐÄʶ±ð·½·¨ - MLKitOcrRectÎÄ×ÖÇøÓòʶ±ð
- * @param {string} lang - ʶ±ðÓïÑÔ£¬Ä¬ÈÏ"zh"
- * @param {Array} region - ʶ±ðÇøÓòµÄ°Ù·Ö±È×ø±ê [x1, y1, x2, y2]
- * @returns {object|null} OCRʶ±ð½á¹û¶ÔÏó»ònull
- */
- MLKitOcrRect: function(lang, region) {
- try {
- lang = lang || "zh";
- region = region || [0, 0, 1, 1];
- var img = screen.screenShotFull();
- var result = img.MLKitOcrRect(lang, region);
- img.recycle();
- return result;
- } catch (e) {
- console.log("MLKitOcrRect error:", e.message);
- return null;
- }
- },
-
- /**
- * ·½·¨19£ººËÐÄʶ±ð·½·¨ - aiworkOCRÎÄ×Öʶ±ð
- * @param {number} size - ʶ±ð³ß´ç£¬ÍƼöÖµ£º640¡¢320
- * @param {Array} region - ʶ±ðÇøÓòµÄ°Ù·Ö±È×ø±ê [x1, y1, x2, y2]
- * @returns {object|null} OCRʶ±ð½á¹û¶ÔÏó»ònull
- */
- aiworkOCR: function(size, region) {
- try {
- size = size || 640;
- region = region || [0, 0, 1, 1];
- var img = screen.screenShotFull();
- var result = img.aiworkOCR(size, region);
- img.recycle();
- return result;
- } catch (e) {
- console.log("aiworkOCR error:", e.message);
- return null;
- }
- },
-
- /**
- * ·½·¨20£ººËÐÄʶ±ð·½·¨ - paddleOCRÎÄ×Öʶ±ð
- * @param {number} size - ʶ±ð³ß´ç£¬ÍƼöÖµ£º640¡¢320
- * @param {Array} region - ʶ±ðÇøÓòµÄ°Ù·Ö±È×ø±ê [x1, y1, x2, y2]
- * @returns {object|null} OCRʶ±ð½á¹û¶ÔÏó»ònull
- */
- paddleOCR: function(size, region) {
- try {
- size = size || 640;
- region = region || [0, 0, 1, 1];
- var img = screen.screenShotFull();
- var result = img.paddleOCR(size, region);
- img.recycle();
- return result;
- } catch (e) {
- console.log("paddleOCR error:", e.message);
- return null;
- }
- },
-
- /**
- * ·½·¨21£ºOpenCVÏà¹Ø·½·¨ - ͨ¹ýѵÁ·×Ö¿âʶ±ð
- * @param {object} matImg - ÊäÈëµÄͼƬ¶ÔÏó£¨MatÀàÐÍ£©
- * @param {string} dictPath - ×Ö¿âÎļþ·¾¶/Ãû³Æ
- * @param {number} threshold - ÏàËÆ¶ÈãÐÖµ£¨0~1£©
- * @param {number} expectedCount - Ô¤ÆÚʶ±ðµÄÎÄ×ÖÊýÁ¿
- * @param {Array} region - ʶ±ðÇøÓòµÄ°Ù·Ö±È×ø±ê [x1,y1,x2,y2]
- * @returns {object|null} OCRʶ±ð½á¹û¶ÔÏó»ònull
- */
- opencvOCR: function(matImg, dictPath, threshold, expectedCount, region) {
- try {
- if (typeof opencv !== 'undefined' && typeof opencv.OCR === 'function') {
- dictPath = dictPath || "";
- threshold = threshold || 0;
- expectedCount = expectedCount || 0;
- region = region || [];
- var result = opencv.OCR(matImg, dictPath, threshold, expectedCount, region);
- return result;
- }
- } catch (e) {
- // ºöÂÔ´íÎ󣬷µ»Ønull
- }
- return null;
- },
-
- /**
- * ·½·¨22£ºOpenCVÏà¹Ø·½·¨ - »ùÓÚCVÎļþµÄOCRʶ±ð
- * @param {string} cvFileName - CVÎļþÃû
- * @returns {object|null} OCRʶ±ð½á¹û¶ÔÏó»ònull
- */
- opencvOCREx: function(cvFileName) {
- try {
- if (typeof opencv !== 'undefined' && typeof opencv.OCREx === 'function') {
- cvFileName = cvFileName || "";
- var result = opencv.OCREx(cvFileName);
- return result;
- }
- } catch (e) {
- console.log("opencvOCREx error:", e.message);
- return null;
- }
- return null;
- }
- };
- /**
- * Ä£¿é£ºOCRÒýÇæ¹ÜÀí
- * ¹¦ÄÜ£º¹ÜÀíOCRÒýÇæµÄ¼ÓÔØºÍÐ¶ÔØ
- */
- var OCREngineManager = {
- /**
- * ¼ÓÔØOCRÒýÇæ
- * @param {string} lang - ÓïÑÔÄ£ÐÍ
- * @returns {boolean} ÊÇ·ñ¼ÓÔØ³É¹¦
- */
- loadOCREngine: function(lang) {
- lang = lang || "chi_sim";
- try {
- if (typeof loadOCR === 'function') {
- var result = loadOCR(lang);
- return result;
- } else {
- console.log("loadOCR function not available");
- return false;
- }
- } catch (e) {
- console.log("Error loading OCR engine:", e.message);
- return false;
- }
- },
-
- /**
- * Ð¶ÔØOCRÒýÇæ
- * @returns {boolean} ÊÇ·ñÐ¶ÔØ³É¹¦
- */
- unloadOCREngine: function() {
- try {
- if (typeof unloadOCR === 'function') {
- var result = unloadOCR();
- return result;
- } else {
- console.log("unloadOCR function not available");
- return false;
- }
- } catch (e) {
- console.log("Error unloading OCR engine:", e.message);
- return false;
- }
- },
-
- /**
- * ¼ì²éOCRÒýÇæ×´Ì¬
- * @returns {boolean} OCRÒýÇæÊÇ·ñÒѼÓÔØ
- */
- isOCREngineLoaded: function() {
- try {
- if (typeof isOCRLoaded === 'function') {
- var result = isOCRLoaded();
- return result;
- } else {
- console.log("isOCRLoaded function not available");
- return false;
- }
- } catch (e) {
- console.log("Error checking OCR engine status:", e.message);
- return false;
- }
- }
- };
- /**
- * Ä£¿é£º¹¤¾ßº¯Êý
- * ¹¦ÄÜ£ºÌṩ¸÷ÖÖ¹¤¾ßº¯Êý
- */
- var Utils = {
- /**
- * ÑÓ³ÙÖ´ÐÐ
- * @param {number} ms - ÑÓ³ÙºÁÃëÊý
- */
- sleep: function(ms) {
- java.lang.Thread.sleep(ms);
- },
-
- /**
- * »ñÈ¡É豸ÐÅÏ¢
- * @returns {Object} É豸ÐÅÏ¢
- */
- getDeviceInfo: function() {
- var screenWidth = 0;
- var screenHeight = 0;
-
- try {
- if (typeof screen !== 'undefined' && typeof screen.getScreenWidth === 'function') {
- screenWidth = screen.getScreenWidth();
- screenHeight = screen.getScreenHeight();
- }
- } catch (e) {
- // ºöÂÔ´íÎó£¬Ê¹ÓÃĬÈÏÖµ
- }
-
- return {
- width: screenWidth,
- height: screenHeight,
- model: "δ֪",
- brand: "δ֪",
- sdk: "δ֪",
- androidVersion: "δ֪"
- };
- },
-
- /**
- * ´´½¨Í¼Ïñ¶ÔÏó
- * @param {string} path - ͼÏñ·¾¶
- * @returns {Object} ͼÏñ¶ÔÏó
- */
- createImage: function(path) {
- try {
- var img = new image();
- img.read(path);
- return img;
- } catch (e) {
- console.log("Error creating image object:", e.message);
- return null;
- }
- },
-
- /**
- * ´òÓ¡ÈÕÖ¾
- * @param {string} message - ÈÕÖ¾ÏûÏ¢
- */
- log: function(message) {
- console.log(message);
- },
-
- /**
- * Ëæ»úÊýÉú³É
- * @param {number} min - ×îСֵ
- * @param {number} max - ×î´óÖµ
- * @returns {number} Ëæ»úÊý
- */
- random: function(min, max) {
- return Math.floor(Math.random() * (max - min + 1)) + min;
- }
- };
- /**
- * ×ÛºÏʾÀý£ºÊ¹ÓÃÕÒ×ÖºÍOCR·½·¨
- */
- function runComprehensiveDemo() {
- console.log("=== AIWROK ÕÒ×ÖÓëOCR·½·¨ÖÕ¼«»ã×ÜʾÀý ===");
-
- // 1. ´òÓ¡É豸ÐÅÏ¢
- console.log("\n1. É豸ÐÅÏ¢:");
- var deviceInfo = Utils.getDeviceInfo();
- console.log("É豸ÐͺÅ: " + deviceInfo.model);
- console.log("ÆÁÄ»³ß´ç: " + deviceInfo.width + "x" + deviceInfo.height);
- console.log("Android°æ±¾: " + deviceInfo.androidVersion);
-
- // 2. ³¢ÊÔʹÓûù´¡ÕÒ×Ö·½·¨
- console.log("\n2. »ù´¡ÕÒ×ÖʾÀý:");
- var textResult = FindTextMethods.findTextBasic("È·¶¨", 3000);
- if (textResult) {
- console.log("ÕÒµ½ÎÄ×Ö'È·¶¨'£¬Î»ÖÃ: x=" + textResult.x + ", y=" + textResult.y);
- } else {
- console.log("δÕÒµ½ÎÄ×Ö'È·¶¨'");
- }
-
- // 3. ³¢ÊÔʹÓÃOCRʶ±ð²¢µã»÷
- console.log("\n3. OCRʶ±ðÓëµã»÷ʾÀý:");
- var targetText = "À×µç"; // Òª²éÕÒ²¢µã»÷µÄÎÄ×Ö
- var clicked = false;
-
- // ³¢ÊÔʹÓÃÄÚÖõÄMLKitOcr½øÐÐʶ±ð
- console.log("\n1. ³¢ÊÔʹÓÃMLKitOcr½øÐÐʶ±ð:");
- var mlkitResult = OCRMethods.MLKitOcr('zh');
- if (mlkitResult) {
- console.log("MLKitOcrʶ±ð³É¹¦");
- console.log("MLKitOcrʶ±ð½á¹û:");
- console.log(mlkitResult);
-
- // ³¢ÊÔ²éÕÒ²¢µã»÷Ä¿±êÎÄ×Ö
- try {
- var target = mlkitResult.findIncludeText(targetText);
- if (target) {
- console.log("ʹÓÃMLKitOcrÕÒµ½ÎÄ×Ö'" + targetText + "'£¬ÕýÔÚµã»÷...");
- target.hidClick();
- clicked = true;
- console.log("µã»÷³É¹¦£¡");
- } else {
- console.log("MLKitOcrδÕÒµ½ÎÄ×Ö'" + targetText + "'");
- }
- } catch (e) {
- console.log("MLKitOcrµã»÷ʧ°Ü:", e.message);
- }
- } else {
- console.log("MLKitOcrʶ±ðʧ°Ü");
- }
-
- // ³¢ÊÔʹÓÃMLKitOcrRect½øÐÐʶ±ð
- console.log("\n2. ³¢ÊÔʹÓÃMLKitOcrRect½øÐÐʶ±ð:");
- var mlkitRectResult = OCRMethods.MLKitOcrRect('zh', [0, 0, 1, 1]);
- if (mlkitRectResult) {
- console.log("MLKitOcrRectʶ±ð³É¹¦");
- console.log("MLKitOcrRectʶ±ð½á¹û:");
- console.log(mlkitRectResult);
-
- // ³¢ÊÔ²éÕÒ²¢µã»÷Ä¿±êÎÄ×Ö
- try {
- var target = mlkitRectResult.findIncludeText(targetText);
- if (target) {
- console.log("ʹÓÃMLKitOcrRectÕÒµ½ÎÄ×Ö'" + targetText + "'£¬ÕýÔÚµã»÷...");
- target.hidClick();
- clicked = true;
- console.log("µã»÷³É¹¦£¡");
- } else {
- console.log("MLKitOcrRectδÕÒµ½ÎÄ×Ö'" + targetText + "'");
- }
- } catch (e) {
- console.log("MLKitOcrRectµã»÷ʧ°Ü:", e.message);
- }
- } else {
- console.log("MLKitOcrRectʶ±ðʧ°Ü");
- }
-
- // ³¢ÊÔʹÓÃaiworkOCR½øÐÐʶ±ð
- console.log("\n3. ³¢ÊÔʹÓÃaiworkOCR½øÐÐʶ±ð:");
- var aiworkResult = OCRMethods.aiworkOCR(640, [0, 0, 1, 1]);
- if (aiworkResult) {
- console.log("aiworkOCRʶ±ð³É¹¦");
- console.log("aiworkOCRʶ±ð½á¹û:");
- console.log(aiworkResult);
-
- // ³¢ÊÔ²éÕÒ²¢µã»÷Ä¿±êÎÄ×Ö
- try {
- var target = aiworkResult.findIncludeText(targetText);
- if (target) {
- console.log("ʹÓÃaiworkOCRÕÒµ½ÎÄ×Ö'" + targetText + "'£¬ÕýÔÚµã»÷...");
- target.hidClick();
- clicked = true;
- console.log("µã»÷³É¹¦£¡");
- } else {
- console.log("aiworkOCRδÕÒµ½ÎÄ×Ö'" + targetText + "'");
- }
- } catch (e) {
- console.log("aiworkOCRµã»÷ʧ°Ü:", e.message);
- }
- } else {
- console.log("aiworkOCRʶ±ðʧ°Ü");
- }
-
- // ³¢ÊÔʹÓÃpaddleOCR½øÐÐʶ±ð
- console.log("\n4. ³¢ÊÔʹÓÃpaddleOCR½øÐÐʶ±ð:");
- var paddleResult = OCRMethods.paddleOCR(640, [0, 0, 1, 1]);
- if (paddleResult) {
- console.log("paddleOCRʶ±ð³É¹¦");
- console.log("paddleOCRʶ±ð½á¹û:");
- console.log(paddleResult);
-
- // ³¢ÊÔ²éÕÒ²¢µã»÷Ä¿±êÎÄ×Ö
- try {
- var target = paddleResult.findIncludeText(targetText);
- if (target) {
- console.log("ʹÓÃpaddleOCRÕÒµ½ÎÄ×Ö'" + targetText + "'£¬ÕýÔÚµã»÷...");
- target.hidClick();
- clicked = true;
- console.log("µã»÷³É¹¦£¡");
- } else {
- console.log("paddleOCRδÕÒµ½ÎÄ×Ö'" + targetText + "'");
- }
- } catch (e) {
- console.log("paddleOCRµã»÷ʧ°Ü:", e.message);
- }
- } else {
- console.log("paddleOCRʶ±ðʧ°Ü");
- }
-
- // ³¢ÊÔʹÓÃopencv.OCREx½øÐÐʶ±ð
- console.log("\n5. ³¢ÊÔʹÓÃopencv.OCREx½øÐÐʶ±ð:");
- var opencvResult = OCRMethods.opencvOCREx('ͼɫ473670.cv');
- if (opencvResult) {
- console.log("opencv.OCRExʶ±ð³É¹¦");
- try {
- console.log("opencv.OCRExʶ±ð½á¹û:");
- console.log(opencvResult.getAllString());
-
- // ³¢ÊÔ²éÕÒ²¢µã»÷Ä¿±êÎÄ×Ö
- var target = opencvResult.findIncludeText(targetText);
- if (target) {
- console.log("ʹÓÃopencv.OCRExÕÒµ½ÎÄ×Ö'" + targetText + "'£¬ÕýÔÚµã»÷...");
- target.hidClick();
- clicked = true;
- console.log("µã»÷³É¹¦£¡");
- } else {
- console.log("opencv.OCRExδÕÒµ½ÎÄ×Ö'" + targetText + "'");
- }
- } catch (e) {
- console.log("opencv.OCREx²Ù×÷ʧ°Ü:", e.message);
- }
- } else {
- console.log("opencv.OCRExʶ±ðʧ°Ü");
- }
-
- // ËùÓÐOCR·½·¨¶¼³¢ÊÔÍê±Ï
- if (!clicked) {
- console.log("\nËùÓÐOCR·½·¨¶¼Î´ÕÒµ½ÎÄ×Ö'" + targetText + "'");
- }
-
- // 7. ÅúÁ¿ÕÒ×ÖʾÀý
- console.log("\n4. ÅúÁ¿ÕÒ×ÖʾÀý:");
- var texts = ["È·¶¨", "È¡Ïû", "ÉèÖÃ", "¿ªÊ¼"];
- var batchResult = FindTextMethods.findTextBatch(texts);
- if (batchResult) {
- console.log("ÕÒµ½ÎÄ×Ö: " + batchResult.text + "£¬Î»ÖÃ: x=" + batchResult.position.x + ", y=" + batchResult.position.y);
- } else {
- console.log("δÕÒµ½ÈκÎÎÄ×Ö");
- }
-
- console.log("\n=== ʾÀýÖ´ÐÐÍê³É ===");
- }
- /**
- * ¸ß¼¶Ê¾Àý£º×Ô¶¯»¯ÈÎÎñÁ÷³Ì
- */
- function runAutomationTask() {
- console.log("=== AIWROK ×Ô¶¯»¯ÈÎÎñÁ÷³ÌʾÀý ===");
-
- console.log("✅ ¿ªÊ¼Ö´ÐÐ×Ô¶¯»¯ÈÎÎñ");
-
- // 1. µÈ´ýÓ¦ÓÃÆô¶¯
- console.log("\n1. µÈ´ýÓ¦ÓÃÆô¶¯...");
- // ÕâÀï¿ÉÒÔʹÓûù´¡ÕÒ×Ö·½·¨µÈ´ýÓ¦ÓÃÆô¶¯
- var appStarted = false;
- var startTime = Date.now();
- var timeoutMs = 30 * 1000;
-
- while (Date.now() - startTime < timeoutMs) {
- var result = FindTextMethods.findTextBasic("Ê×Ò³", 1000);
- if (result) {
- appStarted = true;
- break;
- }
- sleep.millisecond(1000);
- }
-
- if (!appStarted) {
- console.log("❌ Ó¦ÓÃÆô¶¯³¬Ê±");
- return;
- }
- console.log("✅ Ó¦ÓÃÆô¶¯³É¹¦");
-
- // 2. µã»÷µÇ¼°´Å¥
- console.log("\n2. µã»÷µÇ¼°´Å¥...");
- var loginResult = FindTextMethods.findTextBasic("怬", 3000);
- if (loginResult) {
- auto.clickPoint(loginResult.x, loginResult.y);
- sleep.millisecond(1000);
- console.log("✅ µÇ¼°´Å¥µã»÷³É¹¦");
- } else {
- console.log("❌ δÕÒµ½µÇ¼°´Å¥");
- return;
- }
-
- // 3. µÈ´ýµÇÂ¼Ò³Ãæ
- console.log("\n3. µÈ´ýµÇÂ¼Ò³Ãæ...");
- var loginPageLoaded = false;
- startTime = Date.now();
- timeoutMs = 10 * 1000;
-
- while (Date.now() - startTime < timeoutMs) {
- var result = FindTextMethods.findTextBasic("Óû§Ãû", 1000);
- if (result) {
- loginPageLoaded = true;
- break;
- }
- sleep.millisecond(1000);
- }
-
- if (!loginPageLoaded) {
- console.log("❌ µÇÂ¼Ò³Ãæ¼ÓÔØ³¬Ê±");
- return;
- }
- console.log("✅ µÇÂ¼Ò³Ãæ¼ÓÔØ³É¹¦");
-
- // 4. Ö´ÐеǼ²Ù×÷£¨ÕâÀïÖ»ÊÇʾÀý£¬Êµ¼ÊÐèÒªÊäÈëÓû§ÃûÃÜÂ룩
- console.log("\n4. Ö´ÐеǼ²Ù×÷...");
- // Ä£ÄâÊäÈëÓû§ÃûÃÜÂë
- // input.setText("username", "test");
- // input.setText("password", "123456");
-
- // 5. µã»÷µÇ¼°´Å¥
- var loginSubmitResult = FindTextMethods.findTextBasic("怬", 3000);
- if (loginSubmitResult) {
- auto.clickPoint(loginSubmitResult.x, loginSubmitResult.y);
- sleep.millisecond(1000);
- console.log("✅ µÇ¼²Ù×÷Ìá½»³É¹¦");
- } else {
- console.log("❌ δÕÒµ½µÇ¼Ìá½»°´Å¥");
- return;
- }
-
- // 6. µÈ´ýµÇ¼³É¹¦
- console.log("\n5. µÈ´ýµÇ¼³É¹¦...");
- var loginSuccess = false;
- startTime = Date.now();
- timeoutMs = 15 * 1000;
-
- while (Date.now() - startTime < timeoutMs) {
- var result = FindTextMethods.findTextBasic("»¶Ó", 1000);
- if (result) {
- loginSuccess = true;
- break;
- }
- sleep.millisecond(1000);
- }
-
- if (!loginSuccess) {
- console.log("❌ µÇ¼ʧ°Ü»ò³¬Ê±");
- return;
- }
- console.log("✅ µÇ¼³É¹¦");
-
- console.log("\n=== ×Ô¶¯»¯ÈÎÎñÖ´ÐÐÍê³É ===");
- }
- // ÔËÐÐʾÀý£¨Èç¹ûÖ±½ÓÖ´ÐдËÎļþ£©
- if (typeof console !== 'undefined' && console.log) {
- // ÑÓ³Ù1ÃëºóÔËÐÐʾÀý£¬ÒÔ±ãÓÐʱ¼ä×¼±¸²âÊÔ»·¾³
- setTimeout(function() {
- runComprehensiveDemo();
- // ¿ÉÒÔÑ¡ÔñÔËÐÐ×Ô¶¯»¯ÈÎÎñʾÀý
- // runAutomationTask();
- }, 1000);
- }
¸´ÖÆ´úÂë
|
|