Tooth uncle tutorial is easy to understand

Results show

Scarlett Johansson

The American flag

Image fusion

The environment

Android version: 11

Autojs version: 9.0.8

Train of thought

  1. To make the gradient transparent, the image should be in PNG format
  2. Image processing is usually done with OpencV
  3. The data format of the picture is generally MAT, in which the value of the channel corresponding to the transparency is modified
  4. Image fusion uses Canvas to draw directly

Here’s what you’ll learn

  • Get picture type
  • Converting image types
  • Mat turn images
  • Mat turn bitmap
  • Canvas painting
  • Image normalization, that is, making two images the same size
  • Print mat properties
  • Modifies the value of the image transparent channel
  • Byte arrays are converted to hexadecimal strings

The code on

1. Path of two pictures
let imgPath = files.path("./ Flag of the United States. PNG");
let imgPath2 = files.path("./ Scarlett Johansson. JPG");
Copy the code
2. Initialize OpencV
runtime.images.initOpenCvIfNeeded();
Copy the code
3. The imported classes
importClass(org.opencv.core.MatOfByte);
importClass(org.opencv.core.Scalar);
importClass(org.opencv.core.Point);
importClass(org.opencv.core.CvType);
importClass(java.util.List);
importClass(java.util.ArrayList);
importClass(java.util.LinkedList);
importClass(org.opencv.imgproc.Imgproc);
importClass(org.opencv.imgcodecs.Imgcodecs);
importClass(org.opencv.core.Core);
importClass(org.opencv.core.Mat);
importClass(org.opencv.core.MatOfDMatch);
importClass(org.opencv.core.MatOfKeyPoint);
importClass(org.opencv.core.MatOfRect);
importClass(org.opencv.core.Size);
importClass(org.opencv.features2d.DescriptorMatcher);
importClass(org.opencv.features2d.Features2d);
importClass(org.opencv.core.MatOfPoint2f);
importClass(org.opencv.android.Utils);
importClass(android.graphics.Bitmap);
importClass(java.lang.StringBuilder);
importClass(java.io.FileInputStream);
importClass(java.io.File);
Copy the code
4. Get the image type
function getPicType(fis) {
  // Read the first few bytes of the file to determine the image format
  // let b = new byte[4]();
  let b = util.java.array("byte".4);
  try {
    fis.read(b, 0, b.length);
    let type = bytesToHexString(b).toUpperCase();
    type = new java.lang.String(type);
    if (type.contains("FFD8FF")) {
      return TYPE_JPG;
    } else if (type.contains("89504E47")) {
      return TYPE_PNG;
    } else if (type.contains("47494638")) {
      return TYPE_GIF;
    } else if (type.contains("424D")) {
      return TYPE_BMP;
    } else {
      returnTYPE_UNKNOWN; }}catch (e) {
    log(e);
  } finally {
    if(fis ! =null) {
      try {
        fis.close();
      } catch(e) { log(e); }}}return null;
}
Copy the code
5. Normalize the picture so that its width and height are consistent. Although it is processed here, it is best to process the picture in advance to avoid deformation
function normalize(img, img2) {
  let imgWidth = img.getWidth(); / / 200
  let imgHeight = img.getHeight(); / / 200
  return images.resize(img2, [imgWidth, imgHeight]);
}
Copy the code
6. Modify the transparent channel of the PNG image. Here, make the background display two-thirds wide and transparent gradient
function transparentGradient(mat) {
  let width = mat.width();
  let height = mat.height();
  let unit = 256 / ((width / 3) * 2);
  let wLimit = (width / 3) * 2;
  for (var i = 0; i < height; i++) {
    for (var j = 0; j < width; j++) {
      let item = mat.get(i, j);
      if (j > wLimit) {
        item[3] = 0;
      } else {
        item[3] = 255- unit * j; } mat.put(i, j, item); }}return mat;
}
Copy the code
7. Color space conversion, because AJ display RGB color is normal, BGR display color is not normal
Imgproc.cvtColor(mat, mat, Imgproc.COLOR_BGRA2RGBA);
Copy the code
8. Canvas can be used to merge two pictures into one, or the correct color after image fusion can be directly calculated, and then a new MAT can be created. There are more than one ways to merge pictures
function merge(mat, mat3) {
  let tempFilePath = saveMat(mat);
  let w = mat.width();
  let h = mat.height();
  let bitmap3 = mat2bitmap(mat3);
  let bitmap = android.graphics.Bitmap.createBitmap(w, h, android.graphics.Bitmap.Config.ARGB_8888);
  var tempImg = images.read(tempFilePath);
  bitmap2 = tempImg.getBitmap();
  let canvas = new Canvas(bitmap);
  let paint = new Paint();
  canvas.drawBitmap(bitmap3, 0.0, paint);
  canvas.drawBitmap(bitmap2, 0.0, paint);
  var image = canvas.toImage();
  tempImg.recycle();
  return image;
}
Copy the code
9. View the fused image
let tempDir = files.join("/sdcard/Pictures/img");
let tempFilePath2 = files.join(tempDir, files.getName(oImgPath2));
files.createWithDirs(tempFilePath2);
images.save(img4, tempFilePath2);
app.viewFile(tempFilePath2);
Copy the code
10. Free up resources
img.recycle();
img2.recycle();
img3.recycle();
img4.recycle();
mat.release();
mat3.release();
Copy the code

reference

The Java code determines the image file format, not the file suffix.

Quotes.

Thinking is the most important, other Baidu, Bing, StackOverflow, Android documents, autoJS documents, and finally in the group to ask — fang Shu tutorial

The statement

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