api(images): images.copy(), Image(width, height)
This commit is contained in:
@@ -69,6 +69,7 @@ runtime.init();
|
|||||||
importClass(com.stardust.autojs.core.util.Shell);
|
importClass(com.stardust.autojs.core.util.Shell);
|
||||||
importClass(android.graphics.Paint);
|
importClass(android.graphics.Paint);
|
||||||
Canvas = com.stardust.autojs.core.graphics.ScriptCanvas;
|
Canvas = com.stardust.autojs.core.graphics.ScriptCanvas;
|
||||||
|
Image = com.stardust.autojs.core.image.ImageWrapper;
|
||||||
|
|
||||||
//重定向require以便支持相对路径
|
//重定向require以便支持相对路径
|
||||||
(function(){
|
(function(){
|
||||||
|
|||||||
@@ -39,6 +39,15 @@ public class ImageWrapper {
|
|||||||
mHeight = bitmap.getHeight();
|
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)
|
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
|
||||||
public static ImageWrapper ofImage(Image image) {
|
public static ImageWrapper ofImage(Image image) {
|
||||||
@@ -124,7 +133,7 @@ public class ImageWrapper {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ensureNotRecycled() {
|
public void ensureNotRecycled() {
|
||||||
if (mBitmap == null && mMat == null)
|
if (mBitmap == null && mMat == null)
|
||||||
throw new IllegalStateException("image has been recycled");
|
throw new IllegalStateException("image has been recycled");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import com.stardust.concurrent.VolatileDispose;
|
|||||||
import com.stardust.pio.UncheckedIOException;
|
import com.stardust.pio.UncheckedIOException;
|
||||||
import com.stardust.util.ScreenMetrics;
|
import com.stardust.util.ScreenMetrics;
|
||||||
|
|
||||||
|
import org.opencv.core.Core;
|
||||||
import org.opencv.core.Mat;
|
import org.opencv.core.Mat;
|
||||||
import org.opencv.core.Point;
|
import org.opencv.core.Point;
|
||||||
import org.opencv.core.Rect;
|
import org.opencv.core.Rect;
|
||||||
@@ -129,6 +130,17 @@ public class Images {
|
|||||||
return false;
|
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 {
|
public boolean save(ImageWrapper image, String path, String format, int quality) throws IOException {
|
||||||
Bitmap.CompressFormat compressFormat = parseImageFormat(format);
|
Bitmap.CompressFormat compressFormat = parseImageFormat(format);
|
||||||
if (compressFormat == null)
|
if (compressFormat == null)
|
||||||
|
|||||||
Reference in New Issue
Block a user