Tooth uncle tutorial is easy to understand

Results show

origin

I saw on a video site that every other language has picture conversion characters, so AUTOJS should have them too, so I made this tutorial

The environment

Autojs version: 9.0.4 Android version: 8.0.0

Train of thought

Here’s what you’ll learn

  • Initialize the opencv
  • Opencv reads the video
  • Gets the width and height of a video frame
  • OpencvRGB image to grayscale image
  • Opencv resizes images
  • Grayscale mapping character
  • Creating a Mat instance
  • Release opencV resources

The code on

1. UI interface
ui.layout(
  <vertical>
    <text id="content" textSize="5sp" typeface="monospace"></text>
  </vertical>
);
Copy the code
2. Initialize OpencV
runtime.images.initOpenCvIfNeeded();
Copy the code
3. The imported classes
importClass(org.opencv.core.CvType);
importClass(org.opencv.core.Scalar);
importClass(org.opencv.core.Point);
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(android.graphics.Matrix);
importClass(org.opencv.android.Utils);
importClass(android.graphics.Bitmap);
importClass(org.opencv.videoio.VideoCapture);
importClass(org.opencv.videoio.Videoio);
Copy the code
4. Check whether the video file exists
let filePath = "/sdcard/huaQiangMaiGua.mp4";
log(files.exists(filePath));
if (files.exists(filePath)) {
  throw new Error("Video file does not exist," + filePath);
}
Copy the code
5. Read the video
let cap = VideoCapture(filePath);
log(cap);
log(cap.isOpened());
Copy the code
6. Obtain the video width and height
let frame = new Mat();
cap.read(frame);
let h = frame.rows();
let w = frame.cols();
log("w = " + w + ", h = " + h);
Copy the code
7. Turn the picture to gray scale
let imgGray = frame.clone();
Imgproc.cvtColor(frame, imgGray, Imgproc.COLOR_BGR2GRAY);
Copy the code
8. Loop until you finish reading the video
while (r) {
  sleep(10);
  Imgproc.cvtColor(frame, imgGray, Imgproc.COLOR_BGR2GRAY);
  Imgproc.resize(imgGray, imgResize, size, 0.0, Imgproc.INTER_AREA);
  let content = mat2Str(imgResize, imgCharWidth, imgCharHeight);
  ui.post(function () {
    ui.content.setText(content);
  });
  r = cap.read(frame);
}
Copy the code
9. Release resources
events.on("exit".function () {
  // Release resources
  cap.release();
  frame.release();
  imgGray.release();
  imgResize.release();
  imgAdaptiveThreshold.release();
});
Copy the code

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