Skip to content

Commit

Permalink
支持 翻译功能
Browse files Browse the repository at this point in the history
  • Loading branch information
MakesYT committed Feb 11, 2025
1 parent 8874927 commit 75eef10
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
5 changes: 4 additions & 1 deletion KitopiaEx/KitopiaEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public static IServiceProvider GetServiceProvider()

services.AddTransient<ScreenCaptureEx>();
services.AddTransient<ImagePin.ImagePin>();
return services.BuildServiceProvider();
services.AddTransient<Translate.Translate>();
var buildServiceProvider = services.BuildServiceProvider();
ServiceProvider = buildServiceProvider;
return buildServiceProvider;
}
}
1 change: 1 addition & 0 deletions KitopiaEx/KitopiaEx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@


<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="OpenCvSharp4" Version="4.10.0.20241108" />
<PackageReference Include="OpenCvSharp4.runtime.win" Version="4.10.0.20241108" />
<PackageReference Include="SharpHook" Version="5.3.7"/>
Expand Down
73 changes: 73 additions & 0 deletions KitopiaEx/Translate/Translate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;
using KitopiaEx.Ocr;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Linq;
using PluginCore;
using PluginCore.Attribute;

namespace KitopiaEx.Translate;

public class Translate
{
[ScenarioMethod("翻译文字提取结果", $"{nameof(dResult)}=文字识别结果数据", "return=文字识别结果数据")]
public IEnumerable<OcrResult> TranslateOcrResults(IEnumerable<OcrResult> dResult, CancellationToken ct)
{
List<OcrResult> result = new List<OcrResult>();
try
{
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("User-Agent","KitopiaEx/1.1.0");
var s = httpClient.GetStringAsync("https://edge.microsoft.com/translate/auth").Result;
httpClient.Dispose();
httpClient = new HttpClient();
httpClient.BaseAddress =
new Uri(
"https://api-edge.cognitive.microsofttranslator.com/translate?from=&to=zh-Hans&api-version=3.0&includeSentenceLength=true");
httpClient.DefaultRequestHeaders.Add("authorization",$"Bearer {s}");
httpClient.DefaultRequestHeaders.Add("User-Agent","KitopiaEx/1.1.0");
foreach (var item in dResult)
{
var content = new StringContent($$"""[{"Text":"{{item.Text}}"}]""", Encoding.UTF8, "application/json");

var text = httpClient.PostAsync("",content,ct).Result.Content.ReadAsStringAsync(ct).Result;


try
{
using var doc = JsonDocument.Parse(text);
var text2 = doc.RootElement[0]
.GetProperty("translations")[0]
.GetProperty("text")
.GetString();
result.Add(item with { Text = text2 });
}
catch (Exception e)
{
result.Add(item);
}

}
return result;
}
catch (Exception e)
{
return dResult;
}
}
[Capture("翻译",0xf834)]
public void TranslateImgCapture(ScreenCaptureResult dResult)
{
var service = KitopiaEx.ServiceProvider.GetService<Ocr.Ocr>()!;
var ocrResults = service.OcrImg(dResult, CancellationToken.None);
ocrResults = TranslateOcrResults(ocrResults, CancellationToken.None);
service.OcrResultShow(dResult, ocrResults, CancellationToken.None);
}
}

0 comments on commit 75eef10

Please sign in to comment.