6.7.0 - Alpha13 - 修复 images.save 及 ImageWrapper#saveTo 方法传入路径不存在时无法正常写入文件的问题
This commit is contained in:
@@ -9,6 +9,7 @@ import androidx.core.graphics.get
|
||||
import org.autojs.autojs.annotation.ScriptInterface
|
||||
import org.autojs.autojs.core.opencv.Mat
|
||||
import org.autojs.autojs.core.opencv.OpenCVHelper
|
||||
import org.autojs.autojs.extension.AnyExtensions.toRuntimePath
|
||||
import org.autojs.autojs.pio.UncheckedIOException
|
||||
import org.autojs.autojs.runtime.ScriptRuntime
|
||||
import org.autojs.autojs.runtime.api.Images
|
||||
@@ -19,6 +20,7 @@ import org.opencv.core.CvType
|
||||
import org.opencv.core.Size
|
||||
import org.opencv.imgcodecs.Imgcodecs
|
||||
import org.opencv.imgproc.Imgproc
|
||||
import java.io.File
|
||||
import java.io.FileNotFoundException
|
||||
import java.io.FileOutputStream
|
||||
import java.lang.ref.WeakReference
|
||||
@@ -152,7 +154,14 @@ open class ImageWrapper : Shootable<ImageWrapper> {
|
||||
|
||||
fun saveTo(path: String): Boolean {
|
||||
ensureNotRecycled()
|
||||
val fullPath = mScriptRuntime.files.nonNullPath(path)
|
||||
val fullPath = path.toRuntimePath(mScriptRuntime, true)
|
||||
val file = File(fullPath)
|
||||
val parentFile = file.parentFile
|
||||
if (parentFile != null && !parentFile.exists()) {
|
||||
if (!parentFile.mkdirs()) {
|
||||
throw RuntimeException("Failed to create directory: ${parentFile.absolutePath}")
|
||||
}
|
||||
}
|
||||
if (mBitmap == null) {
|
||||
if (mMat != null) {
|
||||
return Imgcodecs.imwrite(fullPath, mMat)
|
||||
@@ -162,11 +171,10 @@ open class ImageWrapper : Shootable<ImageWrapper> {
|
||||
return runCatching { saveWithBitmap(fullPath) }.isSuccess
|
||||
}
|
||||
|
||||
private fun saveWithBitmap(fullPath: String?) {
|
||||
private fun saveWithBitmap(fullPath: String) {
|
||||
try {
|
||||
fullPath ?: throw Exception("Argument \"path\" cannot be null")
|
||||
mBitmap ?: throw Exception("Member \"bitmap\" cannot be null")
|
||||
mBitmap!!.compress(CompressFormat.PNG, 100, FileOutputStream(fullPath))
|
||||
val bitmap = mBitmap ?: throw Exception("Member \"bitmap\" cannot be null")
|
||||
bitmap.compress(CompressFormat.PNG, 100, FileOutputStream(fullPath))
|
||||
} catch (e: FileNotFoundException) {
|
||||
throw UncheckedIOException(e)
|
||||
}
|
||||
|
||||
@@ -132,6 +132,8 @@ object AnyExtensions {
|
||||
|
||||
fun Any?.jsBrief() = "${Context.toString(this)} (${this.jsSpecies()})"
|
||||
|
||||
@JvmStatic
|
||||
@JvmOverloads
|
||||
fun Any?.toRuntimePath(scriptRuntime: ScriptRuntime, isStrict: Boolean = false): String {
|
||||
if (isStrict && this.isJsNullish()) throw IllegalArgumentException(str(R.string.error_cannot_convert_value_into_a_script_runtime_path, this.jsBrief()))
|
||||
return scriptRuntime.files.nonNullPath(coerceString(this, "."))
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.autojs.autojs.runtime.api;
|
||||
|
||||
import org.autojs.autojs.extension.AnyExtensions;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
@@ -49,6 +50,7 @@ import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
@@ -298,7 +300,15 @@ public class Images {
|
||||
|
||||
public boolean save(@NonNull ImageWrapper image, @NonNull String path, @NonNull String format, int quality) throws IOException {
|
||||
try {
|
||||
return saveInternal(image, path, format, quality);
|
||||
var nicePath = AnyExtensions.toRuntimePath(path, mScriptRuntime, true);
|
||||
var file = new File(nicePath);
|
||||
File parentFile = file.getParentFile();
|
||||
if (parentFile != null && !parentFile.exists()) {
|
||||
if (!parentFile.mkdirs()) {
|
||||
throw new IOException("Failed to create parent directory for image save: " + parentFile.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
return saveInternal(image, nicePath, format, quality);
|
||||
} finally {
|
||||
shoot(image);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user