Java使用腾讯翻译api开发app

本文介绍了如何在Android应用中使用腾讯翻译接口,提供了一个Java示例,展示了如何执行异步任务,获取源语言到目标语言的翻译结果并处理回调。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//这是使用腾讯翻译接口的代码
package com.example.simpleocr;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.tmt.v20180321.TmtClient;
import com.tencentcloudapi.tmt.v20180321.models.*;
import java.util.logging.Logger;
import android.os.AsyncTask;
import java.util.logging.Logger;

public class translate {

    public interface TranslationListener {
        void onTranslationResult(String result);
    }

    public static void otherToZh(String content, String src, TranslationListener listener) {
        TranslateAsyncTask task = new TranslateAsyncTask(content, src, "zh", listener);
        task.execute();
    }

    public static void zhToother(String content, String target, TranslationListener listener) {
        TranslateAsyncTask task = new TranslateAsyncTask(content, "zh", target, listener);
        task.execute();
    }

    private static class TranslateAsyncTask extends AsyncTask<Void, Void, String> {
        private String content;
        private String src;
        private String target;
        private TranslationListener listener;

        public TranslateAsyncTask(String content, String src, String target, TranslationListener listener) {
            this.content = content;
            this.src = src;
            this.target = target;
            this.listener = listener;
        }

        @Override
        protected String doInBackground(Void... voids) {
            try {
                String secretId = "";
                String secretKey = "";
                Credential cred = new Credential(secretId, secretKey);
                HttpProfile httpProfile = new HttpProfile();
                httpProfile.setEndpoint("tmt.tencentcloudapi.com");
                ClientProfile clientProfile = new ClientProfile();
                clientProfile.setHttpProfile(httpProfile);
                TmtClient client = new TmtClient(cred, "ap-shanghai", clientProfile);
                TextTranslateRequest req = new TextTranslateRequest();
                req.setSourceText(content);
                req.setSource(src);
                req.setTarget(target);
                req.setProjectId(0L);
                TextTranslateResponse resp = client.TextTranslate(req);
                return resp.getTargetText();
            } catch (TencentCloudSDKException e) {
                Logger log = Logger.getLogger(translate.class.getName());
                log.info("腾讯云接口请求错误,错误信息:" + e.getMessage());
                return "腾讯云接口请求错误";
            }
        }

        @Override
        protected void onPostExecute(String result) {
            if (listener != null) {
                listener.onTranslationResult(result);
            }
        }
    }
}

调用:

translate.otherToZh(ocrText, src, new translate.TranslationListener() {
            @Override
            public void onTranslationResult(String result) {
                // 处理翻译结果,例如更新 UI
                //String combinedText = ocrText+"\n\n  翻译结果: \n\n " + result;
                textView.setText(ocrText+"\n\n  翻译结果: \n\n " + result);
            }
        });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值