本脚本支持仅1920*1080 像素分辨率的机子上面正常运行。
-------------------------------------------------------------------------------
我是真的懒,连能量都不想好好收,因此写了脚本来自动帮我收能量.
Auto.js 这款脚本应用我们在应用市场可以很方便搜索到,它在没有root的时候可以通过开启无障碍服务来实现模拟点击滑动,监听等等.使用下面这个脚本,可以实现打开支付宝,进入蚂蚁森林(你得将它添加到主页常用子应用中),滑动,查找有能量的好友,进入收集。
好好享用,那么上代码:
/**
*
* @name 支付宝自动收能量脚本
* @author Oliver
* @description 需要您的设备分辨率为1920*1080;小米8上,截图需要手动允许,需要点击 “立即开始”; 开始运行时请保证支付宝已经处于首页
*/
auto();
main();
/**
* 下面注释代码可以进一步完善,读取到可以收取的时间,做个记录,然后到时间自动进去收集
*
events.observeNotification();
events.onToast(function (toast) {
var text = toast.getText();
var appName = toast.getPackageName();
var subIdx = text.indexOf("后");
if (appName == "com.eg.android.AlipayGphone" && subIdx != -1) {
var sub = text.substring(0, subIdx);
var idxHour = sub.indexOf("小时");
var idxMin = sub.indexOf("分");
var hour = 0;
var min = 0;
if (idxHour == -1) {
var stringMin = sub.substring(0, idxMin);
min = parseInt(stringMin)
} else {
var stringHour = sub.substring(0, idxHour);
var stringMin = sub.substring(idxHour + 2, idxMin);
hour = parseInt(stringHour)
min = parseInt(stringMin)
}
var time = (hour * 60 + min) * 60 * 1000;
if (nextTime > time) {
nextTime = time;
}
log("NextTime=" + hour + ":" + min + " Microseconds=" + nextTime + "ms");
}
});
//setTimeout(function() {
//}, 1000*1);
// main();
var nextTime = 900000000000;
*/
var end = false;
function main() {
toast("程序开始运行!");
launchApp("支付宝");
sleep(3000);
click("蚂蚁森林"); //为了这后面正常运行,将蚂蚁森林放在支付宝首页中
sleep(3000);
collect();
swipe(540, 1910, 540, 100, 500)
swipe(540, 1910, 540, 100, 500)
swipe(540, 1910, 540, 100, 500)
click(672, 954); //查看排行榜
sleep(2000);
swipe(540, 1800, 540, 1800 - 240, 500);
sleep(500);
toast("现在开始收集能量了!");
while (!end) {
execute();
}
}
function execute() {
swipe(540, 1919, 540, 88, 500)
col();
swipe(540, 1734, 540, 1734 - 156, 500)
click(540, 1918);
sleep(2000);
swipe(540, 1857, 540, 155, 500);
sleep(1000);
col();
}
function col() {
if (!requestScreenCapture()) {
toast("没有截图权限,程序退出!");
exit();
end = true;
}
var img = captureScreen();
for (var i = 187; i <= 1816; i = i + 200) {
if (isEnd(img, i)) {
back();
sleep(1000);
back();
sleep(1000);
back();
sleep(1000);
toast("完成任务啦!")
end = true
}
var p = getColor(img, i);
if (p) {
click(1017, p.y + 20);
sleep(3000);
collect();
back();
sleep(1000);
} else {
log(i + " p=null");
}
}
}
function getColor(img, y) {
var p = findColor(img, "#30bf6c", {
region: [1017, y, 63, 100]
});
return p;
}
function isEnd(img, y) {
var p = findColor(img, "#30bf6c", {
region: [860, y, 10, 10]
});
if (p) {
return true;
} else {
return false;
}
}
function collect() {
for (var y = 460; y <= 860; y += 100) {
for (var x = 185; x <= 890; x += 100) {
click(x, y);
}
}
}
Python中的实现,我们使用android的自动化测试库uiautomator来实现,使用opencv来实现对截图的中可搜集小手的识别,目前还不是很完善,提供一个思路,希望有时间的你来实现,其实Auto.js那个真的好用;-)
#! -*- coding=utf-8 -*-
from uiautomator import Device
from uiautomator import Adb
import os
import cv2
import numpy as np
from matplotlib import pyplot as plt
def match():
img = cv2.imread("1.png",0)
img2 = img.copy()
template = cv2.imread("match.png",0)
w,h = template.shape[::-1]
# method = eval('cv2.TM_CCOEFF')
method = eval('cv2.TM_CCOEFF_NORMED')
res = cv2.matchTemplate(img2,template,method)
threshold = 0.5
loc = np.where( res >= threshold)
arr = []
for pt in zip(*loc[::-1]):
cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
d = (pt,(pt[0] + w, pt[1] + h),)
arr.append(d)
cv2.imwrite('res.png',img)
return arr
if __name__ == "__main__":
d = Device("7cba0eb")
# d.screen.on()
# a = Adb()
# os.system("adb shell am start -n com.eg.android.AlipayGphone/.AlipayLogin")
# # a.cmd("shell am start -n com.eg.android.AlipayGphone/.AlipayLogin")
# d(text="蚂蚁森林").click()
# # d(text="种树").click(
# print d.info
# d.wait.idle()
# d.wait.update()
# d.screenshot("1.png")
# d(scrollable=True).fling()
# web = d(className="com.uc.webview.export.WebView")
# web = d(className="com.uc.webkit.WebView")
# web.scroll.toEnd()
# web.swipe.down()
# web.click(800,940)
# d.wait.update()
# web = d(className="com.uc.webview.export.WebView")
d.screenshot("1.png")
loc= match()
print loc
# print (tl[0]+br[0])/2,(tl[1]+br[1])/2
# d.click((tl[0]+br[0])/2,(tl[1]+br[1])/2)
# d.wait.update()
# for y in range(400,870,100):
# for x in range(50,1080,100):
# d.click(x,y)