Signed-off-by: lilium-saber <WR364999428@hotmail.com>
GDN
lilium-saber 1 year ago
parent b00e0d0f24
commit 47f39ae49e

@ -12,96 +12,179 @@ namespace testRjgc
{
internal class AllClassAndInterfaceWellBeManipulatedInThisFileAndCallThroughFunction
{
AllClassAndInterfaceWellBeManipulatedInThisFileAndCallThroughFunction() { }
public AllClassAndInterfaceWellBeManipulatedInThisFileAndCallThroughFunction() { }
TranslationPrivateIn trin = new TranslationPrivateIn();
TranslationApiStr trstr = new translationPrivateOut();
TranslationApiInt trint = new translationPublic();
TranslationScnToJson scnjson = new TranslationScnToJson();
Sofia sof = new OcrSofia();
Messiah jsonnet = new jsonNet();
public void InputPrivateBaidu(string name, string id, string key)
{
TranslationPrivateIn tr = new TranslationPrivateIn();
tr.InputBaiduPrivate(name, id, key);
trin.InputBaiduPrivate(name, id, key);
}
public void InPutPrivateA(string name, List<string> list)
{
TranslationPrivateIn tr = new TranslationPrivateIn();
tr.InPutPrivate(name, list);
trin.InPutPrivate(name, list);
}
public List<string> OutputNameList()
{
TranslationApiStr tr = new translationPrivateOut();
List<string> list = tr.OutputTranslationNameList();
List<string> list = trstr.OutputTranslationNameList();
return list;
}
public List<string> OutputStr(string id)
{
TranslationApiStr str = new translationPrivateOut();
List<string> li = str.OutputTranslationS(id);
List<string> li = trstr.OutputTranslationS(id);
return li;
}
public List<string> OutputChoicPublic(int id)
{
TranslationApiInt sin = new translationPublic();
List<string> li = sin.OutputTranslationI(id);
List<string> li = trint.OutputTranslationI(id);
return li;
}
public void StartSCNTransJSON()
{
TranslationScnToJson scn = new TranslationScnToJson();
scn.StartTrans();
scnjson.StartTrans();
}
public void StartTransOneself(string spa, string dps)
{
TranslationScnToJson scn = new TranslationScnToJson();
scn.StopTrans(spa, dps);
scnjson.StopTrans(spa, dps);
}
public string WindowsOcrToGetString(string name)
{
Sofia so = new OcrSofia();
string temp = so.WindowsOcrToGetStringAndTranslate(name);
return temp;
return sof.WindowsOcrToGetStringAndTranslate(name);
}
public string NetBaiDu(string input)
{
Messiah me = new jsonNet();
return me.RequestJsonBaiDu(input);
return jsonnet.RequestJsonBaiDu(input);
}
public string NetAllBaiDu(string input, string srcl, string dstl, string aid, string kkey)
{
Messiah me = new jsonNet();
return me.RequestJsonPrivateBaiDu(input, srcl, dstl, aid, kkey);
return jsonnet.RequestJsonPrivateBaiDu(input, srcl, dstl, aid, kkey);
}
public string NetLanBaiDu(string input, string srcl, string dstl)
{
Messiah me = new jsonNet();
return me.RequestJsonLanguageChangeBaiDu(input, srcl, dstl);
return jsonnet.RequestJsonLanguageChangeBaiDu(input, srcl, dstl);
}
public string NetXunFei(string input)
{
Messiah me = new jsonNet();
return me.RequestJsonXunFei(input);
return jsonnet.RequestJsonXunFei(input);
}
public string NetLanXunFei(string input, string srcl, string dstl)
{
Messiah me = new jsonNet();
return me.RequestJsonLanguageChangeXunFei(input, srcl, dstl);
return jsonnet.RequestJsonLanguageChangeXunFei(input, srcl, dstl);
}
public string NetAllXunFei(string input, string srcl, string dstl, string xid, string xsecret, string xkey)
{
Messiah me = new jsonNet();
return me.RequestJsonPrivateXunFei(input, srcl, dstl, xid, xsecret, xkey);
return jsonnet.RequestJsonPrivateXunFei(input, srcl, dstl, xid, xsecret, xkey);
}
}
}
/*
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace OCRConsoleApp
{
class Program
{
static void Main(string[] args)
{
using IHost host = CreateHostBuilder(args).Build();
var ocrService = host.Services.GetRequiredService<IOcrService>();
// 截图并保存到本地文件
var bitmap = ocrService.CaptureScreen();
bitmap.Save("screenshot.png", ImageFormat.Png);
// 识别图片中的文字
var text = ocrService.RecognizeText(bitmap);
Console.WriteLine(text);
}
private static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddConsole();
})
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
services.AddSingleton<IOcrService>(new OcrService());
});
}
public interface IOcrService
{
Bitmap CaptureScreen();
string RecognizeText(Bitmap bitmap);
}
public class OcrService : IOcrService
{
public Bitmap CaptureScreen()
{
var screenBounds = ScreenHelper.GetScreenBounds();
var bitmap = new Bitmap(screenBounds.Width, screenBounds.Height);
using (var graphics = Graphics.FromImage(bitmap))
graphics.CopyFromScreen(screenBounds.X, screenBounds.Y, 0, 0, screenBounds.Size);
return bitmap;
}
public string RecognizeText(Bitmap bitmap)
{
var ocrEngine = new IronOcr.AutoOcr();
var result = ocrEngine.Read(bitmap);
return result.Text;
}
}
public static class ScreenHelper
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool GetCursorPos(out Point lpPoint);
public static Rectangle GetScreenBounds()
{
GetCursorPos(out Point point);
var screenBounds = Screen.FromPoint(point).Bounds;
return screenBounds;
}
}
}
*/

@ -29,9 +29,9 @@ namespace WpfApp1.transToNet
static string xunid = "1299c840";
static string xunsec = "M2Q4YzkzZTIwZjNkMGE2ZjkwZWMxNzZk";
static string xunkey = "828cb2dc915d28464a904bda5d7b01b3";
static string xunfl = "ja";
static string xunfl = "en";//ja
static string xundl = "cn";
static string xuntxt = "白上";
static string xuntxt = "Sakura miko";
//static HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://api.fanyi.baidu.com/api/trans/vip/translate?q=apple&from=en&to=zh&appid=20230925001829663&salt=1435660288&sign=c963bc2275c0fd1ed30da014c1ef1933");
@ -53,7 +53,7 @@ namespace WpfApp1.transToNet
Console.WriteLine($"{jsonResponse}\n");
}
*/
private HttpClient HttpRequestC(string url)
private HttpClient HttpRequestC(string url)//创建HTTP
{
HttpClient httpClient = new HttpClient()
{
@ -80,6 +80,94 @@ namespace WpfApp1.transToNet
return sb.ToString();
}
private static string HmacSha256(string message, string secret)//hmachas256加密
{
byte[] keyBytes = Encoding.UTF8.GetBytes(secret);
byte[] messageBytes = Encoding.UTF8.GetBytes(message);
using (HMACSHA256 hmac = new HMACSHA256(keyBytes))
{
byte[] signatureBytes = hmac.ComputeHash(messageBytes);
return Convert.ToBase64String(signatureBytes);
}
}
private static string Sha256(string message)//SHA-256加密
{
byte[] messageBytes = Encoding.UTF8.GetBytes(message);
using (SHA256 sha256 = SHA256.Create())
{
byte[] signatureBytes = sha256.ComputeHash(messageBytes);
return Convert.ToBase64String(signatureBytes);
}
}
private static string signBody(string body)
{
byte[] messageBytes = Encoding.UTF8.GetBytes(body);
using (SHA256 sha256 = SHA256.Create())
{
byte[] signatureBytes = sha256.ComputeHash(messageBytes);
string signature = Convert.ToBase64String(signatureBytes);
return hmacsign(signature, xunsec);
}
}
private static string hmacsign(string signature, string apiSecret)
{
byte[] keyBytes = Encoding.UTF8.GetBytes(apiSecret);
byte[] messageBytes = Encoding.UTF8.GetBytes(signature);
using (HMACSHA256 hmac = new HMACSHA256(keyBytes))
{
byte[] signatureBytes = hmac.ComputeHash(messageBytes);
return Convert.ToBase64String(signatureBytes);
}
}
private static string ComputeSha256(string message)
{
byte[] messageBytes = Encoding.UTF8.GetBytes(message);
using (SHA256 sha256 = SHA256.Create())
{
byte[] signatureBytes = sha256.ComputeHash(messageBytes);
return Convert.ToBase64String(signatureBytes);
}
}
private static string BuildHttpBody()
{
Dictionary<string, object> paramMap = new()
{
{ "from", "cn" },
{ "to", "en" },
{ "appid", xunid },
{ "salt", DateTime.Now.Millisecond.ToString() },
{ "signType", "v3" },
{ "signature", "" },
{ "voice", "0" },
{ "text", "你好,世界!" }
};
string body = JsonConvert.SerializeObject(paramMap);
string signature = signBody(body);
paramMap["signature"] = signature;
return JsonConvert.SerializeObject(paramMap);
}
private static Dictionary<string, string> BuildHttpHeader(string body)
{
string curTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
string param = xunkey + curTime + body;
string checkSum = ComputeSha256(param);
Dictionary<string, string> header = new()
{
{ "Content-Type", "application/json" },
{ "X-Appid", xunid },
{ "X-CurTime", curTime },
{ "X-Param", body },
{ "X-CheckSum", checkSum }
};
return header;
}
public string RequestJsonPrivateBaiDu(string input, string srcl, string dstl, string aid, string kkey)//自定义idkey以及源语言和目标语言
{
string sign = EncryptString(aid + input + salt + kkey);
@ -157,7 +245,31 @@ namespace WpfApp1.transToNet
public string RequestJsonXunFei(string input)
{
return "error";
string body = BuildHttpBody();
Console.WriteLine("【ITSWebAPI body】\n" + body);
Dictionary<string, string> header = BuildHttpHeader(body);
Dictionary<string, object> resultMap = HttpUtil.DoPost(xunweb, header, body);
string re = "error";
if (resultMap != null)
{
string resultStr = resultMap["body"].ToString();
re = resultStr;
Console.WriteLine("【ITS WebAPI 接口调用结果】\n" + resultStr);
//以下仅用于调试
ResponseData resultData = JsonConvert.DeserializeObject<ResponseData>(resultStr);
int code = resultData.code;
if (resultData.code != 0)
{
Console.WriteLine("请前往https://www.xfyun.cn/document/error-code?code=" + code + "查询解决办法");
}
}
else
{
Console.WriteLine("调用失败请根据错误信息检查代码接口文档https://www.xfyun.cn/doc/nlp/xftrans/API.html");
}
return re;
}
public string RequestJsonPrivateXunFei(string input, string srcl, string dstl, string xid, string xse , string xkey)
@ -170,6 +282,7 @@ namespace WpfApp1.transToNet
return "error";
}
}
class Trans_result
{
public string src { get; set; }
@ -179,4 +292,45 @@ namespace WpfApp1.transToNet
{
public List<Trans_result> trans_result { get; set; }
}
class ResponseData
{
public int code { get; set; }
public string message { get; set; }
public string sid { get; set; }
public List<Result> data { get; set; }
}
class Result
{
public string src { get; set; }
public string tgt { get; set; }
}
class HttpUtil//讯飞用
{
public static Dictionary<string, object> DoPost(string url, Dictionary<string, string> header, string body)
{
using (HttpClient client = new HttpClient())
{
foreach (KeyValuePair<string, string> entry in header)
{
client.DefaultRequestHeaders.Add(entry.Key, entry.Value);
}
HttpContent content = new StringContent(body, Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync(url, content).Result;
if (response.IsSuccessStatusCode)
{
string resultStr = response.Content.ReadAsStringAsync().Result;
Dictionary<string, object> resultMap = JsonConvert.DeserializeObject<Dictionary<string, object>>(resultStr);
resultMap.Add("body", resultStr);
return resultMap;
}
else
{
Console.WriteLine("请求失败,错误码:" + response.StatusCode);
return null;
}
}
}
}
}
Loading…
Cancel
Save