手机顺风出行

# -*- coding: utf-8 -*-
"""
手机屏幕截图的代码: screenshot.py
"""
import subprocess,random
import os
import sys
from PIL import Image
import os
import time
import cv2


# SCREENSHOT_WAY 是截图方法,经过 check_screenshot 后,会自动递减,不需手动修改
SCREENSHOT_WAY = 1


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 find_button(target, template):
    """
    寻找target图片在template中的位置,返回应该点击的坐标。
    """
    theight,twidth = target.shape[:2]
    # 执行模板匹配,采用的匹配方式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)
sjh="pvq4nrx4kze"
aak="adb shell input swipe 200 500 200 1100"
aakx="adb -s {0} shell am start -n com.kuaishou.nebula/com.yxcorp.gifshow.HomeActivity".format(sjh)
xx=2
beitu="./shunfeng.png"
while 1:
    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)
    else:
        process = subprocess.Popen(aakx,shell=True)
        print("找到目标了")
        break
    time.sleep(10)
    #huadong(sjh)
    process = subprocess.Popen(aak,shell=True)
    time.sleep(2)

 

posted @ 2020-12-12 15:18  myrj  阅读(146)  评论(0编辑  收藏  举报