# aardio 范例: 腾讯云机器翻译接口：图像翻译



```aardio
//腾讯云机器翻译接口：图像翻译
/*
到 https://console.cloud.tencent.com/tmt 开通机器翻译。
默认关闭后付费（需手动勾选），仅开通免费版不会收任何费用。
图像翻译 - 每月免费 1 万次调用，翻译速度很快。
*/
//参考文本翻译: https://www.aardio.com/zh-cn/docs/examples/Web/REST/tencentCloud/ImageTranslate.html
import console;
import web.rest.tencentCloud;

// 1. 配置腾讯云 API 参数
var tencentConfig = {
	//https://console.cloud.tencent.com/cam/capi 获取 API 密钥
    secretId = "你的SecretId";       // 替换为你的 SecretId
    secretKey = "你的SecretKey";     // 替换为你的 SecretKey
    region = "ap-guangzhou";        // 接口地域
    service = "tmt";                // 机器翻译服务标识
    version = "2018-03-21";         // 接口版本号
    action = "ImageTranslate";      // 接口动作：图片翻译
}

// 2. 创建客户端
var client = web.rest.tencentCloud(tencentConfig);
var tmtApi = client.api("https://tmt.tencentcloudapi.com");

// 3. 准备图片数据
// 读取本地图片文件
var imgBuffer = string.loadBuffer("/test.jpg"); 
if(!imgBuffer){
    console.error("图片读取失败,请检查路径");
    console.pause();
    return;
}

/*
4. 调用接口
参数说明：https://cloud.tencent.com/document/product/551/17232
*/
console.showLoading(" 正在翻译图片 ");

var result, err = tmtApi.post({
    "SessionUuid": "session-" + tostring(time.tick()), // 序列号,随机字符串即可
    "Scene": "doc", // 场景：doc-文档传真, word-普通图片
    "Source": "auto",
    "Target": "zh",
    "ProjectId": 0,
    // Data 参数直接接受 Base64 编码后的图片原始数据
    "Data": crypt.encodeBin(imgBuffer) 
});

// 5. 处理结果
if(result){
    var resp = result[["Response"]];

    if(resp[["Error"]]){
        console.error("接口返回错误：");
        console.dumpJson(resp.Error);
    }
    else {
        console.success("翻译成功！");

        // 遍历翻译结果文本
        var imageRecord = resp[["ImageRecord"]];
        if(imageRecord && imageRecord[["Value"]]){
            for(i, item in imageRecord.Value){
                console.log("--------------------");
                console.log("原文：", item.SourceText);
                console.log("译文：", item.TargetText);
                console.log("坐标：", string.format("x:%d y:%d w:%d h:%d", item.X, item.Y, item.W, item.H));
            }
        }
    }
}
else {
    console.error("请求失败：", err);
}

console.pause();

```

