Commit f71c8b62 by 陶然

部分方法实现

parent 6d9aad51
...@@ -122,6 +122,7 @@ namespace Kivii.Sample.ImageUploader ...@@ -122,6 +122,7 @@ namespace Kivii.Sample.ImageUploader
// add all devices to combo // add all devices to combo
cameraQuantity = videoDevices.Count; cameraQuantity = videoDevices.Count;
videoDevice = new VideoCaptureDevice(videoDevices[currentCameraIndex].MonikerString); videoDevice = new VideoCaptureDevice(videoDevices[currentCameraIndex].MonikerString);
gbCamView.Text = videoDevices[currentCameraIndex].Name;
enumeratedSupportedFrameSizes(videoDevice); enumeratedSupportedFrameSizes(videoDevice);
} }
else else
...@@ -135,6 +136,21 @@ namespace Kivii.Sample.ImageUploader ...@@ -135,6 +136,21 @@ namespace Kivii.Sample.ImageUploader
} }
} }
private void switchCamera()
{
if (videoDevice.IsRunning)
{
Disconnect();
}
if (currentCameraIndex + 1 > cameraQuantity - 1) currentCameraIndex = 0;
else currentCameraIndex = currentCameraIndex + 1;
videoDevice = new VideoCaptureDevice(videoDevices[currentCameraIndex].MonikerString);
gbCamView.Text = videoDevices[currentCameraIndex].Name;
enumeratedSupportedFrameSizes(videoDevice);
// 默认选择第一个分辨率
cbxResolution.SelectedIndex = 0;
}
/// <summary> /// <summary>
/// 列出摄像头支持的分辨率 /// 列出摄像头支持的分辨率
/// </summary> /// </summary>
...@@ -143,7 +159,6 @@ namespace Kivii.Sample.ImageUploader ...@@ -143,7 +159,6 @@ namespace Kivii.Sample.ImageUploader
{ {
this.Cursor = Cursors.WaitCursor; this.Cursor = Cursors.WaitCursor;
cbxResolution.Items.Clear(); cbxResolution.Items.Clear();
try try
{ {
// 获取摄像头支持的分辨率列表 // 获取摄像头支持的分辨率列表
...@@ -176,7 +191,6 @@ namespace Kivii.Sample.ImageUploader ...@@ -176,7 +191,6 @@ namespace Kivii.Sample.ImageUploader
{ {
videoDevice.VideoResolution = videoCapabilities[selectedIndex]; videoDevice.VideoResolution = videoCapabilities[selectedIndex];
} }
vspCamView.VideoSource = videoDevice; vspCamView.VideoSource = videoDevice;
vspCamView.Start(); vspCamView.Start();
} }
...@@ -216,6 +230,7 @@ namespace Kivii.Sample.ImageUploader ...@@ -216,6 +230,7 @@ namespace Kivii.Sample.ImageUploader
initFormCtlEnable(false); initFormCtlEnable(false);
setDefault(tbxServiceUrl, tbxUserName, tbxPassword); setDefault(tbxServiceUrl, tbxUserName, tbxPassword);
Thread.Sleep(100); Thread.Sleep(100);
if (videoDevice == null) initCameras();
// 默认选择第一个分辨率 // 默认选择第一个分辨率
cbxResolution.SelectedIndex = 0; cbxResolution.SelectedIndex = 0;
}, true); }, true);
...@@ -224,7 +239,8 @@ namespace Kivii.Sample.ImageUploader ...@@ -224,7 +239,8 @@ namespace Kivii.Sample.ImageUploader
initFormCtlEnable(); initFormCtlEnable();
_client.Dispose(); _client.Dispose();
_client = null; _client = null;
MessageBox.Show(err.Message, "Login Error"); var msg = err.GetResponseStatus().Message;
MessageBox.Show(msg, "Login Error");
}, true); }, true);
} }
...@@ -264,5 +280,97 @@ namespace Kivii.Sample.ImageUploader ...@@ -264,5 +280,97 @@ namespace Kivii.Sample.ImageUploader
{ {
Disconnect(); Disconnect();
} }
private void btnSwitch_Click(object sender, EventArgs e)
{
switchCamera();
}
private void btnShot_Click(object sender, EventArgs e)
{
try
{
if (videoDevice != null && vspCamView.IsRunning)
{
// 捕获当前帧
Bitmap bitmap = vspCamView.GetCurrentVideoFrame();
ptbPhotoDisplay.Image = bitmap;
// 创建图片 PictureBox 控件
PictureBox pictureBox = new PictureBox();
pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox.Dock = DockStyle.Fill;
pictureBox.Image = bitmap;
// 添加图片点击动作
pictureBox.Click += PictureBox_Click;
// 创建图片名称的 Label 控件
Label nameLabel = new Label();
String name = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss");
nameLabel.Text = name;
nameLabel.Dock = DockStyle.Fill;
nameLabel.TextAlign = ContentAlignment.MiddleCenter;
// 创建 SplitContainer 控件
SplitContainer splitContainer = new SplitContainer();
splitContainer.Size = new Size(210, 140);
splitContainer.Orientation = Orientation.Horizontal;
splitContainer.SplitterWidth = 1;
splitContainer.SplitterDistance = 100;
splitContainer.Panel1.Padding = new Padding(0, 10, 0, 0);
splitContainer.Panel1.Controls.Add(pictureBox);
splitContainer.Panel2.Controls.Add(nameLabel);
// 将 SplitContainer 添加到 flpPhotosTaken 中
flpPhotosTaken.Controls.Add(splitContainer);
// 改变排序,将新增的添加到顶部
flpPhotosTaken.Controls.SetChildIndex(splitContainer, 0);
// 将拍摄的照片对象添加到集合中
photos.Add(new Photo(bitmap, name));
// 遍历 flpPhotosTaken 中的控件,将之前选中的 SplitContainer 边框改为默认
foreach (Control control in flpPhotosTaken.Controls)
{
if (control is SplitContainer controlContainer)
{
controlContainer.BorderStyle = BorderStyle.None;
}
}
// 为新添加的 SplitContainer 添加边框
splitContainer.BorderStyle = BorderStyle.FixedSingle;
}
}
catch (Exception ex)
{
MessageBox.Show("无法拍照" + ex.Message);
}
}
private void PictureBox_Click(object sender, EventArgs e)
{
// 获取点击的 PictureBox 中的图片
PictureBox pictureBox = (PictureBox)sender;
// 遍历 flpPhotosTaken 中的控件,找到对应的 SplitContainer,并为其设置边框样式
foreach (Control control in flpPhotosTaken.Controls)
{
if (control is SplitContainer splitContainer)
{
// 如果 SplitContainer 包含点击的 PictureBox,则设置其边框,否则设置为默认
if (splitContainer.Panel1.Controls.Count > 0 && splitContainer.Panel1.Controls[0] == pictureBox)
{
splitContainer.BorderStyle = BorderStyle.FixedSingle;
}
else
{
splitContainer.BorderStyle = BorderStyle.None;
}
}
}
// 在界面中的 PictureBox 中显示该图片
ptbPhotoDisplay.Image = (Bitmap)pictureBox.Image;
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment