api(images): images.copy(), Image(width, height)

This commit is contained in:
hyb1996 2018-05-28 21:37:05 +08:00
parent 8379764193
commit 30dbe6e3f2
3 changed files with 23 additions and 1 deletions

View File

@ -69,6 +69,7 @@ runtime.init();
importClass(com.stardust.autojs.core.util.Shell);
importClass(android.graphics.Paint);
Canvas = com.stardust.autojs.core.graphics.ScriptCanvas;
Image = com.stardust.autojs.core.image.ImageWrapper;
//重定向require以便支持相对路径
(function(){

View File

@ -39,6 +39,15 @@ public class ImageWrapper {
mHeight = bitmap.getHeight();
}
public ImageWrapper(Bitmap bitmap, Mat mat) {
mBitmap = bitmap;
mMat = mat;
}
public ImageWrapper(int width, int height) {
this(Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888));
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static ImageWrapper ofImage(Image image) {
@ -124,7 +133,7 @@ public class ImageWrapper {
}
private void ensureNotRecycled() {
public void ensureNotRecycled() {
if (mBitmap == null && mMat == null)
throw new IllegalStateException("image has been recycled");
}

View File

@ -28,6 +28,7 @@ import com.stardust.concurrent.VolatileDispose;
import com.stardust.pio.UncheckedIOException;
import com.stardust.util.ScreenMetrics;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Rect;
@ -129,6 +130,17 @@ public class Images {
return false;
}
public ImageWrapper copy(ImageWrapper image) {
image.ensureNotRecycled();
if (image.getBitmap() == null) {
return new ImageWrapper(image.getMat().clone());
}
if (image.getMat() == null) {
return new ImageWrapper(image.getBitmap().copy(image.getBitmap().getConfig(), true));
}
return new ImageWrapper(image.getBitmap().copy(image.getBitmap().getConfig(), true), image.getMat().clone());
}
public boolean save(ImageWrapper image, String path, String format, int quality) throws IOException {
Bitmap.CompressFormat compressFormat = parseImageFormat(format);
if (compressFormat == null)