申请会员ID:噜啦噜啦嘞
1、申 请 会员I D:噜啦噜啦嘞2、个人邮箱:2226031109@qq.com
3、原创技术文章:Java实现QQ/微信抢红包
代码如下:
package com.baidu.util;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class LuckyMoney {
public static String select="";
public static boolean running =false;
public static int step=0;
public static int x;
public static int y;
public static void main(String[] args) {
showView();
}
private static final String path = LuckyMoney.class.getResource("").getFile();
public static final JLabel error = new JLabel("请确保未被360连接");
public static final String start ="开始监测",stop="停止监测";
public static final JButton botton = new JButton(start);
public static void showView() {
JFrame jframe = new JFrame();
jframe.setSize(220, 220);
jframe.setTitle("抢红包");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setLocationRelativeTo(null);
FlowLayout layout = new FlowLayout(FlowLayout.CENTER, 0, 10);
jframe.getContentPane().setLayout(layout);
jframe.getContentPane().add(new JLabel("请打开USB调试"));
JPanel jpanel = new JPanel();
jpanel.add(new JLabel("请选择红包类型:"));
JComboBox<String> comboBox=new JComboBox<String>();
comboBox.addItem("微信红包");
comboBox.addItem("QQ红包");
jpanel.add(comboBox);
jframe.getContentPane().add(jpanel);
select = comboBox.getSelectedItem().toString();
comboBox.addActionListener(e -> {
select=comboBox.getSelectedItem().toString();
System.out.println(select);
});
System.out.println(select);
JPanel jp = new JPanel(new GridLayout(2,1,0,10));
error.setForeground(Color.red);
jp.add(error);
botton.addActionListener(ae->{
if(!running) {
new Thread(()->{
try {
if(select.equals("微信红包")) {
LuckyMoney.weChatRun();
}else {
LuckyMoney.QQRun();
}
}catch(Exception e) {
System.out.println(e.getMessage());
String msg = "分析图片错误";
stop(msg);
}
}).start();
}else {
stop("=====已停止运行=====");
}
});
jp.add(botton);
jframe.getContentPane().add(jp);
jframe.setVisible(true);
}
public static void weChatRun() throws Exception {
running = true;
botton.setText(stop);
error.setText("=====正在运行中=====");
while(running) {
step=1;//截屏
File pic =getScreenPic();
step=2;//分析图片
boolean red = weChatscanPic(pic);
System.out.println(red);
step=3;//点击红包
if(red) {
String cmd = "adb shell input swipe " + x+" "+y+" "+x+" "+y+" 1";
exec(cmd);
System.out.println(cmd);
Thread.sleep(100);
exec("adb shell input swipe 538 1120 538 1120 1");
Thread.sleep(1000);
exec("adb shell input swipe 304 1870 304 1870 20");
}
}
}
public static void stop(String msg) {
running =false;
botton.setText(start);
error.setText(msg);
}
public static File getScreenPic() throws Exception {
File pic = new File("E:/图片/截图/screen.png");
if (pic.exists()) {//备份一下之前的一张图片
File back = new File(path + System.currentTimeMillis()+".png");
if (!back.exists() || back.delete()) pic.renameTo(back);
}
exec("adb shell screencap -p /sdcard/screen.png");
exec("adb pull /sdcard/screen.png E:/图片/截图/screen.png" );
return pic;
}
private static void exec(String cmd) throws Exception {
Process ps = null;
try {
System.out.println(cmd);
ps = Runtime.getRuntime().exec(cmd.split(" "));
int code = ps.waitFor();
if (code != 0) throw new Exception("exec error(code=" + code + "): " + cmd);
} finally {
if (ps != null) ps.destroy();
}
}
public static boolean weChatscanPic(File pic) throws IOException {
BufferedImage bi = ImageIO.read(pic);
int x1=220,y1=1526,x2=230,y2=230;
for(int i=x1;i<x2;i++) {
for(int j=y1;j>y2;j--) {
if(!colorDiff(bi.getRGB(i, j),216<<16|60<<8|30)) {
x=i;
y=j;
return true;
}
}
}
return false;
}
static boolean colorDiff(int c1, int c2){
int c11 = c1>>16 & 0xFF,c12 = c1>>8 & 0xFF,c13 = c1 & 0xFF;
int c21 = c2>>16 & 0xFF,c22 = c2>>8 & 0xFF,c23 = c2 & 0xFF;
return Math.abs(c11 - c21) > 5 || Math.abs(c12 - c22) > 5 || Math.abs(c13 - c23) > 5;
}
public static void QQRun() throws Exception {
running = true;
botton.setText(stop);
error.setText("=====正在运行中=====");
while(running) {
step=1;//截屏
File pic =getScreenPic();
step=2;//分析图片
boolean red = QQscanPic(pic);
System.out.println(red);
step=3;//点击红包
if(red) {
String cmd = "adb shell input swipe " + (x+20)+" "+(y-20)+" "+(x-20)+" "+(y+20)+" 1";
exec(cmd);
System.out.println(cmd);
Thread.sleep(1000);
exec("adb shell input swipe 304 1870 304 1870 20");
}
}
}
public static boolean QQscanPic(File pic) throws IOException {
BufferedImage bi = ImageIO.read(pic);
int x1=240,y1=1560,x2=250,y2=230;
for(int i=x1;i<x2;i++) {
for(int j=y1;j>y2;j--) {
if(!colorDiff(bi.getRGB(i, j),230<<16|37<<8|85)) {
x=i;
y=j;
return true;
}
}
}
return false;
}
}
截图:
目前仅支持安卓手机,运行前须下载adb,配置adb环境,使用数据线连接手机,并打开手机的USB调试功能,将手机界面调至群界面,计算机将分析是否有红包,若有则点击红包,该代码实用性不大,但在某些情况下还是具有方便性,且根据相同的原理,可以设计出其他程序,例如我写出计算机控制手机批量存储手机图片及重命名图片(对我来说,我运行了一个发漫画的微信公众号,需要批量存储图片且通过重命名图片来对图片排序),该代码是根据之前学习到的微信跳一跳外挂程序改编,但由于微信对外挂的监测力度加大,该代码已失效,本人大一学生一枚,技术尚在学习中,望通过。 这类开源代码非常多,发一些自己原创的分析吧,这个还不足以申请。
页:
[1]