自制实时翻译小工具

程序员有二十年 2024-10-30 11:02:52

在开始,先说说这个小工具的来由。我计划在11月16号在东京举办一次《AI应用开发之旅》的社区活动(https://www.just-agi.com/event.html),活动华人为主,所以用中文,朋友说要带日本人过来,问有没有日语字幕,看了一下我的Teams,只有实时字幕,并没有翻译,于是就开始自己造轮子了。

这个小工具是用WinForm开发,只保留源语言文字和翻译后文字显示,其他部分作透明化处理,实时翻译的API调用的是Microsoft Speech的翻译API,10来分钟的工作量吧。

MainForm.Designer.cs文件:

namespace JapneseHelper{ partial MainForm { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = ; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != )) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { ZhLab = new Label(); JaLab = new Label(); SuspendLayout(); // // ZhLab // ZhLab.BackColor = Color.Transparent; ZhLab.Dock = DockStyle.Bottom; ZhLab.Font = new Font("Microsoft YaHei UI", 24F); ZhLab.ForeColor = Color.White; ZhLab.Location = new Point(0, 530); ZhLab.Margin = new Padding(5, 0, 5, 0); ZhLab.Name = "ZhLab"; ZhLab.Size = new Size(2259, 67); ZhLab.TabIndex = 0; ZhLab.Text = "这里是字幕"; ZhLab.TextAlign = ContentAlignment.MiddleCenter; ZhLab.MouseDown += ControlMouseDown; ZhLab.MouseMove += ControlMouseMove; ZhLab.MouseUp += ControlMouseUp; // // JaLab // JaLab.BackColor = Color.Transparent; JaLab.Dock = DockStyle.Fill; JaLab.Font = new Font("Microsoft YaHei UI", 32F); JaLab.ForeColor = Color.White; JaLab.Location = new Point(0, 0); JaLab.Margin = new Padding(5, 0, 5, 0); JaLab.Name = "JaLab"; JaLab.Size = new Size(2259, 530); JaLab.TabIndex = 1; JaLab.TextAlign = ContentAlignment.BottomCenter; JaLab.MouseDown += ControlMouseDown; JaLab.MouseMove += ControlMouseMove; JaLab.MouseUp += ControlMouseUp; // // MainForm // AutoScaleDimensions = new SizeF(19F, 41F); AutoScaleMode = AutoScaleMode.Font; BackColor = Color.Black; BackgroundImageLayout = ImageLayout.Stretch; ClientSize = new Size(2259, 597); Controls.Add(JaLab); Controls.Add(ZhLab); DoubleBuffered = true; Font = new Font("Microsoft YaHei UI", 16F); FormBorderStyle = FormBorderStyle.None; Margin = new Padding(5); Name = "MainForm"; Text = "字幕"; TopMost = true; TransparencyKey = Color.Black; Load += MainForm_Load; ResumeLayout(false); } #endregion private Label ZhLab; private Label JaLab; }}

MainForm.cs文件:

using Microsoft.CognitiveServices.Speech;using Microsoft.CognitiveServices.Speech.Translation;namespace JapneseHelper{ public partial MainForm : Form { public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { var sourceLanguage = System.Configuration.ConfigurationManager.AppSettings["sourceLanguage"]; var targetLanguage = System.Configuration.ConfigurationManager.AppSettings["targetLanguage"]; var apikey = System.Configuration.ConfigurationManager.AppSettings["apikey"]; var region = System.Configuration.ConfigurationManager.AppSettings["region"]; var config = SpeechTranslationConfig.FromSubscription(apikey, region); config.SpeechRecognitionLanguage = sourceLanguage; config.AddTargetLanguage(targetLanguage); var recognizer = new TranslationRecognizer(config); recognizer.Recognizing += (s, e) => { this.Invoke(() => { ZhLab.Text = e.Result.Text; foreach (var element in e.Result.Translations) { JaLab.Text = element.Value; } }); }; recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false).GetAwaiter(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); Rectangle workingArea = Screen.PrimaryScreen.WorkingArea; var x = (workingArea.Width - this.Width) / 2 + workingArea.Left; var y = workingArea.Bottom - this.Height; this.Location = new Point(x, y); } private Point mouseOffset; private bool isMouseDown = false; private void ControlMouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { mouseOffset = new Point(-e.X, -e.Y); isMouseDown = true; } } private void ControlMouseMove(object sender, MouseEventArgs e) { if (isMouseDown) { Point mousePos = Control.MousePosition; mousePos.Offset(mouseOffset.X, mouseOffset.Y - this.Height / 2); this.Location = mousePos; } } private void ControlMouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { isMouseDown = false; } } }}

效果图:

正好这几天与营业部开会,试用了几次,总体的意思还是能搞清楚的,与现场的环境也有一定的关系。

0 阅读:0

程序员有二十年

简介:感谢大家的关注