“This is the fourth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Tooth uncle tutorial is easy to understand

Autojs image data representation, a total of three

  • img: com.stardust.autojs.core.image.ImageWrapper
  • bitmap
  • mat

The three image data formats are converted to each other

Img to Bitmap and MAT

img.bitmap
img.mat
Copy the code

Turn bitmap img

com.stardust.autojs.core.image.ImageWrapper.ofMat(bitmap)
Copy the code

Mat turn img

com.stardust.autojs.core.image.ImageWrapper.ofMat(mat)
Copy the code

Turn bitmap mat

org.opencv.android.Utils.bitmapToMat(bitmap, mat);
Copy the code

Mat turn bitmap

org.opencv.android.Utils.matToBitmap(mat, bitmap);
Copy the code

Will bitmap and MAT be affected when IMG is recycled

Public function

Function printInfo(tag, img, bitmap, mat) {let arr = ["\n"]; arr.push("tag = " + tag); try { arr.push(java.lang.String.format("%1$-9s: width = %2$s, height = %3$s", "img", img.width, img.height)); } catch (e) { arr.push(java.lang.String.format("%1$-9s: %2$ss", "img", e)); } arr.push( java.lang.String.format( "%1$-9s: width = %2$s, height = %3$s, allocationByteCount = %4$s", "bitmap", bitmap.width, bitmap.height, bitmap.allocationByteCount ) ); arr.push(java.lang.String.format("%1$-9s: width = %2$s, height = %3$s", "mat", mat.width(), mat.height())); log(arr.join("\n")); Img function viewImg(img) {images.save(img, "/sdcard/1.jpg", "JPG ", 50); app.viewFile("/sdcard/1.jpg"); } function viewMat(mat) {let mat2 = mat.clone(); Imgproc.cvtColor(mat, mat2, Imgproc.COLOR_BGRA2RGBA); Let tempFilePath = files.join(files.getsdcardPath (), "script "," mate.png "); let tempFilePath = files.join(files.getsdcardPath (), "script "," mate.png "); Imgcodecs.imwrite(tempFilePath, mat2); mat2.release(); app.viewFile(tempFilePath); }Copy the code

The test code

Let imgPath = files.path("./ img.png "); let img = images.read(imgPath); let bitmap = img.bitmap; let mat = img.mat; PrintInfo (" original image information ", img, bitmap, mat); img.recycle(); PrintInfo (" release image info after IMG ", img, bitmap, mat);Copy the code

The log

Img: width = 564.0, height = 564.0 bitmap: Width = 564.0, height = 564.0, allocationByteCount = 1272384.0 mat: Width = 564.0, height = 564.0 11-19 17:56:54. 150 Script - 56 Main [remote: / / 789216 b9dbb2c184ad7efa6ee6d2c830 / Main js] / D: Img tag = released after the img picture information: JavaException: Java. Lang. An IllegalStateException: image has had recycleds bitmap: Mat: width = 0.0, height = 0.0Copy the code

It can be seen from the log that after IMG is recovered, bitmap is the same width and height, but its size has changed to 0. Mat’s width and height is 0, that is to say, after IMG is recovered, bitmap and MAT must also be recovered

Will img and MAT be affected when bitmap is recycled

The test code

Let imgPath = files.path("./ img.png "); let img = images.read(imgPath); let bitmap = img.bitmap; let mat = img.mat; PrintInfo (" original image information ", img, bitmap, mat); bitmap.recycle(); // mat.release(); PrintInfo (" freed bitmap ", img, bitmap, mat);Copy the code

The log

Img: width = 564.0, height = 564.0 bitmap: Width = 564.0, height = 564.0, allocationByteCount = 1272384.0 mat: Width = 564.0, height = 564.0 11-19 18:00:35. 347 Script - 58 Main [remote: / / 789216 b9dbb2c184ad7efa6ee6d2c830 / Main js] / D: Img: width = 564.0, height = 564.0 bitmap: Width = 564.0, height = 564.0, allocationByteCount = 0.0 mat: width = 564.0, height = 564.0Copy the code

From the log, we can see that the bitmap size is zero, so img is still available? Let’s test that out with a color command like getting the color of a point

images.pixel(img, 100, 200)
Copy the code

An error code

Wrapped java.lang.IllegalStateException: Can't call getPixel() on a recycled bitmap
Copy the code

Img can’t be used properly, so is img recycled? At least mat still exists, mat can be used normally, so IMG is not recycled,

We use code to determine whether the image is recycled

log(img.isRecycled()); // false
Copy the code

The code says that the image is not recycled, so if you release mat, img is not recycled.

mat.release();
log(img.isRecycled()); // false
Copy the code

False again, img is not marked as reclaimed

Autojs img reclaim method specific code

public synchronized void recycle() { Bitmap bitmap = this.mBitmap; if (bitmap ! = null) { bitmap.recycle(); } this.mBitmap = null; Mat mat = this.mMat; if (mat ! = null) { OpenCVHelper.release(mat); } this.mMat = null; Image image = this.mediaImage; if (image ! = null) { image.close(); } this.mediaImage = null; Debug.INSTANCE.noMemoryLeak(this.id); d<b> dVar = this.ref; if (dVar ! = null) { dVar.b = 0; } this.isRecycled = true; }Copy the code

It still looks a little complicated. What is mediaImage? I don’t even use it, I don’t even use it anyway, just know it’s there

When MAT is recycled, will img and Bitmap be affected

The test code

Let imgPath = files.path("./ img.png "); let img = images.read(imgPath); let bitmap = img.bitmap; let mat = img.mat; PrintInfo (" original image information ", img, bitmap, mat); mat.release(); PrintInfo (" image information after releasing MAT ", img, bitmap, mat);Copy the code

The log

Img: width = 564.0, height = 564.0 bitmap: Width = 564.0, height = 564.0, allocationByteCount = 1272384.0 mat: Width = 564.0, height = 564.0 11-19 18:24:44. 707 Script - 81 Main [remote: / / 789216 b9dbb2c184ad7efa6ee6d2c830 / Main js] / D: Img: width = 564.0, height = 564.0 bitmap: Mat: width = 0.04, height = 0.04, allocationByteCount = 1272384.0Copy the code

It can be seen from the log that MAT has returned to zero and the bitmap has not changed. Can IMG be used now? Let’s test it in code for example to get the color of a point

images.pixel(img, 100, 200); / / - 16777212Copy the code

As for whether other methods can be used, it depends on whether MAT is used or not. If MAT is used, it will automatically report an error if mat has been recycled.

conclusion

Img is used for image recycling. As long as IMG is recycled, bitmap and MAT are also recycled. Unless you use bitmap.copy or mat.clone to copy image data, you will have to recycle the image data yourself

The test environment for the above code

Mi 11 Pro Android version: 11 Autojs version: 9.0.11

Quotes.

Ideas are the most important, other Baidu, Bing, StackOverflow, Github, Android docs, AutoJS docs, and last but not least, ask in the group

The statement

This tutorial is intended for learning purposes only and is not intended for any other use

Wechat official account tooth Uncle tutorial