The camera scanning function has unpredictable issues, and repairing it is currently not being considered. The clipboard import feature has been implemented.
This commit is contained in:
Chenx221 2023-08-22 19:12:19 +08:00
parent b1b87ad724
commit a3b6fde6a6
2 changed files with 101 additions and 37 deletions

43
OTP/Form3.Designer.cs generated
View File

@ -42,8 +42,9 @@
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.button7 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label6 = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
@ -138,6 +139,7 @@
this.button6.TabIndex = 9;
this.button6.Text = "Import from Clipboard";
this.button6.UseVisualStyleBackColor = true;
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// label3
//
@ -168,6 +170,7 @@
//
// groupBox1
//
this.groupBox1.Controls.Add(this.label6);
this.groupBox1.Controls.Add(this.button7);
this.groupBox1.Controls.Add(this.pictureBox1);
this.groupBox1.Controls.Add(this.label4);
@ -180,6 +183,22 @@
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Debug Info";
//
// button7
//
this.button7.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.button7.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button7.Location = new System.Drawing.Point(366, 162);
this.button7.MinimumSize = new System.Drawing.Size(75, 75);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(75, 75);
this.button7.TabIndex = 15;
this.button7.Text = "Pause";
this.button7.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.button7.UseVisualStyleBackColor = true;
this.button7.Click += new System.EventHandler(this.button7_Click);
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(105, 113);
@ -189,21 +208,14 @@
this.pictureBox1.TabIndex = 14;
this.pictureBox1.TabStop = false;
//
// button7
// label6
//
this.button7.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.button7.Font = new System.Drawing.Font("Microsoft Sans Serif", 48F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button7.Location = new System.Drawing.Point(366, 162);
this.button7.MinimumSize = new System.Drawing.Size(75, 75);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(75, 75);
this.button7.TabIndex = 15;
this.button7.Text = "●";
this.button7.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.button7.UseVisualStyleBackColor = true;
this.button7.Click += new System.EventHandler(this.button7_Click);
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(8, 146);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(91, 91);
this.label6.TabIndex = 16;
this.label6.Text = "Note:\r\n使用相机扫码会\r\n自动填充内容到\r\n输入框\r\n因为坑比较多不\r\n好填所以这个\r\n功能不是很完善";
//
// Form3
//
@ -256,5 +268,6 @@
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Label label6;
}
}

View File

@ -9,9 +9,11 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using ZXing;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Drawing.Imaging;
namespace OTP
{
@ -24,6 +26,7 @@ namespace OTP
private VideoCaptureDevice camera; // 声明用于摄像头捕获的变量
private bool isCameraRunning = false; // 用于标识摄像头的运行状态
private bool isScanningPaused = false;
private Bitmap previousBitmap = null;
public Form3()
{
@ -35,7 +38,10 @@ namespace OTP
private void Form3_FormClosing(object sender, FormClosingEventArgs e)
{
StopCapturing(); // 关闭窗口时停止截图
StopCamera(); // 关闭窗口时停止摄像头
if (isCameraRunning)
{
StopCamera(); // 关闭窗口时停止摄像头
}
}
private void button1_Click(object sender, EventArgs e)
@ -132,7 +138,6 @@ namespace OTP
ScreenCapturer.StopCapture(); // 停止截图
}
private void ScanQRCode(Bitmap bitmap)
{
// 创建二维码读取器实例
@ -149,7 +154,7 @@ namespace OTP
// 判断是否符合期望的格式
if (decodedText.StartsWith("otpauth://totp/") && decodedText.Contains("?secret="))
{
if (!isLocalfile)
if (!isLocalfile && !isScanningPaused)
{
StopCapturing();
this.Invoke((MethodInvoker)(() => label5.Text = "Stopped"));
@ -168,6 +173,7 @@ namespace OTP
this.Invoke((MethodInvoker)(() => textBox1.Text = title));
this.Invoke((MethodInvoker)(() => textBox2.Text = secretKey));
this.Invoke((MethodInvoker)(() => label4.Text = "Success"));
//StopCamera();
}
else
{
@ -185,6 +191,10 @@ namespace OTP
{
label3.Text = "None";
label4.Text = "QR code not detected.";
}else if (isScanningPaused)
{
this.Invoke((MethodInvoker)(() => label3.Text = "None"));
this.Invoke((MethodInvoker)(() => label4.Text = "QR code not detected."));
}
}
}
@ -210,28 +220,35 @@ namespace OTP
private void button5_Click(object sender, EventArgs e)
{
if (!isCameraRunning)
isScanningPaused = false;
button7.Text = "Pause";
if (isCameraRunning)
{
StopCamera(); // 停止摄像头
}else
{
StartCamera(); // 启动摄像头
}
else
{
StopCamera(); // 停止摄像头
}
}
private void StartCamera()
{
if (videoDevices != null && videoDevices.Count > 0)
if (!isCameraRunning) // 添加条件检查摄像头是否已经在运行
{
camera = new VideoCaptureDevice(videoDevices[0].MonikerString);
camera.NewFrame += Camera_NewFrame; // 注册摄像头捕获图像的事件
camera.Start(); // 启动摄像头
isCameraRunning = true; // 设置摄像头运行状态为 true
//button5.Text = "Stop Camera"; // 更新按钮文本
}
else
{
MessageBox.Show("No camera device found.");
if (videoDevices != null && videoDevices.Count > 0)
{
if (camera == null)
{
camera = new VideoCaptureDevice(videoDevices[0].MonikerString);
}
camera.NewFrame += Camera_NewFrame; // 注册摄像头捕获图像的事件
camera.Start(); // 启动摄像头
isCameraRunning = true; // 设置摄像头运行状态为 true
}
else
{
MessageBox.Show("No camera device found.");
}
}
}
@ -242,22 +259,29 @@ namespace OTP
camera.SignalToStop();
camera.WaitForStop();
camera.NewFrame -= Camera_NewFrame; // 取消注册摄像头捕获图像的事件
camera = null; // 将摄像头对象置为null以便下次重新创建
isCameraRunning = false; // 设置摄像头运行状态为 false
//button5.Text = "Start Camera"; // 更新按钮文本
isScanningPaused = false;
button7.Text = "Pause";
}
}
private void Camera_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
// 进行二维码扫描
Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();
if (!isScanningPaused)
{
if (previousBitmap != null)
{
previousBitmap.Dispose();
}
pictureBox1.Image = bitmap;
}else
previousBitmap = bitmap;
}
else
{
// 进行二维码扫描
ScanQRCode(bitmap);
bitmap.Dispose();
}
}
@ -266,5 +290,32 @@ namespace OTP
isScanningPaused = !isScanningPaused;
button7.Text = isScanningPaused ? "Resume" : "Pause";
}
private void button6_Click(object sender, EventArgs e)
{
string clipboardText = Clipboard.GetText();
if (!string.IsNullOrWhiteSpace(clipboardText) &&
clipboardText.StartsWith("otpauth://totp/") &&
clipboardText.Contains("?secret="))
{
string[] parts = clipboardText.Split(new char[] { '/', '=', '&', '?' });
if (parts.Length >= 6)
{
string title = parts[3]; // 获取 title 部分
string secretKey = parts[5]; // 获取 secretKey 部分
MessageBox.Show("OTP link successfully pasted.");
// 填充文本框
textBox1.Text = title;
textBox2.Text = secretKey;
}
}
else
{
MessageBox.Show("Clipboard content is not a valid OTP link.");
}
}
}
}