add: images.load(), landscape param of requestScreenCapture()

This commit is contained in:
hyb1996 2017-11-27 14:29:21 +08:00
parent d775b5e77c
commit f99e844747
14 changed files with 47 additions and 75 deletions

View File

@ -1,6 +1,6 @@
if(!requestScreenCapture()){
toast("请求截图失败");
stop();
exit();
}
var img = captureScreen();
toastLog("开始找色");

View File

@ -1,6 +1,6 @@
if(!requestScreenCapture()){
toast("请求截图失败");
stop();
exit();
}
var img = captureScreen();
//0xffffff为白色

View File

@ -1,19 +0,0 @@
//减少截图分辨率以提高速度
if(!requestScreenCapture(640, 960)){
toast("请求截图失败");
stop();
}
var img = captureScreen();
toastLog("开始找色");
//0x02b902为输入法绿色字体的颜色
var point = findColor(img, 0x02b902, {
//指定用8个线程找色
threads: 8
});
if(point){
toastLog("x = " + point.x + ", y = " + point.y);
}else{
toastLog("没有找到");
}

View File

@ -3,12 +3,6 @@ console.show();
events.observeTouch();
events.setTouchEventTimeout(30);
events.on("touch", function(point){
var img = captureScreen();
if(img == null){
var c = null;
}else{
var c = colors.toString(images.pixel(captureScreen(), point.x, point.y);
}
var c = colors.toString(images.pixel(captureScreen(), point.x, point.y);
log("(" + point.x + ", " + point.y + "): " + c);
log("");
});

View File

@ -1,6 +1,6 @@
if(!requestScreenCapture()){
toast("请求截图失败");
stop();
exit();
}
var img = captureScreen();
images.saveImage(img, "/sdcard/1.png");

View File

@ -1,6 +0,0 @@
//指定截图分辨率为 640×960
if(!requestScreenCapture(640, 960)){
toast("请求截图失败");
stop();
}
captureScreen("/sdcard/1.png");

View File

@ -1,6 +1,6 @@
if(!requestScreenCapture()){
toast("请求截图失败");
stop();
exit();
}
launchApp("QQ");
sleep(2000);

View File

@ -1,32 +0,0 @@
if(!requestScreenCapture()){
toast("请求截图失败");
stop();
}
var img = captureScreen();
//0xffffff为白色
toastLog("开始找色");
var point = findColor(img, 0xffffff, {
//指定算法为rgb+,比默认算法rgb更准确,但时间更久
algorithm: "rgb+",
//指定颜色临界值为16
threshold: 16,
//指定用8个线程找色
threads: 8
});
if(point){
toastLog("x = " + point.x + ", y = " + point.y);
}else{
toastLog("没有找到");
}
point = findColor(img, 0xffffff, {
//指定算法为颜色差值比默认算法rgb更快
algorithm: "diff",
//指定r, b, b分别都在范围ff±20内
threshold: 0x202020,
});
if(point){
toastLog("x = " + point.x + ", y = " + point.y);
}else{
toastLog("没有找到");
}

View File

@ -1,6 +1,6 @@
if(!requestScreenCapture()){
toast("请求截图失败");
stop();
exit();
}
var img = captureScreen();
//0x9966ff为编辑器紫色字体的颜色

View File

@ -0,0 +1,5 @@
//这个是Auto.js图标的地址
var url = "http://www.autojs.org/assets/uploads/profile/3-profileavatar.png";
var logo = images.load(url);
//保存到路径/sdcard/auto.js.png
images.save(logo, "/sdcard/auto.js.png");

View File

@ -1,5 +1,6 @@
if(!requestScreenCapture()){
toast("请求截图失败");
toast("请求截图失败");
exit
}
sleep(2000);
var x = 760;
@ -10,7 +11,7 @@ var c = images.pixel(captureScreen(), x, y);
var msg = "";
msg += "在位置(" + x + ", " + y + ")处的颜色为" + colors.toString(c);
msg += "\nR = " + colors.red(c) + ", G = " + colors.green(c) + ", B = " + colors.blue(c);
//检测在点(x, y)处是否有颜色0x73bdb6 (模糊比较)
//检测在点(x, y)处是否有颜色#73bdb6 (模糊比较)
var isDetected = images.detectsColor(captureScreen(), "#73bdb6", x, y);
msg += "\n该位置是否匹配到颜色#73bdb6: " + isDetected;
alert(msg);

View File

@ -19,8 +19,12 @@ module.exports = function(__runtime__, scope){
images.read = rtImages.read.bind(rtImages);
images.load = rtImages.load.bind(rtImages);
images.saveImage = rtImages.saveImage.bind(rtImages);
images.save = rtImages.saveImage;
images.pixel = rtImages.pixel;
images.detectsColor = function(img, color, x, y, threshold, algorithm){

View File

@ -103,7 +103,7 @@ public class ImageWrapper {
}
}
public int getPixel(int x, int y) {
public int pixel(int x, int y) {
if (mBitmap != null) {
return mBitmap.getPixel(x, y);
}

View File

@ -2,7 +2,6 @@ package com.stardust.autojs.runtime.api;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
@ -15,6 +14,8 @@ import android.view.Display;
import android.view.Surface;
import android.view.WindowManager;
import com.nickandjerry.dynamiclayoutinflator.lib.ImageLoader;
import com.nickandjerry.dynamiclayoutinflator.lib.util.Drawables;
import com.stardust.autojs.annotation.ScriptVariable;
import com.stardust.autojs.core.image.ColorFinder;
import com.stardust.autojs.core.image.ImageWrapper;
@ -23,7 +24,6 @@ import com.stardust.autojs.core.image.ScreenCapturer;
import com.stardust.autojs.core.image.TemplateMatching;
import com.stardust.autojs.runtime.ScriptRuntime;
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
import com.stardust.concurrent.VolatileBox;
import com.stardust.concurrent.VolatileDispose;
import com.stardust.pio.UncheckedIOException;
import com.stardust.util.ScreenMetrics;
@ -35,6 +35,9 @@ import org.opencv.core.Rect;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Locale;
/**
@ -77,6 +80,14 @@ public class Images {
return requestResult.blockedGetOrThrow(ScriptInterruptedException.class);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public boolean requestScreenCapture(boolean landscape) {
if (!landscape)
return requestScreenCapture(ScreenMetrics.getDeviceScreenWidth(), ScreenMetrics.getDeviceScreenHeight());
else
return requestScreenCapture(ScreenMetrics.getDeviceScreenHeight(), ScreenMetrics.getDeviceScreenWidth());
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public boolean requestScreenCapture() {
if (mDisplay.getRotation() == Surface.ROTATION_0 || mDisplay.getRotation() == Surface.ROTATION_180)
@ -125,7 +136,7 @@ public class Images {
public static int pixel(ImageWrapper image, int x, int y) {
x = ScreenMetrics.rescaleX(x, image.getWidth());
y = ScreenMetrics.rescaleY(y, image.getHeight());
return image.getPixel(x, y);
return image.pixel(x, y);
}
@ -134,6 +145,20 @@ public class Images {
return ImageWrapper.ofBitmap(bitmap);
}
public ImageWrapper load(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
return ImageWrapper.ofBitmap(bitmap);
} catch (IOException e) {
return null;
}
}
public static void saveBitmap(Bitmap bitmap, String path) {
try {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(path));