异步提交图片翻译任务
POST
http://***/submit_task消耗: 1 积分此接口用于异步提交图片翻译任务,接收图片(URL 或 Base64 格式)、源语种、目标语种等参数,返回任务标识(RequestId)。
请求参数
| 名称 | 类型 | 必填 | 描述 |
|---|---|---|---|
| AccountId | string | 是 | 账号ID。 |
| ImageUrl | string | 否 | 原图 URL,与 ImageBase64 二选一,优先使用 ImageBase64。 |
| ImageBase64 | string | 否 | 原图 Base64 编码,与 ImageUrl 二选一。 |
| SourceLanguage | string | 是 | 源语种,请参考"语言支持列表"。 |
| TargetLanguage | string | 是 | 目标语种,请参考"语言支持列表"。 |
| MpProtect | string | 否 | 主图保护, 0: 不开启 ; 1:开启 |
| TranslateMode | string | 否 | 翻译模式, 宽松模式:loose; 严格模式: strict |
| OutputFormat | string | 否 | 输出图片格式,可选参数,例如 png、jpeg、jpg、webp。传入后服务端会以 output_format 字段下发到翻译队列;不传时保持默认输出格式。注:传递此参数必须参与 Signature 签名计算。 |
| Signature | string | 是 | 请求签名,用于验证请求合法性,参见"验签方式"。 |
返回示例
{
"RequestId": "D774D33D-F1CB-5A2C-A787-E0A2179239CE",
"Code": 200,
"Message": "Task submitted successfully"
}OutputFormat 为可选字段,只影响图片翻译任务的输出格式;未传时不影响现有默认流程。
Python 调用示例
# -*- coding: utf-8 -*-
import base64
import hashlib
import hmac
import json
import time
import requests
API_ENDPOINT = "http://***/submit_task"
ACCOUNT_ID = "pic_YOUR_ID"
SECRET_KEY = "YOUR_SECRET_KEY"
def generate_signature(params, secret_key):
sorted_params = sorted(params.items())
param_string = "&".join(
f"{key}={value}"
for key, value in sorted_params
if value is not None and value != "" and key != "Signature"
)
sign_string = f"{param_string}&SecretKey={secret_key}"
signature = hmac.new(
secret_key.encode("utf-8"),
sign_string.encode("utf-8"),
hashlib.sha256
).digest()
return base64.b64encode(signature).decode("utf-8")
def call_api():
params = {
"AccountId": ACCOUNT_ID,
"ImageUrl": "https://example.com/test.jpg",
"SourceLanguage": "zh",
"TargetLanguage": "en",
"OutputFormat": "png",
}
params["Signature"] = generate_signature(params, SECRET_KEY)
response = requests.post(API_ENDPOINT, json=params, timeout=60)
try:
print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except ValueError:
print(response.text)
if __name__ == "__main__":
call_api()Java 调用示例
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
public class PicTechSubmitTaskDemo {
private static final String API_URL = "http://***/submit_task";
private static final String ACCOUNT_ID = "pic_YOUR_ID";
private static final String SECRET_KEY = "YOUR_SECRET_KEY";
public static void main(String[] args) throws Exception {
Map<String, Object> params = new TreeMap<>();
params.put("AccountId", ACCOUNT_ID);
params.put("ImageUrl", "https://example.com/test.jpg");
params.put("SourceLanguage", "zh");
params.put("TargetLanguage", "en");
params.put("OutputFormat", "png");
params.put("Signature", generateSignature(params, SECRET_KEY));
String jsonBody = buildJson(params);
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(API_URL))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonBody))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
private static String generateSignature(Map<String, Object> params, String secretKey) throws Exception {
String paramString = params.entrySet().stream()
.filter(entry -> entry.getValue() != null && !entry.getValue().toString().isEmpty())
.map(entry -> entry.getKey() + "=" + entry.getValue())
.collect(Collectors.joining("&"));
String signString = paramString + "&SecretKey=" + secretKey;
Mac hmacSha256 = Mac.getInstance("HmacSHA256");
hmacSha256.init(new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
return Base64.getEncoder().encodeToString(
hmacSha256.doFinal(signString.getBytes(StandardCharsets.UTF_8))
);
}
private static String buildJson(Map<String, Object> params) {
return "{" + params.entrySet().stream()
.map(entry -> "\"" + entry.getKey() + "\":" + toJsonValue(entry.getValue()))
.collect(Collectors.joining(",")) + "}";
}
private static String toJsonValue(Object value) {
if (value instanceof Number || value instanceof Boolean) {
return value.toString();
}
return "\"" + escapeJson(value.toString()) + "\"";
}
private static String escapeJson(String value) {
return value
.replace("\\", "\\\\")
.replace("\"", "\\\"")
.replace("\n", "\\n")
.replace("\r", "\\r")
.replace("\t", "\\t");
}
}
