新增 images.rotate(), images.concat()旋转和拼接图片,参见《图片处理》示例

This commit is contained in:
hyb1996 2018-11-02 00:01:08 +08:00
parent d3c073fa9d
commit d8d02a1137
4 changed files with 110 additions and 9 deletions

View File

@ -2,17 +2,33 @@
var url = "https://www.autojs.org/assets/uploads/files/1540386817060-918021-20160416200702191-185324559.jpg";
var logo = null;
var currentImg = null;
events.on("exit", function(){
if(logo != null){
logo.recycle();
}
if(currentImg != null){
currentImg.recycle();
}
});
ui.layout(
<vertical>
<img id="img" w="250" h="250" url="{{url}}" />
<button id="grayscale" text="灰度化" />
<button id="binary" text="二值化" />
<button id="adaptiveBinary" text="自适应二值化" />
<button id="hsv" text="RGB转HSV" />
<button id="blur" text="模糊" />
<button id="medianBlur" text="中值滤波" />
<button id="gaussianBlur" text="高斯模糊" />
<scroll>
<vertical>
<button id="rotate" text="旋转" />
<button id="concat" text="拼接" />
<button id="grayscale" text="灰度化" />
<button id="binary" text="二值化" />
<button id="adaptiveBinary" text="自适应二值化" />
<button id="hsv" text="RGB转HSV" />
<button id="blur" text="模糊" />
<button id="medianBlur" text="中值滤波" />
<button id="gaussianBlur" text="高斯模糊" />
</vertical>
</scroll>
</vertical>
);
@ -36,11 +52,36 @@ function processImg(process) {
}
//处理图片
var result = process(logo);
if(currentImg != null){
currentImg.recycle();
}
currentImg = result;
//把处理后的图片设置到图片控件中
setImage(result);
}, 0);
}
var degress = 0;
ui.rotate.on("click", () => {
processImg(img => {
degress += 90;
//旋转degress角度
return images.rotate(img, degress);
});
});
ui.concat.on("click", () => {
processImg(img => {
if(currentImg == null){
toast("请先点击其他按钮,再点击本按钮");
return img.clone();
}
//把currentImg拼接在img右边
return images.concat(img, currentImg, "right");
});
});
ui.grayscale.on("click", () => {
processImg(img => {
//灰度化

View File

@ -189,6 +189,23 @@ module.exports = function (runtime, scope) {
return images.matToImage(mat);
}
images.rotate = function(img, degree, x, y) {
if(x == undefined){
x = img.width / 2;
}
if(y == undefined){
y = img.height / 2;
}
return javaImages.rotate(img, x, y, degree);
}
images.concat = function(img1, img2, direction, rect1, rect2) {
direction = direction || "right";
rect1 = buildRegion(rect1, img1);
rect2 = buildRegion(rect2, img1);
return javaImages.concat(img1, rect1, img2, rect2, android.view.Gravity[direction.toUpperCase()]);
}
images.detectsColor = function (img, color, x, y, threshold, algorithm) {
color = parseColor(color);
algorithm = algorithm || "diff";
@ -345,6 +362,9 @@ module.exports = function (runtime, scope) {
}
function buildRegion(region, img) {
if(region == undefined){
region = [];
}
var x = region[0] === undefined ? 0 : region[0];
var y = region[1] === undefined ? 0 : region[1];
var width = region[2] === undefined ? img.getWidth() - x : region[2];

View File

@ -3,6 +3,7 @@ package com.stardust.autojs.core.ui.dialog;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.location.LocationManager;
import android.os.Build;
import android.os.Looper;
import android.support.annotation.Nullable;

View File

@ -5,14 +5,15 @@ import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.media.Image;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.RequiresApi;
import android.util.Base64;
import android.view.Display;
import android.view.WindowManager;
import android.view.Gravity;
import com.stardust.autojs.annotation.ScriptVariable;
import com.stardust.autojs.core.image.ColorFinder;
@ -39,6 +40,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
/**
* Created by Stardust on 2017/5/20.
@ -145,6 +147,43 @@ public class Images {
return image.pixel(x, y);
}
public static ImageWrapper concat(ImageWrapper img1, Rect rect1, ImageWrapper img2, Rect rect2, int direction) {
if(!Arrays.asList(Gravity.LEFT, Gravity.RIGHT, Gravity.TOP, Gravity.BOTTOM).contains(direction)){
throw new IllegalArgumentException("unknown direction " + direction);
}
int width;
int height;
if (direction == Gravity.LEFT || direction == Gravity.TOP) {
ImageWrapper tmp = img1;
img1 = img2;
img2 = tmp;
}
if (direction == Gravity.LEFT || direction == Gravity.RIGHT) {
width = rect1.width + rect2.width;
height = Math.max(rect1.height, rect2.height);
} else {
width = Math.max(rect1.width, rect2.height);
height = rect1.height + rect2.height;
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
if (direction == Gravity.LEFT || direction == Gravity.RIGHT) {
canvas.drawBitmap(img1.getBitmap(), 0, (height - rect1.height) / 2, paint);
canvas.drawBitmap(img2.getBitmap(), rect1.width, (height - rect2.height) / 2, paint);
} else {
canvas.drawBitmap(img1.getBitmap(), (width - rect1.width) / 2, 0, paint);
canvas.drawBitmap(img2.getBitmap(), (width - rect2.width) / 2, rect1.height, paint);
}
return ImageWrapper.ofBitmap(bitmap);
}
public ImageWrapper rotate(ImageWrapper img, float x, float y, float degree) {
Matrix matrix = new Matrix();
matrix.postRotate(degree, x, y);
return ImageWrapper.ofBitmap(Bitmap.createBitmap(img.getBitmap(), 0, 0, img.getWidth(), img.getHeight(), matrix, true));
}
public ImageWrapper clip(ImageWrapper img, int x, int y, int w, int h) {
return ImageWrapper.ofBitmap(Bitmap.createBitmap(img.getBitmap(), x, y, w, h));
}