论文写完,感觉头脑好久没被灵感刺激了,前些天室友介绍了个小游戏,我突然来了灵感可以写的简单的android 程序实现自动运行。主要的过会为三步:

1,Android 屏幕的获取。因为安全的原因,过程比较麻烦,我目前采用的是开启用户调试模式,利用adb脚本反复循环截图。

2,图像分析。这部分代码中有体现,过程比较简单。

3,模拟Click。代码中已经体,,我采用了一种最简单的方法,代码将在下面做详细分析。

先上个图,一口气跑到183分:

分析图片的代码如下,具体过程为:先获取图像->找到纯黑色的区域->分析黑色的间隔->根据间隔计算时间. 其中根据图像获取的一行数据如右侧图,1为黑色区域,0为非黑色区域,

根据1、0便可以计算宽度了.

 package com.hennsun.decode;

 import android.graphics.Bitmap;
import android.util.Log;
public class DecodeImage { /*
* -16777216 表示ARGB的纯黑色
* */
public static byte[] getLightValue(Bitmap image){
int wight = image.getWidth();
int hight = image.getHeight();
int loc = (int)(hight*8/9.5);
byte[] dataA = new byte[wight];
for(int i = 0;i<wight;i++){
if(image.getPixel(i, loc) == -16777216)
dataA[i] = 1;
}
return dataA;
} /**
* 得到间隔宽度
* @param light
* @return
*/
public static int decodeGap(byte[] light){
int start = 0,end1 =0,end2 = light.length ;
for(int i = 0;i<light.length-1;i++){
if(light[i+1]<light[i]){
start = i+1;
Log.d("Plug", "start is " + Integer.toString(start));
break;
}
}
for(int i = start;i<light.length-1;i++){
if(light[i+1]>light[i]){
end1 = i;
Log.d("Plug", "end1 is " + Integer.toString(end1));
break;
}
}
for(int i = end1+1 ;i<light.length-1;i++){
if(light[i+1]<light[i]){
end2 = i;
Log.d("Plug", "end2 is " + Integer.toString(end2));
break;
}
}
if(start == end2+1)
return 0;
else
return (end1+end2)/2 - start;
} /**
* 获得点击的时间
* @param image 游戏的界面
* @param index 为 像素值/ms
* @return
*/
public static float getTime(Bitmap image,float index){
float time = 0;
int gap = 0;
byte[] gray = getLightValue(image);
gap = decodeGap(gray); //return pixe counts.
time = gap/index; //这里采用可调整系数。
Log.d("Plug","the width of the gap is "+Float.toString((float) (5.35*gap/720))+"cm");
return time; } }

关于屏幕的截图,我可以使用adb方式,脚本如下。当然方式比较的多,我选择了相对比较简单的。

:abc
adb shell screencap -p /sdcard/Demo/screen.bmp
ping 127.0.0.1 -n 10>null
goto abc

或下面这种方式都可以实现截屏,我已经验证完全没有问题,但是对就处理流程就有点不同了.

 package com.hennsun.runtime;

 import java.io.BufferedOutputStream;
import java.io.PrintStream; import android.util.Log; public class CaptureScreen {
/**
* http://my.oschina.net/u/2241960/blog/330485
* @param path 图片保存路径
*/
public static void screenshot(String path){
Process process = null;
Log.d("Plug","start to capture screen");
try{
process = Runtime.getRuntime().exec("su");
PrintStream outputStream = null;
try {
outputStream = new PrintStream(new BufferedOutputStream(process.getOutputStream(), 8192));
outputStream.println("screencap -p " + path);
outputStream.flush();
}catch(Exception e){
e.printStackTrace();
} finally {
if (outputStream != null) {
outputStream.close();
}
}
process.waitFor();
}catch(Exception e){
e.printStackTrace();
}finally {
if(process != null){
process.destroy();
}
}
}
}

模拟Touch,我是从下面的参考的部分获取的代码,根据时间间隔便可以操作Touch事件了,不过需要软件Root权限,代码如下:

 /**
* simulate Click
* @param time
*/
private void simulateClick(float time){
try{
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
//String cmd = "/system/bin/input tap 100 200\n";
//time 为 ms
String timeS = String.valueOf((int)time);
Log.d("Plug", "the necessary time is "+timeS);
String cmd = "/system/bin/input swipe 100 200 100 200 "+timeS+"\n";
os.writeBytes(cmd);
os.writeBytes("exit\n");
os.flush();
os.close();
process.waitFor();
}catch(Exception e){ }
}

以上代码仅做学习交流使用,本文原创,且勿转载!!

视频展示:

Youtube展示链接 https://www.youtube.com/watch?v=sF0PuKGJFUI&feature=youtu.be

这是国外另外一个团队做了,应该是印度人,他做的比较麻烦.

https://www.youtube.com/watch?v=dJW59UliLhc

需要源码的可以访问我的 个人主页 http://www.shareideas.net/

参考:

http://w3facility.org/question/how-to-simulate-touch-from-background-service-with-sendevent-or-other-way/

http://stackoverflow.com/questions/11142843/how-can-i-use-adb-to-send-a-longpress-key-event

https://grymoire.wordpress.com/2014/09/17/remote-input-shell-scripts-for-your-android-device/

Stick hero "攻略", android 代码编写与分析(后台截屏, 后台模拟点击)的更多相关文章

  1. Android开发笔记:安卓程序截屏方法

    1,基于Android SDK的截屏方法 (1)主要就是利用SDK提供的View.getDrawingCache()方法.网上已经有很多的实例了.首先创建一个android project,然后进行L ...

  2. android后台截屏实现(2)--screencap源码修改

    首先找到screencap类在Android源码中的位置,/442/frameworks/base/cmds/screencap/screencap.cpp 源码如下: /* * Copyright ...

  3. android后台截屏实现(3)--编译screencap

    修改好之后就要编译了,screencap的编译是要在源码环境中进行的. 将修改后的screencap.cpp文件替换源码中的原始文件,然后修改screencap的Android.mk文件,修改后的文件 ...

  4. 【Android实战】Bitmap图片的截屏、模糊处理、传递、使用

    项目中遇到了这样一个需求: 当某个条件满足时就截取当前屏幕.并跳转到另外一个页面,同一时候将这个截屏图片作为下一个页面的背景图片,同一时候背景图片须要模糊处理 接下来就一步一步解决这个问题: 1.截取 ...

  5. 二叉查找树速通攻略 图文代码精心编写(Java实现)

    说在前面 如题目所言 这篇文章为了给下一篇二叉查找数做铺垫和前期知识准备,以便大家有良好的阅读体验,本来想合在一起的,但觉得有些长,所以就拆开了哈哈哈,还是新手向,两篇文章有些长,但如果能认真看下去, ...

  6. mac攻略(七) -- 环境变量PATH分析

      一.首先需要了解 1>mac 一般使用bash作为默认shell 2>Mac系统的环境变量,加载顺序为: 1.系统级别的 /etc/profile /etc/bashrc /etc/p ...

  7. android后台截屏实现(1)--源码编译

    前段时间接到任务要实现后台截图并上传的功能,在网上查了好久,发现遇到这类问题的人还不少.经过一番对比后发现还是修改并编译源码中的screencap类然后通过JNI来调用这种方法比较可靠,而其他的在ja ...

  8. 【转】Android 音量键+电源键 截屏代码小结

    http://104zz.iteye.com/blog/1752961 原文地址:http://blog.csdn.net/hk_256/article/details/7306590 ,转载请注明出 ...

  9. [置顶] Android 应用内禁止截屏功能的实现

    截图介绍   Android的调试工具DDMS提供有截屏功能,很多软件也会有截屏功能,在做支付等安全类应用的时候,为了保证用户的资产和系统安全,往往会禁止应用内截屏,禁止之后,在此应用处于前台的情况下 ...

随机推荐

  1. Object.prototype 与 Function.prototype 与 instanceof 运算符

    方法: hasOwnProperty isPrototypeOf propertyIsEnumerable hasOwnProperty 该方法用来判断一个对象中的某一个属性是否是自己提供的( 住要用 ...

  2. Eclipse默认空间与工作空间的更改(转)

    一.更改eclipse默认空间 进行 eclipse 目录下的 configuration 目录, 打开config.ini文件 将 osgi.instance.area.default= 项修改成你 ...

  3. Destoon B2B 调优SQL后 生成首页仍然慢或不成功的原因

    修改php.ini文件,查找 ;date.timezone = ,把前面的分号去掉在 “=”后面加上时区. 比如:Asia/Shanghai (上海) 自动task有上面的问题 但后台生成时这样修改后 ...

  4. ps技巧

    ADOBE PHOTOSHOP 同义词 PS(位图图像处理软件Photoshop)一般指ADOBE PHOTOSHOP 本词条由“科普中国”百科科学词条编写与应用工作项目 审核 . Adobe Pho ...

  5. php生成唯一订单号

    支持更改长度/** * 生成唯一订单号 * */ function build_order_no(){ return date('Ymd').substr(implode(NULL, array_ma ...

  6. Xcode6中如何使用自定义的类模板

    说到IOS类的模板,有些人感觉很陌生,但是只要有开发过IOS程序的人,其实都用过类的模板,只不过是用的系统自带的类的模板. 例如创建一个ClassTemplateVC继承于UIViewControll ...

  7. hash连接

    简单回顾嵌套循环: 两个表关联,较小的表(指使用了过滤条件后结果集较小的表)称为驱动表或者外表(,另一个称为内表.在嵌套连接过程中,oracle首先读取驱动表的第一条数据,然后和内表进行比对,所以匹配 ...

  8. Android 学习第12课,应用出错信息

    应用在运行时,出现的错误信息都会在LogCat中显示 如果调出LogCat ? 菜单:窗口 -> 显示视图 -> 其他 -> LogCat

  9. 2015.10.15night

    #include<stdio.h> main() { int x,y; scanf("%d",&x); if(x>0)y=1; else {if(x< ...

  10. URL参数为url,获取不到部分参数问题

    url1中的参数含有url2,在页面上获取url时发现url后面跟的参数获取不到,其实是浏览器把url2中&后的参数作为url1的参数来处理了. 如:http://www.ilcng.com/ ...