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
- autojs is responsible for interface display
- nodejs is responsible for accessing SDK
Catalog
Install module
npm i --no-bin-links
You can't find the location of this picture above
- Open any js file
- There is a file in the upper left corner, click it
- Click on the three points on the right side of your project folder
- Click npm
- Click on another npm command or package manager
- 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
WeChat Public Dental Tutorial
QQ Group
747748653