-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_camera.cs
More file actions
96 lines (89 loc) · 2.83 KB
/
basic_camera.cs
File metadata and controls
96 lines (89 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Drawing;
using System.Windows.Forms;
namespace CameraApp
{
public partial class MainForm : Form
{
private PictureBox pictureBox;
private Button startButton;
private Button stopButton;
private Timer timer;
private VideoCapture capture;
public MainForm()
{
InitializeComponent();
InitializeCamera();
}
private void InitializeComponent()
{
this.pictureBox = new PictureBox();
this.startButton = new Button();
this.stopButton = new Button();
this.SuspendLayout();
//
// pictureBox
//
this.pictureBox.Location = new Point(10, 10);
this.pictureBox.Size = new Size(640, 480);
this.pictureBox.BackColor = Color.Black;
//
// startButton
//
this.startButton.Location = new Point(10, 500);
this.startButton.Size = new Size(75, 23);
this.startButton.Text = "Start";
this.startButton.Click += StartButton_Click;
//
// stopButton
//
this.stopButton.Location = new Point(100, 500);
this.stopButton.Size = new Size(75, 23);
this.stopButton.Text = "Stop";
this.stopButton.Enabled = false;
this.stopButton.Click += StopButton_Click;
//
// MainForm
//
this.ClientSize = new Size(660, 535);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.startButton);
this.Controls.Add(this.stopButton);
this.Text = "Camera App";
this.ResumeLayout(false);
}
private void InitializeCamera()
{
capture = new VideoCapture();
timer = new Timer();
timer.Interval = 1000 / 30; // 30 frames per second
timer.Tick += Timer_Tick;
}
private void Timer_Tick(object sender, EventArgs e)
{
if (capture.IsOpened())
{
Mat frame = capture.QueryFrame();
if (frame != null)
{
Bitmap bitmap = frame.ToBitmap();
pictureBox.Image = bitmap;
}
}
}
private void StartButton_Click(object sender, EventArgs e)
{
capture.Open(0); // Open the default camera (index 0)
timer.Start();
startButton.Enabled = false;
stopButton.Enabled = true;
}
private void StopButton_Click(object sender, EventArgs e)
{
timer.Stop();
capture.Release();
startButton.Enabled = true;
stopButton.Enabled = false;
}
}
}