我是真的懶,連能量都不想好好收,因此寫了腳本來自動幫我收能量.
Auto.js 這款腳本應用我們在應用市場可以很方便搜索到,它在沒有root的時候可以通過開啓無障礙服務來實現模擬點擊滑動,監聽等等.使用下面這個腳本,可以實現打開支付寶,進入螞蟻森林(你得將它添加到主頁常用子應用中),滑動,查找有能量的好友,進入蒐集
.After that,quit!
不廢話了直接上代碼:
auto();
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();s
var end = false;
var nextTime = 900000000000;
main();
function main() {
// home();
// sleep(5000)
// swipe(540,1600,540,1000,350);
// startToastListener();
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) {
exec();
}
}
function exec() {
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() {
// toast("Begin col")
if (!requestScreenCapture()) {
toast("沒有截圖權限");
exit();
end = true;
}
var img = captureScreen();
// var p = findColor(img,"#30bf6c"); //
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 = locColor(img, i);
if (p) {
log(i + " p.x=" + p.x + " p.y=" + p.y);
click(1017, p.y + 20);
sleep(3000);
collect();
back();
sleep(1000);
} else {
log(i + " p=null");
}
}
}
function locColor(img, y) {
var p = findColor(img, "#30bf6c", {
region: [1017, y, 73, 100]
});
return p;
}
function isEnd(img, y) {
var p = findColor(img, "#30bf6c", {
region: [860, y, 10, 10]
});
// toastLog("Got "+(img==null)+" "+y+" "+(p==null))
if (p) {
// toastLog("Got "+(img==null)+" "+y)
return true;
} else {
//toastLog("Got "+(img==null)+" "+y)
return false;
}
}
function nextPage() {
swipe(540, 1910, 540, 59, 5000)
}
function all(startHeight) {
var eachHeight = 180;
for (; startHeight < 1920; startHeight = eachHeight + startHeight) {
toast(startHeight);
click(540, startHeight);
sleep(3000);
collect();
back();
sleep(1000);
}
}
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)