帮助与文档 > 产品文档 > 自然语言翻译 > API文档 > 图片翻译服务
图片翻译服务

有道智云翻译 API 简介

有道智云图片翻译API接口提供有道的图片文字识别翻译服务。您只需要通过调用有道智云图片翻译API,传入经过Base64编码的图片,指定源语言与目标语言,通过POST请求方式,就可以识别图片中的文字并进行翻译。

有道智云图片翻译 API HTTP地址:

http://openapi.youdao.com/ocrtransapi

有道智图片翻译 API HTTPS地址:

https://openapi.youdao.com/ocrtransapi

接口调用参数

调用API需要向接口发送以下字段来访问服务。

字段名含义必填举例
type文件上传类型,目前支持base64True1
from源语言,支持的语言参见语言列表Trueauto
to目标语言,支持的语言参见语言列表Trueen
appKey创建api类型的应用获得的应用IDTruetestKey
salt随机值,建议北京当前时间戳Truewhatever
sign签名,sign由客户端算好后传递,具体生成规则,见附注Truewhatever
ext翻译结果音频格式,支持mp3falsemp3
q要识别的图片,type为1时必填,图片的base64编码true在type为1的情况下必填
docType服务器响应类型,目前只支持jsonfalsejson

签名生成方法如下:

1、将请求参数中的 appKey(应用ID), (注意为图片的Base64编码), 随机数 salt应用密钥 按照 appKey+q+salt+应用密钥 的顺序拼接得到字符串 str

2、对字符串 str 做 md5,得到32位大写的 sign (参考Java生成MD5示例,可点击右侧的JAVA示例)。

注意:

  1. 请先将需要识别的图片转换为 Base64 编码。
  2. 在发送HTTP请求之前需要对各字段做 URL encode。
  3. 在生成签名拼接 appKey+q+salt+密钥 字符串时,q 不需要做 URL encode,在生成签名之后,发送 HTTP 请求之前才需要对要发送的 q 做URL encode。
  4. 签名和请求有疑问,可以参考文档最后的常用语言demo。

输出结果

返回的结果是json格式,具体说明如下:

字段名字段说明
orientation图片所对应的方向
lanFromocr所识别出来认为的图片中的语言
textAngle图片的倾斜角度
errorCode错误码
lanTo目标语言
resRegions图片翻译的具体内容
-boundingBox区域范围,四个值:左上角的x值,左上角的y值,区域的的宽,区域的高,例如:134,0,1066,249
-linesCount行数(用于前端排版)
-lineheight行高
-context该区域的原文
-linespace行间距
-tranContent翻译结果

注:resRegions是一个json列表,表示图中多个区域对应的翻译结果,每个json中有6个参数:boundingBox、linesCount、lineheight、context、linespace、tranContent。

示例

{
    "orientation": "Up",
    "lanFrom": "en",
    "textAngle": "0.0",
    "errorCode": "0",
    "lanTo": "zh-CHS",
    "resRegions": [{
        "boundingBox": "134,0,1066,249",
        "linesCount": 7,
        "lineheight": 28,
        "context": " There are two ways to cope with the problem in government. One (dis cussed in the preceding chapter) is to supply the service or product in a marketlike environment. Shift the burden of evaluation off the shoulders of professional evaluators and onto the shoulders of clients and customers, and let the latter vote with their feet. The \"client\" in these cases can be individual citizens or government agencies; what is important is that the client be able to choose from among rival suppliers.",
        "linespace": 7,
        "tranContent": "有两种方法可以解决政府的问题。一个(在前一章中讨论的)是在一个市场环境中提供服务或产品。将评估的负担从专业评估人员的肩膀上转移到客户和客户的肩上,让后者用脚投票。这些案件的“委托人”可以是公民个人或政府机构;重要的是客户可以从竞争对手那里选择。"
        }, {
        "boundingBox": "116,253,1100,354",
        "linesCount": 10,
        "lineheight": 28,
        "context": " But some public services cannot be supplied, or arc never going to be sup plied, by a market. We can imagine allowing parents to choose among schools but we cannot imagine letting them choose (at least for most purposes) among police departments or armies. In that case one should adopt the second way of evaluating a public service: carry out a demonstration project or conduct a field experiment. (I will use the two ideas interchangeably, though some schol ars distinguish between them.7) An experiment is a planned alteration in a state of affairs designed to measure the effect of the intervention. It involves asking the question, \"If I change X, what will happen to Y, having first made certain that everything else stays the same?\" It sounds easy, but it is not.",
        "linespace": 5,
        "tranContent": "但是一些公共服务不能提供,或者不能被市场所垄断。我们可以想象允许父母在学校里选择,但我们无法想象让他们在警察部门或军队中选择(至少是在大多数情况下)。在这种情况下,应该采用第二种评价公共服务的方法:进行示范项目或进行现场试验。(我将把这两种观点交替使用,尽管有些学校将两者区分开来。)实验是一种计划中的改变,旨在衡量干预的效果。它涉及到问这个问题,“如果我改变X, Y会发生什么,首先确定其他一切都保持不变?”这听起来很容易,但事实并非如此。"
        }, {
        "boundingBox": "101,609,1141,152",
        "linesCount": 4,
        "lineheight": 28,
        "context": " A good experiment (bad ones are worse than no experiment at all) requires that one do the following: First, identify a course of action to be test ed; call it the treatment. A \"treatment\" can be a police tactic, a school cur riculum, or a welfare program. Second, decide what impact the treatment is",
        "linespace": 5,
        "tranContent": "一个好的实验(坏的实验比没有实验更糟糕)需要一个人做以下的事情:首先,确定要测试的行动过程;叫它治疗。“治疗”可以是警察的策略,也可以是学校里的,或者是福利项目。其次,确定治疗的影响。"
    }]
}

支持的语言表

语言支持目标语言
中文英文、日文、韩文、法文、西班牙文、葡萄牙文、俄文、越南文、德文、阿拉伯文、印尼文
英文中文
日文中文
韩文中文
法文中文
西班牙文中文
葡萄牙文中文
意大利文中文
俄文中文
越南文中文
德文中文
阿拉伯文中文
印尼文中文
自动识别中文

其中,自动识别支持以上所有语言的自动识别

下表为各语言对应代码:

语言代码
中文zh-CHS
英文en
日文ja
韩文ko
法文fr
西班牙文es
葡萄牙文pt
意大利文it
俄文ru
越南文vi
德文de
阿拉伯文ar
印尼文id
自动识别auto

错误代码列表

错误码含义
101缺少必填的参数,出现这个情况还可能是et的值和实际加密方式不对应
102不支持的语言类型
103翻译文本过长
104不支持的API类型
105不支持的签名类型
106不支持的响应类型
107不支持的传输加密类型
108appKey无效,注册账号, 登录后台创建应用和实例并完成绑定, 可获得应用ID和密钥等信息,其中应用ID就是appKey( 注意不是应用密钥)
109batchLog格式不正确
110无相关服务的有效实例
111开发者账号无效
113q不能为空
201解密失败,可能为DES,BASE64,URLDecode的错误
202签名检验失败
203访问IP地址不在可访问IP列表
205创建的应用(Android、iOS、Web)与调用接口不一致
206因为时间戳无效导致签名校验失败
301辞典查询失败
302翻译查询失败
303服务端的其它异常
401账户已经欠费停
411访问频率受限,请稍后访问
412大图片请求过于频繁,请稍后访问
5001无效的OCR类型
5002不支持的OCR image类型
5003不支持的语言类型
5004识别图片过大
5201图片base64解密失败
5301OCR段落识别失败
5411访问频率受限
5412超过最大识别流量

常见问题及注意事项

  • 返回110

应用没有绑定服务实例,可以新建服务实例,绑定服务实例。

  • 返回108

appKey无效,注册账号, 登录后台创建应用和实例并完成绑定, 可获得应用ID和密钥等信息,其中应用ID就是appKey( 注意不是应用密钥)

  • 返回101

首先确保必填参数齐全,然后,确认参数书写是否正确。

  • 返回202

如果确认 appKeyappSecret 的正确性,仍返回202,一般是编码问题。请确保 q 为UTF-8编码.

  • 返回205

确保接入方式(Android SDK、IOS SDK、API)与创建的应用平台类型一致。

常用语言 Demo

Java 示例

import net.coobird.thumbnailator.Thumbnails;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
*
* 图片翻译Api Demo
* 1.构建参数
* 2.请求api接口
* 3.处理结果
*/
public class TransOCRApiDemo1 {

    private static Logger logger = LoggerFactory.getLogger(TransOCRApiDemo1.class);

    public static void main(String[] args) throws IOException {

        String appKey = "您的应用ID";
        String appSecret = "您的应用密钥";
        String filePath = "您的图片地址";
        String tempFilePath = "压缩图片临时地址";
        ocrtrans(appKey,appSecret,filePath,tempFilePath);
    }

    /**
    *
    * @param appKey 应用ID
    * @param appSecret 应用密钥
    * @param filePath 图片路径
    * @param tmpFilePath 压缩后文件临时保存路径
    */
    public static void ocrtrans(String appKey,String appSecret,String filePath,String tmpFilePath) throws IOException {
        /** 图片翻译接口地址 */
        String url = "http://openapi.youdao.com/ocrtransapi";

        /** 构建参数 */
        Map<String,String> params = new HashMap<String,String>();

        File file = new File(filePath);
        if(!file.exists()){
            logger.error("文件不存在");
            return;
        }
        /** 压缩图片 */
        long maxSize = 1 * 1024 * 1024;
        float quality = 0.7f;
        if(file.length() > maxSize){
            /** 设置图片大小和质量 */
            Thumbnails.of(filePath).scale(1f).outputQuality(quality).toFile(new File(tmpFilePath));
            File tmpFile = new File(tmpFilePath);
            filePath = tmpFilePath;
            /** 连续压缩 */
            while(tmpFile.length() > maxSize){
                quality -= 0.2;
                Thumbnails.of(filePath).scale(1f).outputQuality(quality).toFile(tmpFile);
                tmpFile = new File(tmpFilePath);
            }
        }
        System.out.println(file.length());

        String salt = String.valueOf(System.currentTimeMillis());
        String from = "auto";
        String to = "zh-CHS";
        String type = "1";
        String sign = null;
        params.put("appKey",appKey);
        params.put("salt",salt);
        params.put("from",from);
        params.put("to",to);
        params.put("type",type);


        /** 请求图片翻译 */
        File imgFile = new File(filePath);
        String result = null;
        String q = getBase64OfFile(imgFile);
        params.put("q", q);
        sign = md5(appKey + q + salt +appSecret);
        params.put("sign",sign);
        result = requestForHttp(url,params);

        /** 处理结果 */
        System.out.println(result);
    }

    public static String requestForHttp(String url,Map<String,String> params) throws IOException {
        String result = "";

        /** 创建HttpClient */
        CloseableHttpClient httpClient = HttpClients.createDefault();

        /** httpPost */
        HttpPost httpPost = new HttpPost(url);
        List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
        Iterator<Map.Entry<String,String>> it = params.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry<String,String> en = it.next();
            String key = en.getKey();
            String value = en.getValue();
            paramsList.add(new BasicNameValuePair(key,value));
        }
        httpPost.setEntity(new UrlEncodedFormEntity(paramsList,"UTF-8"));
        CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
        try{
            HttpEntity httpEntity = httpResponse.getEntity();
            result = EntityUtils.toString(httpEntity,"UTF-8");
            EntityUtils.consume(httpEntity);
        }finally {
            try{
                if(httpResponse!=null){
                    httpResponse.close();
                }
            }catch(IOException e){
                logger.info("## release resouce error ##" + e);
            }
        }
        return result;
    }


    /**
    * 生成32位MD5摘要
    * @param string
    * @return
    */
    public static String md5(String string) {
        if(string == null){
            return null;
        }
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        'A', 'B', 'C', 'D', 'E', 'F'};
        byte[] btInput = string.getBytes();
        try{
            /** 获得MD5摘要算法的 MessageDigest 对象 */
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            /** 使用指定的字节更新摘要 */
            mdInst.update(btInput);
            /** 获得密文 */
            byte[] md = mdInst.digest();
            /** 把密文转换成十六进制的字符串形式 */
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (byte byte0 : md) {
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        }catch(NoSuchAlgorithmException e){
            return null;
        }
    }

    public static String getBase64OfFile(File file){
        byte[] data = null;
        InputStream in = null;
        try{
            in = new BufferedInputStream(new FileInputStream(file));
            data = new byte[in.available()];
            in.read(data);

        }catch (Exception e){
            e.printStackTrace();
        }
        return com.sun.org.apache.xml.internal.security.utils.Base64.encode(data);
    }
}

Python 示例

#/usr/bin/env python
#coding=utf8

import httplib
import md5
import urllib
import urllib2
import random
import json
import base64

# 您的应用ID
appKey = "您的应用ID"
# 您的应用密钥,请勿把它和appKey泄露给他人
appSecret = "您的应用密钥"


httpClient = None

try:

    # 参数部分
    f=open(r'D:/test111.jpg','rb') #二进制方式打开图文件
    q=base64.b64encode(f.read()) #读取文件内容,转换为base64编码
    f.close()
    # 源语言
    fromLan = "en"
    # 目标语言
    to = "zh-CHS"
    # 上传类型
    type = "1"
    # 随机数,自己随机生成,建议时间戳
    salt = random.randint(1, 65536)
    # 签名
    sign = appKey+q+str(salt)+appSecret
    m1 = md5.new()
    m1.update(sign)
    sign = m1.hexdigest()
    data = {'appKey':appKey,'q':q,'from':fromLan,'to':to,'type':type,'salt':str(salt),'sign':sign}
    data = urllib.urlencode(data)
    req = urllib2.Request('http://openapi.youdao.com/ocrtransapi',data)

    #response是HTTPResponse对象
    response = urllib2.urlopen(req)
    print response.read()
except Exception, e:
    print e
finally:
    if httpClient:
        httpClient.close()

C#示例

using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace ConsoleApplication2
{
    using System;
    using System.Collections.Specialized;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Web;
    using System.Security.Cryptography;
    using System.Drawing;

    class Test
    {

        public static void Main() {

            String url = "http://openapi.youdao.com/ocrtransapi";
            Dictionary dic = new Dictionary();

            string img = ImgToBase64String("d:/1.png");
            string appKey = "your appKey";
            string detectType = "10012";
            string langType = "en";
            String imageType = "1";
            string salt = DateTime.Now.Millisecond.ToString();
            string appSecret = "your appSecret";
            MD5 md5 = new MD5CryptoServiceProvider();
            string md5Str = appKey + img + salt + appSecret;
            byte[] output = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(md5Str));
            string sign = BitConverter.ToString(output).Replace("-", "");
            dic.Add("img", System.Web.HttpUtility.UrlEncode(img));
            dic.Add("appKey", appKey);
            dic.Add("langType", langType);
            dic.Add("detectType", detectType);
            dic.Add("imageType", imageType);
            dic.Add("salt", salt);
            dic.Add("sign", sign);
            string result = Post(url, dic);
            Console.WriteLine(result);

        }

        protected static string ImgToBase64String(string Imagefilename)
        {
            try
            {
                System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Imagefilename);

                MemoryStream ms = new MemoryStream();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                return Convert.ToBase64String(arr);
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public static string Post(string url,Dictionary dic)
        {
            string result = "";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            #region 添加Post 参数
            StringBuilder builder = new StringBuilder();
            int i = 0;
            foreach (var item in dic)
            {
                if (i > 0) {
                    builder.Append("&");
                }
                builder.AppendFormat("{0}={1}", item.Key, item.Value);
                i++;
            }
            Console.WriteLine(builder.ToString());
            byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            #endregion
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取响应内容
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }


    }
}

PHP 示例

<?php
define("CURL_TIMEOUT",   2000);
define("URL",            "http://openapi.youdao.com/ocrtransapi");
define("APP_KEY",         "您的应用ID"); //替换为您的应用ID
define("SEC_KEY",        "您的应用密钥");//替换为您的密钥

function ocrtrans($q, $type, $from, $to)
{
                    $args = array(
        'q' => $q,
        'appKey' => APP_KEY,
        'salt' => rand(10000,99999),
        'type' => $type,
        'from' => $from,
        'to' => $to
    );
                    $args['sign'] = buildSign(APP_KEY, $q, $args['salt'], SEC_KEY);
                    $ret = call(URL, $args);
    echo $ret;
                    $ret = json_decode($ret, true);
    return $ret;
}

//加密
function buildSign($appKey, $query, $salt, $secKey)
{/*{{{*/
                    $str = $appKey . $query . $salt . $secKey;
                    $ret = md5($str);
    return $ret;
}/*}}}*/

//发起网络请求
function call($url, $args=null, $method="post", $testflag = 0, $timeout = CURL_TIMEOUT, $headers=array())
{/*{{{*/
                    $ret = false;
                    $i = 0;
    while($ret === false)
    {
        if($i > 1)
         break;
        if($i > 0)
        {
            sleep(1);
        }
                    $ret = callOnce($url, $args, $method, false, $timeout, $headers);
                    $i++;
    }
    return $ret;
}/*}}}*/

function callOnce($url, $args=null, $method="post", $withCookie = false, $timeout = CURL_TIMEOUT, $headers=array())
{/*{{{*/
                    $ch = curl_init();
    if($method == "post")
    {
                    $data = convert($args);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_POST, 1);
    }
    else
    {
                    $data = convert($args);
        if($data)
        {
            if(stripos($url, "?") > 0)
            {
                    $url .= "&$data";
            }
            else
            {
                    $url .= "?$data";
            }
        }
    }
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if(!empty($headers))
    {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }
    if($withCookie)
    {
        curl_setopt($ch, CURLOPT_COOKIEJAR, $_COOKIE);
    }
                    $r = curl_exec($ch);
    curl_close($ch);
    return $r;
}/*}}}*/

function convert(&$args)
{/*{{{*/
                    $data = '';
    if (is_array($args))
    {
        foreach ($args as $key=>$val)
        {
            if (is_array($val))
            {
                foreach ($val as $k=>$v)
                {
                    $data .= $key.'['.$k.']='.rawurlencode($v).'&';
                }
            }
            else
            {
                    $data .="$key=".rawurlencode($val)."&";
            }
        }
        return trim($data, "&");
    }
    return $args;
}/*}}}*/
                    $file="d:/test111.jpg";
                    $fp=fopen($file,"r") or die("Can't open file");
                    $q=chunk_split(base64_encode(fread($fp,filesize($file))));//base64编码
fclose($fp);
//调用asr
ocrtrans($q,"1","en","zh-CHS");
?>