|
Æ»¹ûϵͳÀïÈçºÎʹÓÃOCR½øÐÐÎı¾Ê¶±ð²¢Ö´Ðеã»÷²Ù×÷
- /**
- * Æ»¹ûOCRµã»÷ʾÀý
- * Õâ¸öʾÀýչʾÁËÈçºÎʹÓÃOCR½øÐÐÎı¾Ê¶±ð²¢Ö´Ðеã»÷²Ù×÷
- * 🍎½»Á÷QQȺ711841924Ⱥһ£¬Æ»¹ûÄÚ²âȺ£¬528816639
- */
- /**
- * ʾÀý1: »ù±¾OCRʶ±ðÓ뾫ȷÎı¾µã»÷
- * ʹÓÃfindText·½·¨²éÕÒ¾«È·Îı¾²¢µã»÷
- */
- function ocrClickExample() {
- printl("==== ʾÀý1: »ù±¾OCRʶ±ðÓ뾫ȷÎı¾µã»÷ ====");
-
- try {
- // Ö´ÐÐÈ«ÆÁÄ»OCRʶ±ð
- var ocrResult = screen.ocr(0, 0, 1, 1);
-
- // ²éÕÒ¾«È·Îı¾"È·¶¨"
- var target = ocrResult.findText("È·¶¨", [0, 0, 1, 1]);
-
- if (target != null) {
- printl("ÕÒµ½Îı¾ 'È·¶¨'£¬×¼±¸µã»÷");
-
- // »ñÈ¡Îı¾µÄÖÐÐĵã×ø±ê
- var x = target.x + target.width / 2;
- var y = target.y + target.height / 2;
-
- printl("µã»÷×ø±ê: x=" + x + ", y=" + y);
-
- // Ö´Ðеã»÷²Ù×÷
- hid.click(x * device.width, y * device.height);
- printl("µã»÷³É¹¦");
- } else {
- printl("δÕÒµ½Îı¾ 'È·¶¨'");
- }
- } catch (e) {
- printl("OCR²Ù×÷·¢Éú´íÎó: " + e);
- }
- }
- /**
- * ʾÀý2: ʹÓÃÄÚÖõÄclickRandom·½·¨½øÐÐËæ»úλÖõã»÷
- * ÕâÊǸü¼òµ¥µÄµã»÷·½Ê½
- */
- function ocrClickRandomExample() {
- printl("\n==== ʾÀý2: ʹÓÃclickRandom·½·¨½øÐÐËæ»úλÖõã»÷ ====");
-
- try {
- // Ö´ÐÐOCRʶ±ð
- var ocrResult = screen.ocr(0, 0, 1, 1);
-
- // ²éÕÒ°üº¬"ÉèÖÃ"µÄÎı¾
- var target = ocrResult.findIncludeText("ÉèÖÃ", [0, 0, 1, 1]);
-
- if (target != null) {
- printl("ÕÒµ½°üº¬ 'ÉèÖÃ' µÄÎı¾£¬×¼±¸ÔÚÆäÇøÓòÄÚËæ»úµã»÷");
-
- // ʹÓÃÄÚÖõÄclickRandom·½·¨ÔÚÎı¾ÇøÓòÄÚËæ»úλÖõã»÷
- target.clickRandom();
- printl("Ëæ»úλÖõã»÷³É¹¦");
- } else {
- printl("δÕÒµ½°üº¬ 'ÉèÖÃ' µÄÎı¾");
- }
- } catch (e) {
- printl("OCR²Ù×÷·¢Éú´íÎó: " + e);
- }
- }
- /**
- * ʾÀý3: ʹÓÃÕýÔò±í´ïʽ²éÕÒ¶à¸öÆ¥ÅäÏî²¢µã»÷µÚÒ»¸ö
- */
- function ocrRegexClickExample() {
- printl("\n==== ʾÀý3: ʹÓÃÕýÔò±í´ïʽ²éÕÒ¶à¸öÆ¥ÅäÏî²¢µã»÷µÚÒ»¸ö ====");
-
- try {
- // Ö´ÐÐOCRʶ±ð
- var ocrResult = screen.ocr(0, 0, 1, 1);
-
- // ʹÓÃÕýÔò±í´ïʽ²éÕÒ°üº¬Êý×ÖµÄÎı¾
- var targets = ocrResult.findTextWithRegex("\\d+", [0, 0, 1, 1]);
-
- if (targets != null && targets.length > 0) {
- printl("ÕÒµ½ " + targets.length + " ¸ö°üº¬Êý×ÖµÄÎı¾");
- printl("µÚÒ»¸öÆ¥ÅäµÄÎı¾ÊÇ: " + targets[0].text);
-
- // µã»÷µÚÒ»¸öÆ¥ÅäÏî
- targets[0].clickRandom();
- printl("µã»÷µÚÒ»¸öÆ¥ÅäÏî³É¹¦");
- } else {
- printl("δÕÒµ½°üº¬Êý×ÖµÄÎı¾");
- }
- } catch (e) {
- printl("OCR²Ù×÷·¢Éú´íÎó: " + e);
- }
- }
- /**
- * ʾÀý4: OCRʶ±ðºó±éÀúËùÓÐÎı¾²¢µã»÷ÌØ¶¨Ìõ¼þµÄÎı¾
- */
- function ocrIterateAndClickExample() {
- printl("\n==== ʾÀý4: OCRʶ±ðºó±éÀúËùÓÐÎı¾²¢µã»÷ÌØ¶¨Ìõ¼þµÄÎı¾ ====");
-
- try {
- // Ö´ÐÐOCRʶ±ð
- var ocrResult = screen.ocr(0, 0, 1, 1);
-
- // »ñÈ¡ËùÓÐʶ±ðµ½µÄÎı¾
- var allDetects = ocrResult.getAllDetect();
-
- printl("×ܹ²Ê¶±ðµ½ " + allDetects.length + " ¸öÎı¾ÔªËØ");
-
- // ±éÀúËùÓÐÎı¾
- for (var i = 0; i < allDetects.length; i++) {
- var detect = allDetects[i];
- printl("Îı¾ " + i + ": " + detect.text + ", λÖÃ: [" + detect.x + ", " + detect.y + ", " +
- detect.width + ", " + detect.height + "]");
-
- // Èç¹ûÎı¾°üº¬"È¡Ïû"£¬µã»÷Ëü
- if (detect.text.includes("È¡Ïû")) {
- printl("ÕÒµ½°üº¬ 'È¡Ïû' µÄÎı¾£¬×¼±¸µã»÷");
- detect.clickRandom();
- printl("µã»÷³É¹¦");
- break; // µã»÷ºóÍ˳öÑ»·
- }
- }
- } catch (e) {
- printl("OCR²Ù×÷·¢Éú´íÎó: " + e);
- }
- }
- /**
- * ʾÀý5: ÇøÓòOCRʶ±ð²¢µã»÷
- * Ö»ÔÚÆÁÄ»µÄij¸öÌØ¶¨ÇøÓò½øÐÐOCRʶ±ð
- */
- function regionOcrClickExample() {
- printl("\n==== ʾÀý5: ÇøÓòOCRʶ±ð²¢µã»÷ ====");
-
- try {
- // ¶¨ÒåÒ»¸öÇøÓò: ÆÁÄ»µÄÖм䲿·Ö (¿í¶È: 60%, ¸ß¶È: 40%, ¾ÓÖÐ)
- var region = [0.2, 0.3, 0.8, 0.7];
-
- printl("ÔÚÇøÓò [" + region[0] + ", " + region[1] + ", " + region[2] + ", " + region[3] + "] ÄÚ½øÐÐOCRʶ±ð");
-
- // ÔÚÖ¸¶¨ÇøÓòÖ´ÐÐOCRʶ±ð
- var ocrResult = screen.ocr(region[0], region[1], region[2], region[3]);
-
- // ²éÕÒÎı¾
- var target = ocrResult.findText("ÏÂÒ»²½", region);
-
- if (target != null) {
- printl("ÔÚÖ¸¶¨ÇøÓòÕÒµ½Îı¾ 'ÏÂÒ»²½'£¬×¼±¸µã»÷");
- target.clickRandom();
- printl("µã»÷³É¹¦");
- } else {
- printl("ÔÚÖ¸¶¨ÇøÓòδÕÒµ½Îı¾ 'ÏÂÒ»²½'");
- }
- } catch (e) {
- printl("OCR²Ù×÷·¢Éú´íÎó: " + e);
- }
- }
- /**
- * ʾÀý6: ×ÛºÏʾÀý - Á¬ÐøOCRʶ±ðºÍµã»÷²Ù×÷
- * Ä£ÄâÒ»¸ö¼òµ¥µÄ×Ô¶¯»¯Á÷³Ì
- */
- function comprehensiveOcrExample() {
- printl("\n==== ʾÀý6: ×ÛºÏʾÀý - Á¬ÐøOCRʶ±ðºÍµã»÷²Ù×÷ ====");
-
- try {
- // ²½Öè1: ²éÕÒ²¢µã»÷"ÉèÖÃ"
- printl("²½Öè1: ²éÕÒ²¢µã»÷'ÉèÖÃ'");
- var ocrResult1 = screen.ocr(0, 0, 1, 1);
- var settingTarget = ocrResult1.findIncludeText("ÉèÖÃ", [0, 0, 1, 1]);
-
- if (settingTarget != null) {
- settingTarget.clickRandom();
- printl("µã»÷ÉèÖóɹ¦");
- sleep(2000); // µÈ´ý2Ã룬ÈÃÒ³Ãæ¼ÓÔØ
-
- // ²½Öè2: ÔÚÐÂÒ³Ãæ²éÕÒ²¢µã»÷"ͨÓÃ"
- printl("²½Öè2: ²éÕÒ²¢µã»÷'ͨÓÃ'");
- var ocrResult2 = screen.ocr(0, 0, 1, 1);
- var generalTarget = ocrResult2.findText("ͨÓÃ", [0, 0, 1, 1]);
-
- if (generalTarget != null) {
- generalTarget.clickRandom();
- printl("µã»÷ͨÓóɹ¦");
- } else {
- printl("δÕÒµ½Îı¾ 'ͨÓÃ'");
- }
- } else {
- printl("δÕÒµ½°üº¬ 'ÉèÖÃ' µÄÎı¾");
- }
- } catch (e) {
- printl("×ÛºÏʾÀý·¢Éú´íÎó: " + e);
- }
- }
- /**
- * ʾÀý7: ʹÓÃOCRËø¶¨Ìá¸ßЧÂÊ
- */
- function lockedOcrExample() {
- printl("\n==== ʾÀý7: ʹÓÃOCRËø¶¨Ìá¸ßЧÂÊ ====");
-
- try {
- // Ëø¶¨OCRʶ±ð½á¹û
- OCR.lockOCR();
- printl("OCR½á¹ûÒÑËø¶¨");
-
- // Ö´ÐнØÍ¼
- var image = screen.screenShotFull();
-
- // ÔÚËø¶¨×´Ì¬Ï½øÐжà´Îʶ±ð²Ù×÷
- printl("Ö´ÐеÚÒ»´ÎOCRʶ±ð");
- var result1 = image.ocr(0, 0, 1, 1);
- var text1 = result1.getAllString();
- printl("µÚÒ»´Îʶ±ðµ½µÄÎı¾ÊýÁ¿: " + text1.length);
-
- printl("Ö´Ðеڶþ´ÎOCRʶ±ð");
- var result2 = image.ocr(0, 0, 1, 1);
- var settingTarget = result2.findIncludeText("ÉèÖÃ", [0, 0, 1, 1]);
-
- if (settingTarget != null) {
- printl("ÕÒµ½°üº¬ 'ÉèÖÃ' µÄÎı¾");
- }
-
- // ¹Ø±ÕͼƬ
- image.close();
-
- // ÊÍ·ÅOCRËø
- OCR.unLock();
- printl("OCR½á¹ûÒѽâËø");
- } catch (e) {
- printl("Ëø¶¨OCRʾÀý·¢Éú´íÎó: " + e);
- // È·±£ÔÚ´íÎóÇé¿öÏÂÒ²ÄÜÊÍ·ÅËø
- try {
- OCR.unLock();
- } catch (unlockError) {
- printl("ÊÍ·ÅËøÊ±·¢Éú´íÎó: " + unlockError);
- }
- }
- }
- /**
- * Ö÷º¯Êý - ÔËÐÐËùÓÐʾÀý
- */
- function main() {
- printl("Æ»¹ûOCRµã»÷ʾÀý¼¯ºÏ\n");
-
- // ÔËÐи÷¸öʾÀý
- ocrClickExample();
- ocrClickRandomExample();
- ocrRegexClickExample();
- ocrIterateAndClickExample();
- regionOcrClickExample();
- comprehensiveOcrExample();
- lockedOcrExample();
-
- printl("\nËùÓÐʾÀýÖ´ÐÐÍê±Ï£¡");
- }
- // µ÷ÓÃÖ÷º¯ÊýÔËÐÐʾÀý
- main();
- /**
- * ʹÓÃ˵Ã÷£º
- * 1. Õâ¸öʾÀýÎļþ°üº¬ÁË7ÖÖ²»Í¬µÄOCRʶ±ðºÍµã»÷·½Ê½
- * 2. Äã¿ÉÒÔ¸ù¾Ýʵ¼ÊÐèÇóÑ¡ÔñÊʺϵķ½·¨
- * 3. ÔÚʵ¼ÊʹÓÃʱ£¬¿ÉÄÜÐèÒª¸ù¾Ý¾ßÌåÓ¦ÓõĽçÃæµ÷Õû´úÂë
- * 4. ¶ÔÓÚÁ¬Ðø²Ù×÷£¬Êʵ±Ìí¼ÓsleepµÈ´ýʱ¼äÒÔÈ·±£²Ù×÷µÄÎȶ¨ÐÔ
- * 5. ´íÎó´¦ÀíÊDZØÒªµÄ£¬¿ÉÒÔ¸ù¾ÝÐèÒªÀ©Õ¹Òì³£´¦ÀíÂß¼
- */
¸´ÖÆ´úÂë
|
|