From b1b87ad724ea783d52b99020fe0f47d8e1366d73 Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Tue, 22 Aug 2023 15:21:52 +0800 Subject: [PATCH] The part of calling the camera has been implemented but the recognition is not yet complete --- OTP/Form3.Designer.cs | 38 ++++++++++++++++++++++-- OTP/Form3.cs | 68 +++++++++++++++++++++++++++++++++++++++++++ OTP/OTP.csproj | 9 ++++++ OTP/packages.config | 3 ++ 4 files changed, 116 insertions(+), 2 deletions(-) diff --git a/OTP/Form3.Designer.cs b/OTP/Form3.Designer.cs index b3b517f..95b0da7 100644 --- a/OTP/Form3.Designer.cs +++ b/OTP/Form3.Designer.cs @@ -42,7 +42,10 @@ 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.groupBox1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.SuspendLayout(); // // label1 @@ -125,6 +128,7 @@ this.button5.TabIndex = 8; this.button5.Text = "Scan QR Code with Camera"; this.button5.UseVisualStyleBackColor = true; + this.button5.Click += new System.EventHandler(this.button5_Click); // // button6 // @@ -164,22 +168,49 @@ // // groupBox1 // + this.groupBox1.Controls.Add(this.button7); + this.groupBox1.Controls.Add(this.pictureBox1); this.groupBox1.Controls.Add(this.label4); this.groupBox1.Controls.Add(this.label5); this.groupBox1.Controls.Add(this.label3); this.groupBox1.Location = new System.Drawing.Point(15, 148); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(453, 243); + this.groupBox1.Size = new System.Drawing.Size(453, 288); this.groupBox1.TabIndex = 14; this.groupBox1.TabStop = false; this.groupBox1.Text = "Debug Info"; // + // pictureBox1 + // + this.pictureBox1.Location = new System.Drawing.Point(105, 113); + this.pictureBox1.Name = "pictureBox1"; + this.pictureBox1.Size = new System.Drawing.Size(255, 169); + this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom; + this.pictureBox1.TabIndex = 14; + this.pictureBox1.TabStop = false; + // + // 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", 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); + // // Form3 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.CancelButton = this.button2; - this.ClientSize = new System.Drawing.Size(488, 403); + this.ClientSize = new System.Drawing.Size(488, 448); this.Controls.Add(this.groupBox1); this.Controls.Add(this.button6); this.Controls.Add(this.button5); @@ -201,6 +232,7 @@ this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form3_FormClosing); this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -222,5 +254,7 @@ private System.Windows.Forms.Label label4; private System.Windows.Forms.Label label5; private System.Windows.Forms.GroupBox groupBox1; + private System.Windows.Forms.PictureBox pictureBox1; + private System.Windows.Forms.Button button7; } } \ No newline at end of file diff --git a/OTP/Form3.cs b/OTP/Form3.cs index 002b638..b717d1e 100644 --- a/OTP/Form3.cs +++ b/OTP/Form3.cs @@ -10,6 +10,8 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using ZXing; +using AForge.Video; +using AForge.Video.DirectShow; namespace OTP { @@ -18,15 +20,22 @@ namespace OTP private string connectionString = "Data Source=key.db;Version=3;"; private bool isCapturing = false; // 添加标识截图状态的成员变量 private bool isLocalfile = false; + private FilterInfoCollection videoDevices; // 声明用于存储摄像头设备信息的变量 + private VideoCaptureDevice camera; // 声明用于摄像头捕获的变量 + private bool isCameraRunning = false; // 用于标识摄像头的运行状态 + private bool isScanningPaused = false; public Form3() { InitializeComponent(); + videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); + } private void Form3_FormClosing(object sender, FormClosingEventArgs e) { StopCapturing(); // 关闭窗口时停止截图 + StopCamera(); // 关闭窗口时停止摄像头 } private void button1_Click(object sender, EventArgs e) @@ -198,5 +207,64 @@ namespace OTP } } } + + private void button5_Click(object sender, EventArgs e) + { + if (!isCameraRunning) + { + StartCamera(); // 启动摄像头 + } + else + { + StopCamera(); // 停止摄像头 + } + } + private void StartCamera() + { + if (videoDevices != null && videoDevices.Count > 0) + { + 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."); + } + } + + private void StopCamera() + { + if (camera != null && camera.IsRunning) + { + camera.SignalToStop(); + camera.WaitForStop(); + camera.NewFrame -= Camera_NewFrame; // 取消注册摄像头捕获图像的事件 + isCameraRunning = false; // 设置摄像头运行状态为 false + //button5.Text = "Start Camera"; // 更新按钮文本 + } + } + + private void Camera_NewFrame(object sender, NewFrameEventArgs eventArgs) + { + // 进行二维码扫描 + Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone(); + if (!isScanningPaused) + { + pictureBox1.Image = bitmap; + }else + { + // 进行二维码扫描 + ScanQRCode(bitmap); + } + } + + private void button7_Click(object sender, EventArgs e) + { + isScanningPaused = !isScanningPaused; + button7.Text = isScanningPaused ? "Resume" : "Pause"; + } } } diff --git a/OTP/OTP.csproj b/OTP/OTP.csproj index 2aa5424..ffdbe92 100644 --- a/OTP/OTP.csproj +++ b/OTP/OTP.csproj @@ -36,6 +36,15 @@ 4 + + ..\packages\AForge.2.2.5\lib\AForge.dll + + + ..\packages\AForge.Video.2.2.5\lib\AForge.Video.dll + + + ..\packages\AForge.Video.DirectShow.2.2.5\lib\AForge.Video.DirectShow.dll + ..\packages\CircularProgressBar.2.8.0.16\lib\net40\CircularProgressBar.dll diff --git a/OTP/packages.config b/OTP/packages.config index 2c93e7f..1bf25ae 100644 --- a/OTP/packages.config +++ b/OTP/packages.config @@ -1,5 +1,8 @@  + + +