Face age change of autojs

Keywords: node.js autojs

The dental tutorial is easy to understand

Effect Display

origin

2021/10/04, autojs is upgraded to 9.0.9 today, and its own nodejs can basically be used, so test it with Tencent Cloud's face transformation sdk

Environmental Science

Mobile phone: Mi 11 Pro

Android version: 11

Autojs version: 9.0.9

thinking

  1. autojs is responsible for interface display
  2. nodejs is responsible for accessing SDK

Catalog

Install module

npm i  --no-bin-links

You can't find the location of this picture above

  1. Open any js file
  2. There is a file in the upper left corner, click it
  3. Click on the three points on the right side of your project folder
  4. Click npm
  5. Click on another npm command or package manager
  6. Enter command npm i --no-bin-links

You will learn the following points

  • autojs and nodejs communication
  • nodejs calls the SDK of the platform

Code Explanation

1.UI interface, just a picture, a drag bar, the 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="Age: " textSize="50sp"></text>
      <text id="age" textSize="50sp">
        30
      </text>
    </horizontal>
  </vertical>
);
2.Run the nodejs script using the Rhino engine, and once you have the id, you can transfer something from the nodejs to the autojs
const execution = $engines.execScriptFile("./node Handling Faces.node.js", {
  arguments: {
    serverEngineId: $engines.myEngine().id,
  },
});
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);
  });
});
4.Set the drag bar to listen for events, and when the value changes, send a message to the node, and when the result is returned, modify the picture
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: "Switch Age",
          args: {
            imgPath: imgPath,
            age: parseInt(age),
          },
        });
      },
    })
  );
}, 1000);
5.When the script ends, the node script also ends
events.on("exit", function () {
  execution.engine.forceStop();
});
6.Nodejs main code
// Remove the Rhino engine ID from the parameter
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 "Switch Age":
      Switch Age(command.args);
      break;
  }
});

// Send http requests based on url parameters and reply to Rhino engine
async function Switch Age(args) {
  let age = args.age;
  let imgPath = args.imgPath;
  const res = await getNewFace(age, imgPath);
  serverEngine.emit("reply", {
    actionName: "Switch Age Results",
    result: res,
  });
}

Matters needing attention

  • When autojs and nodejs are mixed, you cannot have project.json
  • Don't expose your own secret key
  • There are two node modules used: tencentcloud-sdk-nodejs, mkdirp
  • js is inherently asynchronous
  • Communication between autojs and nodejs is the focus

activity

Official channels to receive 7 days of iQIYI vip membership free of charge, true and effective!!!

Reference resources

Face Age Change - Official Documentation

activity

Official channels to receive 7 days of iQIYI vip membership free of charge, true and effective!!!

Celebrity sayings

Ideas are the most important, other Baidu, bing, stackoverflow, Android, autojs documents, and finally group questions
- Uncle Dental Tutorial

statement

Some content from the network
This tutorial is for learning only, not for other purposes

bilibili

Uncle Dental Tutorial

WeChat Public Dental Tutorial

QQ Group

747748653

Full Source

Posted by Hopps on Mon, 04 Oct 2021 10:52:59 -0700