小米手机
# -*- coding: utf-8 -*- """ 手机屏幕截图的代码: screenshot.py """ import subprocess import os import sys,re from PIL import Image import os import time,random import cv2 import aircv as ac import numpy as np # SCREENSHOT_WAY 是截图方法,经过 check_screenshot 后,会自动递减,不需手动修改 SCREENSHOT_WAY = 1 def run_cmd( cmd_str='', echo_print=1): """ 执行cmd命令,不显示执行过程中弹出的黑框 备注:subprocess.run()函数会将本来打印到cmd上的内容打印到python执行界面上,所以避免了出现cmd弹出框的问题 :param cmd_str: 执行的cmd命令 :return: """ from subprocess import run if echo_print == 1: print('\n执行cmd指令="{}"'.format(cmd_str)) run(cmd_str, shell=True) def run_cmd_Popen_fileno(cmd_string): """ 执行cmd命令,并得到执行后的返回值,python调试界面输出返回值 :param cmd_string: cmd命令,如:'adb devices' :return: """ import subprocess print('运行cmd指令:{}'.format(cmd_string)) return subprocess.Popen(cmd_string, shell=True, stdout=None, stderr=None).wait() def run_cmd_Popen_PIPE(cmd_string): """ 执行cmd命令,并得到执行后的返回值,python调试界面不输出返回值 :param cmd_string: cmd命令,如:'adb devices"' :return: """ import subprocess print('运行cmd指令:{}'.format(cmd_string)) return subprocess.Popen(cmd_string, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='gbk').communicate()[0] def pull_screenshot(xh,sjh,xx): """ 获取屏幕截图,目前有 0 1 2 3 四种方法,未来添加新的平台监测方法时, 可根据效率及适用性由高到低排序 """ global SCREENSHOT_WAY SCREENSHOT_WAY=xh if 1 <= SCREENSHOT_WAY <= 3: process = subprocess.Popen( 'adb -s {0} shell screencap -p'.format(sjh), shell=True, stdout=subprocess.PIPE) binary_screenshot = process.stdout.read() if SCREENSHOT_WAY == 2: binary_screenshot = binary_screenshot.replace(b'\r\n', b'\n') elif SCREENSHOT_WAY == 1: binary_screenshot = binary_screenshot.replace(b'\r\r\n', b'\n') au='tmall'+str(xx)+'.png' with open(au, 'wb') as fa: fa.write(binary_screenshot) elif SCREENSHOT_WAY == 0: os.system('adb -s {0} shell screencap -p /sdcard/tmall.png'.format(sjh)) os.system('adb -s {0} pull /sdcard/tmall.png .'.format(sjh)) def check_screenshot(xh,sjh): """ 检查获取截图的方式 """ global SCREENSHOT_WAY if os.path.isfile('tmall.png'): try: os.remove('tmall.png') except Exception: pass if SCREENSHOT_WAY < 0: print('暂不支持当前设备') sys.exit() #xh=1 pull_screenshot(xh,sjh) try: Image.open('./tmall.png').load() print('采用方式 {} 获取截图'.format(SCREENSHOT_WAY)) except Exception: SCREENSHOT_WAY -= 1 check_screenshot() def huadong(sjh): i = 0 #每次操作的间隔时间取决于手机配置,配置越高时间越短 sleep_time =int(random.uniform(10,15)) aav=sleep_time while 1: #用popen设置shell=True不会弹出cmd框 sleep_time =int(random.uniform(10,33)) sleep_time1 =int(random.uniform(10,30)) aaa=250+sleep_time aab=700+sleep_time aav=300-sleep_time aaa1=250+sleep_time1 aab1=700+sleep_time1 aav1=300-sleep_time1 aak='adb -s {0} shell input swipe {1} {2} {3} {4}'.format(sjh,aav,aab,aab,aaa) print(aak) process = subprocess.Popen(aak,shell=True) time.sleep(int(random.uniform(1,2))) #print(aak) #aak='adb -s {0} shell input swipe {1} {2} {3} {4}'.format(sjh,aaa1,aab1,aaa1,aav1) #aak='adb -s c748555a7d23 shell input swipe {0} {1} {2} {3}'.format(aaa1,aab1,aaa1,aav1) #process = subprocess.Popen(aak,shell=True) #print(aak) #print(aav) #time.sleep(sleep_time) i+=1 #print(str(i) + 'clicks have been completed') break def opqrxza(sjh,xh): aak8='adb -s {0} shell input keyevent 4'.format(sjh)#返回 aa2="adb -s {0} shell am start -n com.kuaishou.nebula/com.yxcorp.gifshow.HomeActivity".format(sjh) if sjjtjc(1,sjh,"./opxz.png",xh): process = subprocess.Popen(aak8,shell=True) time.sleep(3) process = subprocess.Popen(aak8,shell=True) print("oppo确认下载》》》》》》》》》》》》》》") else: print("oppo没有确认下载》》》》》》》》》》》》》") #pass def xmqrxzaa(sjh,xh): aak8='adb -s {0} shell input keyevent 4'.format(sjh)#返回 aa2="adb -s {0} shell am start -n com.kuaishou.nebula/com.yxcorp.gifshow.HomeActivity".format(sjh) if sjjtjcwza(2,sjh,"./xmqrxz.png",0,xh): process = subprocess.Popen(aak8,shell=True) time.sleep(3) process = subprocess.Popen(aak8,shell=True) print("XIAOMI确认下载》》》》》》》》》》》》》》") else: print("XIAOMI没有确认下载》》》》》》》》》》》》》") pass def vivoqrxza(sjh,xh): aak8='adb -s {0} shell input keyevent 4'.format(sjh)#返回 aa2="adb -s {0} shell am start -n com.kuaishou.nebula/com.yxcorp.gifshow.HomeActivity".format(sjh) if sjjtjc(2,sjh,"./voqrxz.png",xh): process = subprocess.Popen(aak8,shell=True) time.sleep(3) process = subprocess.Popen(aak8,shell=True) print("vivo确认下载》》》》》》》》》》》》》》") else: print("vivo没有确认下载》》》》》》》》》》》》》") #pass def jiance(sjh): #sjh=sjh.replace(":5555","") aakk="adb -s {0} shell dumpsys activity activities".format(sjh) aak="adb -s {0} shell am start -n com.kuaishou.nebula/com.yxcorp.gifshow.HomeActivity".format(sjh) zh=[] lx=[] ap=0 p = subprocess.Popen(aakk, shell=True, stdout=subprocess.PIPE) out, err = p.communicate() time.sleep(4) #print(out.splitlines()[24:27]) for line in out.splitlines(): aa=line.decode("utf-8") #print(len(aa)) try: ab=re.findall('cmp=com.(.*?)/',aa) #al=ab[0]+'.HomeActivity' if len(ab)>0: break except: continue try: aa=ab[0] except: ap=1 if ap==0: if aa=="kuaishou.nebula": print("已经运行") pass else: process = subprocess.Popen(aak,shell=True) time.sleep(2) print("已经重新打开了") else: print("没有得到ab") def wmhd(xh,sjh,aa,mubiao,xx): bzz0=0 bzz1=0 bzz2=0 xxa=0 yya=0 xxb=0 yyb=0 #aa1=qutua.wmjc(2,sjh,"./xm1.png",14) aa2="" bbzz=0 while 1: jiance(sjh) if aa=="oppo": opqrxza(sjh,xx) if aa=="xiaomi": xmqrxzaa(sjh,xx) if aa=="vivo": vivoqrxza(sjh,xx) if bzz1==1: aa1=wmjc(xh,sjh,mubiao,xx,xxb,yyb) bzz1=0 print("0000aaaaaaaaaaaaaaaaaaa") else: aa1=wmjc(xh,sjh,mubiao,xx) if aa1[0]==0: huadong(sjh) bzz0=1 xxa=aa1[1] yya=aa1[2] print("aa1",aa1) time.sleep(3) if bzz0==1: aa2=wmjc(xh,sjh,mubiao,xx,xxa,yya) bzz0=0 print("00000bbbbbbbbbbbbbbbbbbbbbbbb") else: aa2=wmjc(xh,sjh,mubiao,xx) if aa2[0]==0: bzz1=1 xxb=aa2[1] yyb=aa2[2] huadong(sjh) #continue if bbzz/10==int(bbzz/10): huadong(sjh) print("aa2",aa2) while abs(aa1[0]-aa2[0])<=4: huadong(sjh) #aa2=aa1 time.sleep(3) break bbzz=bbzz+1 def wmhda(xh,sjh,aa,mubiao,xx): bzz0=0 bzz1=0 bzz2=0 xxa=0 yya=0 xxb=0 yyb=0 #aa1=qutua.wmjc(2,sjh,"./xm1.png",14) aa2="" bbzz=0 while 1: jiance(sjh) if aa=="oppo": opqrxza(sjh,xx) if aa=="xiaomi": xmqrxzaa(sjh,xx) if aa=="vivo": vivoqrxza(sjh,xx) if bzz1==1: aa1=wmjc(xh,sjh,mubiao,xx,xxb,yyb) bzz1=0 print("0000aaaaaaaaaaaaaaaaaaa") else: aa1=wmjc(xh,sjh,mubiao,xx) if aa1[0]==0: huadong(sjh) bzz0=1 xxa=aa1[1] yya=aa1[2] print("aa1",aa1) time.sleep(3) if bzz0==1: aa2=wmjc(xh,sjh,mubiao,xx,xxa,yya) bzz0=0 print("00000bbbbbbbbbbbbbbbbbbbbbbbb") else: aa2=wmjc(xh,sjh,mubiao,xx) if aa2[0]==0: bzz1=1 xxb=aa2[1] yyb=aa2[2] huadong(sjh) #continue if bbzz/10==int(bbzz/10): huadong(sjh) print("aa2",aa2) while abs(aa1[0]-aa2[0])<=4: huadong(sjh) #aa2=aa1 time.sleep(3) break bbzz=bbzz+1 print(bbzz) if bbzz>4: break def find_button(target, template): """ 寻找target图片在template中的位置,返回应该点击的坐标。 """ theight,twidth = target.shape[:2] #print(theight,twidth) # 执行模板匹配,采用的匹配方式cv2.TM_SQDIFF_NORMED result = cv2.matchTemplate(target, template, cv2.TM_SQDIFF_NORMED) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result) # 如果匹配度小于99%,就认为没有找到。 if min_val > 0.01: return None strmin_val = str(min_val) #print(strmin_val) # 绘制矩形边框,将匹配区域标注出来 # cv2.rectangle(template, min_loc, (min_loc[0] + twidth, min_loc[1] + theight), (0, 0, 225), 2) # cv2.imshow("MatchResult----MatchingValue="+strmin_val, template) # cv2.imwrite('1.png', template, [int(cv2.IMWRITE_PNG_COMPRESSION), 9]) # cv2.waitKey() # cv2.destroyAllWindows() x = min_loc[0] + twidth//3 y = min_loc[1] + theight//3 return (x, y) def fun1(): os.system("adb nodaemon server") # 如果adb服务器不能正常启动,可以开另一个线程调用该函数。 def jiancha(yuantu,beitu): #检查yuantu中是否有beitu, #qutu.pull_screenshot(sjh) # 截图 temp = cv2.imread(yuantu) pos = find_button(beitu,temp) if pos: return True else: return False def sjjtjc(xh,sjh,beitu,xha): #手机截图检查是否有指定对象,有则返回True,否则返回False #xh=1 pos=False try: pull_screenshot(xh,sjh,xha) # 截图 au='./tmall'+str(xha)+'.png' temp = cv2.imread(au) target = cv2.imread(beitu) pos = find_button(target,temp) except: pass if pos: return True else: return False def wmjc(xha,sjh,mubiao,xxa,xx=0,yy=0): pull_screenshot(xha,sjh,xxa) # 截图 temp='./tmall'+str(xxa)+'.png' imsrc = ac.imread(temp) # 原始图像 imsch = ac.imread(mubiao) # 带查找的部分 aa=ac.find_template(imsrc, imsch)# #aa={'result': (90.0, 429.0), 'rectangle': ((73, 413), (73, 445), (107, 413), (107, 445)), 'confidence': 0.9999932646751404} #result为找到目标的中心坐标, #rectangle为找到目标的左上,左下,右上,右下坐标 #confidence:相似度 if (aa!=None): xx=aa['result'][0] yy=aa['result'][1] print(xx-70,yy-69,xx+80,yy+79) img=Image.open(temp) img1=img.crop((xx-70,yy-69,xx+80,yy+79)) #im=np.array(Image.open(img).convert('RGB')) im=np.array(img1.convert('RGB')) sought = [255,80,0]#红色颜色的特征 result = np.count_nonzero(np.all(im==sought,axis=2))#统计图形中红色的比例 print(result) #plt.imshow(img1) #plt.show()#前后二行结合显示指定图片 return result,xx,yy else: print(xx-70,yy-69,xx+80,yy+79) img=Image.open(temp) img1=img.crop((xx-70,yy-69,xx+80,yy+79)) #im=np.array(Image.open(img).convert('RGB')) im=np.array(img1.convert('RGB')) sought = [255,80,0]#红色颜色的特征 result = np.count_nonzero(np.all(im==sought,axis=2))#统计图形中红色的比例 print(result) #plt.imshow(img1) #plt.show()#前后二行结合显示指定图片 return result,xx,yy def sjjtjcwza(xh,sjh,beitu,jl,xx): #手机利用已经存在的图查找目标并点击 pull_screenshot(xh,sjh,xx) # 截图 au='./tmall'+str(xx)+'.png' temp = cv2.imread(au) target = cv2.imread(beitu) pos = find_button(target,temp) if pos: print(pos[0],pos[1]) ak="adb -s adudd shell input tap {:d} {:d}".format(pos[0]+jl,pos[1]) ak=ak.replace("adudd",sjh) print(ak) #time.sleep(2) process = subprocess.Popen(ak,shell=True) time.sleep(2) print("点了呀") return True else: return False def sjjtjcwz(sjh,beitu,jl,xx): #手机利用已经存在的图查找目标并点击 pull_screenshot(2,sjh,xx) # 截图 au='./tmall'+str(xx)+'.png' temp = cv2.imread(au) target = cv2.imread(beitu) pos = find_button(target,temp) if pos: print(pos[0],pos[1]) ak="adb -s adudd shell input tap {:d} {:d}".format(pos[0]+jl,pos[1]) ak=ak.replace("adudd",sjh) print(ak) #time.sleep(2) process = subprocess.Popen(ak,shell=True) time.sleep(2) print("点了呀") return True else: return False def sjjtjcwzxm(sjh,beitua,jl,xx): #手机利用已经存在的图查找目标并点击 pull_screenshot(1,sjh,xx) # 截图 au='./tmall'+str(xx)+'.png' temp = cv2.imread(au) bz=0 for beitu in beitua: target = cv2.imread(beitu) pos = find_button(target,temp) if pos: print(pos[0],pos[1]) ak="adb -s adudd shell input tap {:d} {:d}".format(pos[0]+jl,pos[1]) ak=ak.replace("adudd",sjh) #print(ak) #time.sleep(2) process = subprocess.Popen(ak,shell=True) time.sleep(2) print("点了呀") bz=1 else: pass if bz==1: return True else: return False def sjjtjcwzopa(sjh,beitu,jl,xx): #手机利用已经存在的图查找目标并点击 pull_screenshot(2,sjh,xx) # 截图 au='./tmall'+str(xx)+'.png' temp = cv2.imread(au) bz=0 target = cv2.imread(beitu) pos = find_button(target,temp) if pos: print(pos[0],pos[1]) ak="adb -s adudd shell input tap {:d} {:d}".format(pos[0]+jl,pos[1]) ak=ak.replace("adudd",sjh) #print(ak) #time.sleep(2) #process = subprocess.Popen(ak,shell=True) run_cmd(ak) time.sleep(2) print("点了呀") return True else: return False def sjjtjcwzop(sjh,beitua,jl,xx): #手机利用已经存在的图查找目标并点击 pull_screenshot(1,sjh,xx) # 截图 au='./tmall'+str(xx)+'.png' temp = cv2.imread(au) bz=0 for beitu in beitua: target = cv2.imread(beitu) pos = find_button(target,temp) if pos: print(pos[0],pos[1]) ak="adb -s adudd shell input tap {:d} {:d}".format(pos[0]+jl,pos[1]) ak=ak.replace("adudd",sjh) #print(ak) #time.sleep(2) process = subprocess.Popen(ak,shell=True) time.sleep(2) print("点了呀") time.sleep(2) if sjjtjcwzopa(sjh,"./opspzg.png",0,35): time.sleep(30) #sjjtjcwzopa(sjh,"./opspzg.png",0,35) bz=1 else: pass if bz==1: return True else: return False def sjjtjcwzdj(xha,sjh,beitu,jl,xx): #手机截图检查是否有指定对象,有则点击其坐标位置,否则返回False;xha取图方式,sjh手机序列号,beitu查找的对象,jl查找到的位置与实际位置间的水平距离 #xh=2 pull_screenshot(xha,sjh,xx) # 截图 time.sleep(1) au='./tmall'+str(xx)+'.png' temp = cv2.imread(au) pos=None try: target = cv2.imread(beitu) #print(target) except: pass try: pos = find_button(target,temp) except: pass if pos: print(pos[0],pos[1]) ak="adb -s adudd shell input tap {:d} {:d}".format(pos[0]+jl,pos[1]) ak=ak.replace("adudd",sjh) print(ak) #time.sleep(2) process = subprocess.Popen(ak,shell=True) time.sleep(2) print("点了呀") return True else: return False def xmjiancezong(sjh): #继续观看 确认下载 xmlb=["./xmcs.png","./xmjxck.png","./xmqrxz.png","./xmjx.png","./xmjxgksp.png"] aak8='adb -s {0} shell input keyevent 4'.format(sjh)#返回 pull_screenshot(2,sjh,119) au='./tmall119.png' temp = cv2.imread(au) for i in xmlb: target = cv2.imread(i) pos = find_button(target,temp) if pos: ak="adb -s adudd shell input tap {:d} {:d}".format(pos[0],pos[1]) ak=ak.replace("adudd",sjh) print(ak) #time.sleep(2) process = subprocess.Popen(ak,shell=True) time.sleep(2) print("点了呀") time.sleep(2) process = subprocess.Popen(aak8,shell=True) time.sleep(2) process = subprocess.Popen(aak8,shell=True) break def dj(): for ii in range(1,10): if sjjtjcwzopa(sjh,"./opgztc.png",0,31): break time.sleep(1) def ceshi(): pull_screenshot(2,sjh,2) def hua10(): for ab in range(1,11): abc=0 while 1: abc=abc+1 if abc%2==0: aa=wmjc(2,sjh,"./xmhba.png",2) else: aaa=wmjc(2,sjh,"./xmhba.png",2) try: if abs(aaa[0]-aa[0])<=5: huadong(sjh) break except: pass time.sleep(2) sjh="pvq4nrx4kzemw4ca" ak='adb -s {0} shell input keyevent 4'.format(sjh)#返回 aka='adb -s {0} shell input swipe 400 500 400 200'.format(sjh)#滑动 akb="adb -s {0} shell input tap 500 70".format(sjh)#退出直播 akc="adb -s {0} shell input tap 270 560".format(sjh)#关注退出直播 akd="adb -s {0} shell input tap 270 640".format(sjh)#不关注退出直播 ake="adb -s {0} shell input tap 250 690".format(sjh)#开宝箱 akf="adb -s {0} shell input tap 90 310".format(sjh)#开宝箱 akg="adb -s {0} shell input tap 300 750".format(sjh)#直播1 akh="adb -s {0} shell input tap 800 750".format(sjh)#直播2 aki="adb -s {0} shell input tap 300 1750".format(sjh)#直播3 akj="adb -s {0} shell input tap 800 1750".format(sjh)#直播4 #pull_screenshot(2,sjh,2) #sys.exit(0) fl=zb=0 while 1: sleep_time1 =int(random.uniform(3,8)) print(sleep_time1,time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))) beitua=["./xmksfl.png","./xmkslfl.png","./xmkskbx.png","./xmksqzq.png","./xmksksp.png"] #beitua=["./oplfl.png"] ci=0 for beitu in beitua: ci=ci+1 print("zb=",zb) if zb>=0 and beitu=="./xmkslfl.png": continue if sjjtjcwzopa(sjh,beitu,0,2): if beitu=="./xmksfl.png": fl=fl+1 time.sleep(30+sleep_time1) time.sleep(4) if sjjtjcwzopa(sjh,"./xmksfqjl.png",0,2): pass process = subprocess.Popen(ak,shell=True) #continue time.sleep(4) break else: if beitu=="./xmkslfl.png" and zb<=10: zb=zb+1 abx=int(random.uniform(1,4)) if abx==1: akka=akg elif abx==2: akka=akh elif abx==3: akka=aki else: akka=akj time.sleep(2) process = subprocess.Popen(akka,shell=True) time.sleep(65) process = subprocess.Popen(ak,shell=True) time.sleep(2) if sjjtjcwzopa(sjh,"./xmkstczb.png",0,2): pass if sjjtjcwzopa(sjh,"./xmkszbgztc.png",0,2): pass time.sleep(2) process = subprocess.Popen(ak,shell=True) time.sleep(2) break else: if beitu=="./xmabx.png": time.sleep(4) if sjjtjcwzopa(sjh,"./xmksbxa.png",0,2): pass if sjjtjcwzopa(sjh,"./xmksksp.png",0,2): pass #process = subprocess.Popen(ake,shell=True) time.sleep(65) process = subprocess.Popen(ak,shell=True) time.sleep(4) break elif beitu=="./xmksqzq.png": hua10() if sjjtjcwzopa(sjh,"./xmksfhzq.png",0,2): pass time.sleep(2) break else: time.sleep(65) process = subprocess.Popen(ak,shell=True) time.sleep(4) break else: time.sleep(4) if sjjtjcwzopa(sjh,"./xmksfqjl.png",0,2): pass time.sleep(sleep_time1) continue print("ci=",ci) if ci%3==0: time.sleep(5) process = subprocess.Popen(aka,shell=True) huadong(sjh) time.sleep(sleep_time1) ## ## #huadong(sjh) ## time.sleep(sleep_time1) #sjh="192.168.0.103:5555" #sjh="pvq4nrx4kzemw4ca" #sjh="2249f564"adb #sjjtjcwzxm(sjh,"./oplfl.png",0,31) #sjjtjcwzopa(sjh,"./oplfl.png",0,31) #pull_screenshot(1,sjh,31) # 截图 #sjjtjcwzopa(sjh,"./opgztc.png",0,31) #sjjtjcwzdj(2,sjh,"./gk.png",0,35) #sjjtjcwz(sjh,"./gk.png",0,35) #qutuzijia.jiancejx(sjh) #sjjtjc(2,sjh,"./xmqrxz.png",14)