From d09149391dbf9730792d5a4f525faa9dfdffe819 Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Sat, 2 Sep 2023 14:32:42 +0800 Subject: [PATCH] fix2 --- OTP/Form2.Designer.cs | 154 +++++++++++++++++++------------------ OTP/Form2.resx | 50 ++++++------ OTP/Form3.Designer.cs | 36 +++------ OTP/Form3.cs | 173 +++++++++++++----------------------------- 4 files changed, 166 insertions(+), 247 deletions(-) diff --git a/OTP/Form2.Designer.cs b/OTP/Form2.Designer.cs index e6e5f0e..b91aa1b 100644 --- a/OTP/Form2.Designer.cs +++ b/OTP/Form2.Designer.cs @@ -28,101 +28,107 @@ /// private void InitializeComponent() { - this.panel1 = new System.Windows.Forms.Panel(); - this.dataGridView1 = new System.Windows.Forms.DataGridView(); - this.button1 = new System.Windows.Forms.Button(); - this.button2 = new System.Windows.Forms.Button(); - this.button3 = new System.Windows.Forms.Button(); - this.button4 = new System.Windows.Forms.Button(); - this.panel1.SuspendLayout(); - ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); - this.SuspendLayout(); + panel1 = new System.Windows.Forms.Panel(); + dataGridView1 = new System.Windows.Forms.DataGridView(); + button1 = new System.Windows.Forms.Button(); + button2 = new System.Windows.Forms.Button(); + button3 = new System.Windows.Forms.Button(); + button4 = new System.Windows.Forms.Button(); + panel1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)dataGridView1).BeginInit(); + SuspendLayout(); // // panel1 // - this.panel1.Controls.Add(this.dataGridView1); - this.panel1.Location = new System.Drawing.Point(12, 12); - this.panel1.Name = "panel1"; - this.panel1.Size = new System.Drawing.Size(595, 287); - this.panel1.TabIndex = 0; + panel1.Controls.Add(dataGridView1); + panel1.Location = new System.Drawing.Point(14, 14); + panel1.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + panel1.Name = "panel1"; + panel1.Size = new System.Drawing.Size(694, 331); + panel1.TabIndex = 0; // // dataGridView1 // - this.dataGridView1.AllowUserToAddRows = false; - this.dataGridView1.AllowUserToDeleteRows = false; - this.dataGridView1.AllowUserToResizeColumns = false; - this.dataGridView1.AllowUserToResizeRows = false; - this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill; - this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; - this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill; - this.dataGridView1.Location = new System.Drawing.Point(0, 0); - this.dataGridView1.Name = "dataGridView1"; - this.dataGridView1.ReadOnly = true; - this.dataGridView1.RowTemplate.Resizable = System.Windows.Forms.DataGridViewTriState.False; - this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; - this.dataGridView1.Size = new System.Drawing.Size(595, 287); - this.dataGridView1.TabIndex = 0; + dataGridView1.AllowUserToAddRows = false; + dataGridView1.AllowUserToDeleteRows = false; + dataGridView1.AllowUserToResizeColumns = false; + dataGridView1.AllowUserToResizeRows = false; + dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill; + dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill; + dataGridView1.Location = new System.Drawing.Point(0, 0); + dataGridView1.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + dataGridView1.Name = "dataGridView1"; + dataGridView1.ReadOnly = true; + dataGridView1.RowTemplate.Resizable = System.Windows.Forms.DataGridViewTriState.False; + dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; + dataGridView1.Size = new System.Drawing.Size(694, 331); + dataGridView1.TabIndex = 0; // // button1 // - this.button1.Location = new System.Drawing.Point(60, 314); - this.button1.Name = "button1"; - this.button1.Size = new System.Drawing.Size(107, 41); - this.button1.TabIndex = 1; - this.button1.Text = "New Record"; - this.button1.UseVisualStyleBackColor = true; - this.button1.Click += new System.EventHandler(this.button1_Click); + button1.Location = new System.Drawing.Point(70, 362); + button1.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + button1.Name = "button1"; + button1.Size = new System.Drawing.Size(125, 47); + button1.TabIndex = 1; + button1.Text = "New Record"; + button1.UseVisualStyleBackColor = true; + button1.Click += button1_Click; // // button2 // - this.button2.Location = new System.Drawing.Point(187, 314); - this.button2.Name = "button2"; - this.button2.Size = new System.Drawing.Size(107, 41); - this.button2.TabIndex = 2; - this.button2.Text = "Delete Record(s)"; - this.button2.UseVisualStyleBackColor = true; - this.button2.Click += new System.EventHandler(this.button2_Click); + button2.Location = new System.Drawing.Point(218, 362); + button2.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + button2.Name = "button2"; + button2.Size = new System.Drawing.Size(125, 47); + button2.TabIndex = 2; + button2.Text = "Delete Record(s)"; + button2.UseVisualStyleBackColor = true; + button2.Click += button2_Click; // // button3 // - this.button3.Location = new System.Drawing.Point(314, 314); - this.button3.Name = "button3"; - this.button3.Size = new System.Drawing.Size(107, 41); - this.button3.TabIndex = 3; - this.button3.Text = "Save Changes"; - this.button3.UseVisualStyleBackColor = true; - this.button3.Click += new System.EventHandler(this.button3_Click); + button3.Location = new System.Drawing.Point(366, 362); + button3.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + button3.Name = "button3"; + button3.Size = new System.Drawing.Size(125, 47); + button3.TabIndex = 3; + button3.Text = "Save Changes"; + button3.UseVisualStyleBackColor = true; + button3.Click += button3_Click; // // button4 // - this.button4.Location = new System.Drawing.Point(439, 314); - this.button4.Name = "button4"; - this.button4.Size = new System.Drawing.Size(107, 41); - this.button4.TabIndex = 4; - this.button4.Text = "Discard Changes"; - this.button4.UseVisualStyleBackColor = true; - this.button4.Click += new System.EventHandler(this.button4_Click); + button4.Location = new System.Drawing.Point(512, 362); + button4.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + button4.Name = "button4"; + button4.Size = new System.Drawing.Size(125, 47); + button4.TabIndex = 4; + button4.Text = "Discard Changes"; + button4.UseVisualStyleBackColor = true; + button4.Click += button4_Click; // // Form2 // - this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(619, 367); - this.ControlBox = false; - this.Controls.Add(this.button4); - this.Controls.Add(this.button3); - this.Controls.Add(this.button2); - this.Controls.Add(this.button1); - this.Controls.Add(this.panel1); - this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; - this.Name = "Form2"; - this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; - this.Text = "Data Manage"; - this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form2_FormClosing); - this.panel1.ResumeLayout(false); - ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); - this.ResumeLayout(false); - + AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + ClientSize = new System.Drawing.Size(722, 437); + ControlBox = false; + Controls.Add(button4); + Controls.Add(button3); + Controls.Add(button2); + Controls.Add(button1); + Controls.Add(panel1); + FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; + Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + Name = "Form2"; + StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; + Text = "Data Manage"; + FormClosing += Form2_FormClosing; + panel1.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)dataGridView1).EndInit(); + ResumeLayout(false); } #endregion diff --git a/OTP/Form2.resx b/OTP/Form2.resx index 1af7de1..af32865 100644 --- a/OTP/Form2.resx +++ b/OTP/Form2.resx @@ -1,17 +1,17 @@  - diff --git a/OTP/Form3.Designer.cs b/OTP/Form3.Designer.cs index fbd7588..1636835 100644 --- a/OTP/Form3.Designer.cs +++ b/OTP/Form3.Designer.cs @@ -42,9 +42,8 @@ label4 = new System.Windows.Forms.Label(); label5 = new System.Windows.Forms.Label(); groupBox1 = new System.Windows.Forms.GroupBox(); - button7 = new System.Windows.Forms.Button(); - pictureBox1 = new System.Windows.Forms.PictureBox(); button8 = new System.Windows.Forms.Button(); + pictureBox1 = new System.Windows.Forms.PictureBox(); groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit(); SuspendLayout(); @@ -184,7 +183,6 @@ // groupBox1 // groupBox1.Controls.Add(button8); - groupBox1.Controls.Add(button7); groupBox1.Controls.Add(pictureBox1); groupBox1.Controls.Add(label4); groupBox1.Controls.Add(label5); @@ -198,20 +196,15 @@ groupBox1.TabStop = false; groupBox1.Text = "Debug Info"; // - // button7 + // button8 // - button7.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right; - button7.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - button7.Location = new System.Drawing.Point(427, 187); - button7.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); - button7.MinimumSize = new System.Drawing.Size(88, 87); - button7.Name = "button7"; - button7.Size = new System.Drawing.Size(88, 87); - button7.TabIndex = 15; - button7.Text = "Pause"; - button7.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; - button7.UseVisualStyleBackColor = true; - button7.Click += button7_Click; + button8.Location = new System.Drawing.Point(427, 0); + button8.Name = "button8"; + button8.Size = new System.Drawing.Size(101, 23); + button8.TabIndex = 15; + button8.Text = "Stop Camera"; + button8.UseVisualStyleBackColor = true; + button8.Click += button8_Click; // // pictureBox1 // @@ -223,16 +216,6 @@ pictureBox1.TabIndex = 14; pictureBox1.TabStop = false; // - // button8 - // - button8.Location = new System.Drawing.Point(427, 0); - button8.Name = "button8"; - button8.Size = new System.Drawing.Size(101, 23); - button8.TabIndex = 15; - button8.Text = "Stop Camera"; - button8.UseVisualStyleBackColor = true; - button8.Click += button8_Click; - // // Form3 // AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -283,7 +266,6 @@ private System.Windows.Forms.Label label5; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.PictureBox pictureBox1; - private System.Windows.Forms.Button button7; private System.Windows.Forms.Button button8; } } \ No newline at end of file diff --git a/OTP/Form3.cs b/OTP/Form3.cs index cd5e1dc..2f7154d 100644 --- a/OTP/Form3.cs +++ b/OTP/Form3.cs @@ -7,10 +7,9 @@ using System.Drawing; using System.IO; using System.Linq; using System.Threading; +using System.Timers; using System.Windows.Forms; using ZXing; -//using AForge.Video; -//using AForge.Video.DirectShow; namespace OTP { @@ -19,12 +18,11 @@ 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; + private bool isRecording = false; //private Bitmap previousBitmap = null; private SynchronizationContext synchContext; + private System.Windows.Forms.Timer timer; // Constructed capture device. private CaptureDevice captureDevice; @@ -32,16 +30,34 @@ namespace OTP public Form3() { InitializeComponent(); - //videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); + // 初始化定时器,间隔为1秒(1000毫秒) + timer = new System.Windows.Forms.Timer(); + timer.Interval = 1000; + timer.Tick += Timer_Tick; } + private void Timer_Tick(object sender, EventArgs e) + { + // 检查是否有图像可用 + if (pictureBox1.Image != null && isRecording) + { + // 调用 ScanQRCode 方法,将当前图像传递给它 + ScanQRCode((Bitmap)pictureBox1.Image); + } + } - private void Form3_FormClosing(object sender, FormClosingEventArgs e) + + private async void Form3_FormClosing(object sender, FormClosingEventArgs e) { StopCapturing(); // 关闭窗口时停止截图 - if (isCameraRunning) + if (isRecording) { - StopCamera(); // 关闭窗口时停止摄像头 + await this.captureDevice.StopAsync(); + pictureBox1.Image = null; + isRecording = false; + // Discard capture device. + this.captureDevice?.Dispose(); + this.captureDevice = null; } } @@ -115,7 +131,7 @@ namespace OTP ScreenCapturer.StopCapture(); // 停止截图 } - private void ScanQRCode(Bitmap bitmap) + private async void ScanQRCode(Bitmap bitmap) { // 创建二维码读取器实例 var barcodeReader = new ZXing.Windows.Compatibility.BarcodeReader(); @@ -143,6 +159,7 @@ namespace OTP string[] parts = decodedText.Split(new char[] { '/', '=', '&', '?' }); if (parts.Length >= 6) { + //扫码成功 string title = parts[3]; // 获取 title 部分 string secretKey = parts[5]; // 获取 secretKey 部分 @@ -150,7 +167,15 @@ namespace OTP this.Invoke((MethodInvoker)(() => textBox1.Text = title)); this.Invoke((MethodInvoker)(() => textBox2.Text = secretKey)); this.Invoke((MethodInvoker)(() => label4.Text = "Success")); - //StopCamera(); + if (isRecording) + { + await this.captureDevice.StopAsync(); + pictureBox1.Image = null; + isRecording = false; + // Discard capture device. + this.captureDevice?.Dispose(); + this.captureDevice = null; + } } else { @@ -199,15 +224,9 @@ namespace OTP private async void button5_Click(object sender, EventArgs e) { this.synchContext = SynchronizationContext.Current; - - //////////////////////////////////////////////// - // Initialize and start capture device - - // Capture device enumeration: var devices = new CaptureDevices(); var descriptors = devices.EnumerateDescriptors(). - // You could filter by device type and characteristics. //Where(d => d.DeviceType == DeviceTypes.DirectShow). // Only DirectShow device. Where(d => d.Characteristics.Length >= 1). // One or more valid video characteristics. ToArray(); @@ -216,26 +235,15 @@ namespace OTP var descriptor0 = descriptors.ElementAtOrDefault(0); if (descriptor0 != null) - { -#if false - // Request video characteristics strictly: - // Will raise exception when parameters are not accepted. - var characteristics = new VideoCharacteristics( - PixelFormats.JPEG, 1920, 1080, 60); -#else - // Or, you could choice from device descriptor: - // Hint: Show up video characteristics into ComboBox and like. + { var characteristics = descriptor0.Characteristics. FirstOrDefault(c => c.PixelFormat != PixelFormats.Unknown); -#endif if (characteristics != null) { - // Open capture device: this.captureDevice = await descriptor0.OpenAsync( characteristics, this.OnPixelBufferArrived); - // Start capturing. await this.captureDevice.StartAsync(); } } @@ -243,105 +251,22 @@ namespace OTP } private void OnPixelBufferArrived(PixelBufferScope bufferScope) { - //////////////////////////////////////////////// - // Pixel buffer has arrived. - // NOTE: Perhaps this thread context is NOT UI thread. -#if false - // Get image data binary: - byte[] image = bufferScope.Buffer.ExtractImage(); -#else - // Or, refer image data binary directly. - // (Advanced manipulation, see README.) ArraySegment image = bufferScope.Buffer.ReferImage(); -#endif - // Convert to Stream (using FlashCap.Utilities) using (var stream = image.AsStream()) { - // Decode image data to a bitmap: var bitmap = Image.FromStream(stream); - - // `bitmap` is copied, so we can release pixel buffer now. bufferScope.ReleaseNow(); - - // Switch to UI thread. - // HACK: Here is using `SynchronizationContext.Post()` instead of `Control.Invoke()`. - // Because in sensitive states when the form is closing, - // `Control.Invoke()` can fail with exception. this.synchContext.Post(_ => { - // HACK: on .NET Core, will be leaked (or delayed GC?) - // So we could release manually before updates. - var oldImage = this.BackgroundImage; - if (oldImage != null) - { - //this.BackgroundImage = null; - oldImage.Dispose(); - } - - // Update a bitmap. pictureBox1.Image = bitmap; + if (!isRecording) + { + isRecording = true; + timer.Start(); + } }, null); } } - private void StartCamera() - { - //if (!isCameraRunning) // 添加条件检查摄像头是否已经在运行 - //{ - // 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."); - // } - //} - } - - private void StopCamera() - { - //if (camera != null && camera.IsRunning) - //{ - // camera.SignalToStop(); - // camera.WaitForStop(); - // camera.NewFrame -= Camera_NewFrame; // 取消注册摄像头捕获图像的事件 - // camera = null; // 将摄像头对象置为null,以便下次重新创建 - // isCameraRunning = false; // 设置摄像头运行状态为 false - // 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; - // // previousBitmap = bitmap; - // //} - // //else - // //{ - // // ScanQRCode(bitmap); - // // bitmap.Dispose(); - // //} - //} - - private void button7_Click(object sender, EventArgs e) - { - isScanningPaused = !isScanningPaused; - button7.Text = isScanningPaused ? "Resume" : "Pause"; - } private void button6_Click(object sender, EventArgs e) { @@ -370,11 +295,17 @@ namespace OTP } } - private void button8_Click(object sender, EventArgs e) + private async void button8_Click(object sender, EventArgs e) { - // Discard capture device. - this.captureDevice?.Dispose(); - this.captureDevice = null; + if (isRecording) + { + await this.captureDevice.StopAsync(); + pictureBox1.Image = null; + isRecording = false; + // Discard capture device. + this.captureDevice?.Dispose(); + this.captureDevice = null; + } } } }