In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the Android version of Wechat jump automatic execution code example analysis, the article is very detailed, has a certain reference value, interested friends must read it!
The overall structure
The overall structure of the script is relatively simple, as shown in the following figure.
Mobile phone connection PC,PC takes a screenshot of the mobile game interface through the adb command
PC copies the screenshot back to PC through the adb command
The PC side processes the image through python (opencv used in the first edition is currently used to directly read the RGB value of pixels), obtains the position of the chess piece, obtains the position of the next chessboard, and then calculates the distance of the next hop, thus calculating the pressing time t according to the empirical value.
The jump of chess pieces can be realized by simulating pressing time t by adb command.
Repeat 1x4 to automate the "jump" game.
Android phone screen coordinates
Code analysis
Main function code
Def main (): # get device information dump_device_info () # check adb check_adb () # main loop while True: # take screenshots through adb And copy the picture back to PC pull_screenshot () # load the picture into memory im = Image.open ('. / autojump.png') # get the position of chess pieces and board piece_x, piece_y, board_x, board_y = find_piece_and_board (im) # print debugging information ts = int (time.time ()) print (ts, piece_x, piece_y, board_x Board_y) # set the position of the press to the position of [another round] When this fails, you can automatically start set_button_position (im) # to calculate the distance of the next hop, convert it into time according to the empirical value, and send it to the device via adb to simulate pressing jump (math.sqrt ((board_x-piece_x) * * 2 + (board_y-piece_y) * * 2)) # to take screenshots of the debugging interface to facilitate debugging. And backup the debug screenshot to save_debug_creenshot (ts, im, piece_x, piece_y, board_x, board_y) backup_screenshot (ts) # in order to ensure that the screenshot should be stable, delay time.sleep (random.uniform (1,1.1) for a while)
Issue the screenshot command through adb, and copy the screenshot back to PC. Here, the adb command is directly used without much explanation.
Def pull_screenshot (): os.system ('del autojump.png') os.system (' adb shell screencap-p / sdcard/autojump.png') os.system ('adb pull / sdcard/autojump.png.')
Calculation coordinate
The find_piece_and_board function is the core code, which mainly does two things: one is to calculate the position of the chess piece; the other is to calculate the position of the next chessboard. Let's pick the main code to say.
1. Find the position coordinates of the chess piece
# here is to simply look up the scope, in fact, you don't have to add it, but it's a waste of time to traverse the whole screen. The author did two things # 1. First look for screen 1 / 2 / 3 / 3 range, top-to-bottom # 2. The search for a point that is different from the background color stops, indicating that the pixel below from this height may be a chess piece or pedestal for i in range (int (h / 3), int (hog2 / 3), 50): last_pixel = im_pixel [0memi] for j in range (1, w): pixel=im_pixel [jjjjI] # is not a solid color line, then record the value of scan_start_y Ready to jump out of the loop # pixel array 012 is the RGB trichromatic value respectively, as long as there is a difference indicating that the point is not the background color if pixel [0]! = last_pixel [0] or pixel [1]! = last_pixel [1] or pixel [2]! = last_pixel [2]: # rewind 50 pixels up Lest the height above is not the top different pixel scan_start_y = I-50 break # jump out of the loop if scan_start_y: break # the author begins to follow the top-down detailed traversal of the above point # scan down from scan_start_y, the chess piece should be in the upper half of the screen It is tentatively set to be no more than 2 for i in range 3 for i in range (scan_start_y, int (h * 2 + 3)): # Abscissa removes a certain boundary, and then begins to traverse, saving some time for j in range (scan_x_border, w-scan_x_border): # take out the coordinate points on this coordinate pixel = im_pixel [jjinger I] # here is the lowest line to find the chess piece and judge by color The range of RGB is pre-selected by the author # according to the color judgment of the lowest row of the chess piece, find the average of those points in the last row, this color should be OK, do not mention if (50)
< pixel[0] < 60) and (53 < pixel[1] < 63) and (95 < pixel[2] < 110): #像素点的横坐标值之和,因为棋子是对称的,这些横坐标的平均值就是棋子的中心位置 piece_x_sum += j piece_x_c += 1 #棋子最低点所处的位置 piece_y_max = max(i, piece_y_max) #如果其中有一个为0,则直接返回异常 if not all((piece_x_sum, piece_x_c)): return 0, 0, 0, 0 #平均得到棋子的横坐标 piece_x = piece_x_sum / piece_x_c #棋子的最低点并不是棋子所在的中心位置,需要补偿一定的值,这个值就是棋子底盘的高度一半 piece_y = piece_y_max - piece_base_height_1_2 2.查找下一跳底盘的坐标 #同样,查找缩小查找范围,只查找屏幕1/3~2/3范围之内的 for i in range(int(h / 3), int(h * 2 / 3)): #取边界上的坐标作为上一次坐标初始值。该变量的作用是判断像素是否变化,如果变化则进入了底座像素 last_pixel = im_pixel[0, i] #如果计算得到了坐标,跳出循环 if board_x or board_y: break #与查找棋子类型,底座也是对称的,则像素点横坐标的平均值就是底座的中心点 board_x_sum = 0 board_x_c = 0 #开始查找底座,横向扫描 for j in range(w): #取出一个像素点 pixel = im_pixel[j,i] # 修掉脑袋比下一个小格子还高的情况的 bug if abs(j - piece_x) < piece_body_width: continue #像素点的RGB值发生了变化,则说明进入了底座像素区域 # 修掉圆顶的时候一条线导致的小 bug,这个颜色判断应该 OK,暂时不提出来 if abs(pixel[0] - last_pixel[0]) + abs(pixel[1] - last_pixel[1]) + abs(pixel[2] - last_pixel[2]) >10: board_x_sum + = j board_x_c + = 1 # calculate the Abscissa of the base if board_x_sum: board_x = board_x_sum / board_x_c # the next base is in the 30-degree direction of the current base, so the vertical coordinate of the base can be calculated according to the Abscissa calculated above # according to the actual angle Find the coordinates close to the center of the next board where the angle should be 30 °, the value should be tan 30 °, math.sqrt (3) / 3 board_y = piece_y-abs (board_x-piece_x) * math.sqrt (3) / 3 # if one value is empty, return exception if not all ((board_x, board_y)): return 0,0,0,0
Make a jump
Knowing the current coordinates and the coordinates of the next hop, you can calculate the distance between the two points.
Math.sqrt ((board_x-piece_x) * 2 + (board_y-piece_y) * * 2)
Then pass the value to the jump function.
# this function calculates the press time def jump (distance) from the distance according to the empirical value: press_time = distance * press_coefficient press_time = max (press_time, 200) # set 200 ms is the minimum press time press_time = int (press_time) # simulate the press command cmd = 'adb shell input swipe {x1} {x1} {x2} {duration}' .format (x1=swipe ['x1'], y1=swipe [' y1'] via adb transmission X2=swipe ['x2'], y2=swipe [' y2'], duration=press_time) print (cmd) os.system (cmd) these are all the contents of this article entitled "sample Analysis of one-hop automatic Code execution in Android Wechat" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.