Tooth uncle tutorial is easy to understand

Results show

origin

2021/10/04, today autoJS has been upgraded to 9.0.9, and its own NodeJS can be used basically. Let’s test it with Tencent Cloud’s face transformation SDK

The environment

Phone: Mi 11 Pro

Android version: 11

Autojs version: 9.0.9

Train of thought

  1. Autojs is responsible for the interface presentation
  2. Nodejs is responsible for accessing the SDK

directory

Install the module

npm i  --no-bin-links
Copy the code

You can’t locate the picture above

  1. Open any JS file
  2. There’s a file in the upper left corner, click on it
  3. Click on the three dots to the right of your project folder
  4. Click on the NPM
  5. Click another NPM command or package manager
  6. Enter the command NPM I –no-bin-links

Here’s what you’ll learn

  • Autojs communicates with NodeJS
  • Nodejs calls the platform SDK

The code on

1. UI interface, just a picture, a drag bar, drag bar is responsible for changing the age
ui.layout(
  <vertical>
    <img id="img" src="file://{{imgPath}}"></img>
    <seekbar id="seekbar" progress="30" w="*" margin="18" />
    <horizontal w="*" gravity="center">
      <text text="年龄: " textSize="50sp"></text>
      <text id="age" textSize="50sp">
        30
      </text>
    </horizontal>
  </vertical>
);
Copy the code
2. Run the NodeJS script using the Rhino engine, and once you have the ID, you can upload things from nodeJS to AutoJS
const execution = $engines.execScriptFile("./node handles face.node.js", {
  arguments: {
    serverEngineId: $engines.myEngine().id,
  },
});
Copy the code
3. Listen for messages from the Node.js engine
$events.on("reply".(result) = > {
  console.log(result);
  http.get(result.result.ResultUrl, {}, function (res, err) {
    if (err) {
      console.error(err);
      return;
    }
    log("code = " + res.statusCode);
    let tempPath = files.join(files.getSdcardPath(), "Script"."temp.jpg");
    files.writeBytes(tempPath, res.body.bytes());

    ui.img.attr("src"."file://" + tempPath);
  });
});
Copy the code
4. Set the drag bar to listen to events. If the value changes, a message will be sent to Node
setTimeout(function () {
  ui.seekbar.setOnSeekBarChangeListener(
    new android.widget.SeekBar.OnSeekBarChangeListener({
      onStopTrackingTouch: function (seekbar) {
        let age = seekbar.getProgress();
        toastLog(age);
        ui.age.setText(age + "");
        // Send a message to the Node.js engine
        execution.engine.emit("command", {
          name: "Switching age".args: {
            imgPath: imgPath,
            age: parseInt(age), }, }); }})); },1000);
Copy the code
5. When the script ends, the Node script is also terminated
events.on("exit".function () {
  execution.engine.forceStop();
});
Copy the code
6. Nodejs main code
// Retrieve the Rhino engine ID from the argument
const serverEngineId = engines.myEngine().execArgv.serverEngineId;
// Find the Rhino engine by ID
const serverEngine = engines.getRunningEngines().find((e) = > e.id === serverEngineId);

$autojs.keepRunning();

// Listen for command messages
engines.myEngine().on("command".(command) = > {
  switch (command.name) {
    case "Switching age": Toggle age (command-args);break; }});// Send an HTTP request to the Rhino engine based on the URL parameters
async functionSwitching age (args) {
  let age = args.age;
  let imgPath = args.imgPath;
  const res = await getNewFace(age, imgPath);
  serverEngine.emit("reply", {
    actionName: "Age switching results".result: res,
  });
}
Copy the code

Matters needing attention

  • When using autojs and nodeJS, you cannot have project.json
  • Don’t reveal your secret key
  • There are two node modules used: tencentCloud – sdK-nodejs, mkdirp
  • Js is asynchronous by nature
  • Autojs and NodeJS communication is the focus

reference

Face age changes – official documentation

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