You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

506 lines
20 KiB

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge.Video.DirectShow;
using System.Threading;
using System.Reflection;
using ShenTun.ImageCollection.Common;
using OpenCvSharp;
namespace ShenTun.ImageCollection.AForgeCamera
{
public partial class FrmCamera : Form
{
private FilterInfoCollection videoDevices;
private VideoCaptureDevice videoSource;
private ImageData imgData;
private RequestInterface requset;
public DialogResponse Result { get; private set; }
public FrmCamera()
{
InitializeComponent();
}
public FrmCamera(RequestInterface _requset)
{
InitializeComponent();
requset= _requset;
// this.Refresh();
}
private void CameraConn()
{
videoSource = new VideoCaptureDevice(videoDevices[cmbCamera.SelectedIndex].MonikerString);
int index = Convert.ToInt32(cmbPixelVideo.SelectedValue);
videoSource.VideoResolution = videoSource.VideoCapabilities[index];
videoSourcePlayer1.VideoSource = videoSource;
}
private void SetPatientInfo()
{
if (requset == null) return;
lbBarCode.Text = requset.BarCode;
lbPatName.Text = requset.PatientName;
lbSexName.Text = requset.SexName;
lbAge.Text = requset.Age;
lbAsbitenNames.Text = requset.AsbitemName;
}
private void SetRangValue()
{
VideoProcAmpFlags flag;
int value, maxvalue, minvalue, stepSize, defalutvalue;
videoSource.GetVideoProperty(VideoProcAmpProperty.Brightness, out value, out flag);
videoSource.GetVideoPropertyRange(VideoProcAmpProperty.Brightness, out minvalue, out maxvalue, out stepSize, out defalutvalue, out flag);
tbBrightness.MaxValue = maxvalue;
tbBrightness.MinValue = minvalue;
tbBrightness.Tag = flag;
tbBrightness.Value = value;
videoSource.GetVideoProperty(VideoProcAmpProperty.Contrast, out value, out flag);
videoSource.GetVideoPropertyRange(VideoProcAmpProperty.Contrast, out minvalue, out maxvalue, out stepSize, out defalutvalue, out flag);
tbContrast.MaxValue = maxvalue;
tbContrast.MinValue = minvalue;
tbContrast.Tag = flag;
tbContrast.Value = value;
videoSource.GetVideoProperty(VideoProcAmpProperty.Hue, out value, out flag);
videoSource.GetVideoPropertyRange(VideoProcAmpProperty.Hue, out minvalue, out maxvalue, out stepSize, out defalutvalue, out flag);
tbHue.MaxValue = maxvalue;
tbHue.MinValue = minvalue;
tbHue.Tag = flag;
tbHue.Value = value;
videoSource.GetVideoProperty(VideoProcAmpProperty.Saturation, out value, out flag);
videoSource.GetVideoPropertyRange(VideoProcAmpProperty.Saturation, out minvalue, out maxvalue, out stepSize, out defalutvalue, out flag);
tbSaturation.MaxValue = maxvalue;
tbSaturation.MinValue = minvalue;
tbSaturation.Tag = flag;
tbSaturation.Value = value;
videoSource.GetVideoProperty(VideoProcAmpProperty.Gamma, out value, out flag);
videoSource.GetVideoPropertyRange(VideoProcAmpProperty.Gamma, out minvalue, out maxvalue, out stepSize, out defalutvalue, out flag);
tbGamma.MaxValue = maxvalue;
tbGamma.MinValue = minvalue;
tbGamma.Tag = flag;
tbGamma.Value = value;
//videoSource.GetVideoProperty(VideoProcAmpProperty.ColorEnable, out value, out flag);
}
private void setValue(VideoProcAmpProperty vpap)
{
int value = 0;
VideoProcAmpFlags flag;
switch (vpap)
{
case VideoProcAmpProperty.Brightness:
value = Convert.ToInt32(tbBrightness.Value);
flag=(VideoProcAmpFlags)tbBrightness.Tag;
videoSource.SetVideoProperty(VideoProcAmpProperty.Brightness, value, flag);
break;
case VideoProcAmpProperty.Contrast:
value = Convert.ToInt32(tbContrast.Value);
flag = (VideoProcAmpFlags)tbContrast.Tag;
videoSource.SetVideoProperty(VideoProcAmpProperty.Contrast, value, flag);
break;
case VideoProcAmpProperty.Hue:
value = Convert.ToInt32(tbHue.Value);
flag = (VideoProcAmpFlags)tbHue.Tag;
videoSource.SetVideoProperty(VideoProcAmpProperty.Hue, value, flag);
break;
case VideoProcAmpProperty.Saturation:
value = Convert.ToInt32(tbSaturation.Value);
flag = (VideoProcAmpFlags)tbSaturation.Tag;
videoSource.SetVideoProperty(VideoProcAmpProperty.Saturation, value, flag);
break;
case VideoProcAmpProperty.Gamma:
value = Convert.ToInt32(tbGamma.Value);
flag = (VideoProcAmpFlags)tbGamma.Tag;
videoSource.SetVideoProperty(VideoProcAmpProperty.Gamma, value, flag);
break;
}
}
private void SetCmbDevice()
{
if (videoDevices.Count == 0) return;
string name = "";
List<DeviceItem> list = new List<DeviceItem>();
for (int i = 0; i < videoDevices.Count; i++)
{
name = videoDevices[0].Name;
DeviceItem d = new DeviceItem()
{
index = i,
text = name,
};
list.Add(d);
}
cmbCamera.DisplayMember = "text";
cmbCamera.ValueMember = "index";
cmbCamera.DataSource = list;
}
private void SetCmbPixelVideo(VideoCapabilities[] _videoCapabilities)
{
if (_videoCapabilities.Length == 0) return;
string text = "";
List<VideoPixelItem> list = new List<VideoPixelItem>();
for (int i = 0; i < _videoCapabilities.Length; i++)
{
text = string.Format("{0} x {1}", _videoCapabilities[i].FrameSize.Width, _videoCapabilities[i].FrameSize.Height);
VideoPixelItem item = new VideoPixelItem()
{
index = i,
text = text,
width = _videoCapabilities[i].FrameSize.Width
};
list.Add(item);
}
var listSort= list.OrderByDescending(p => p.width).ToList();
cmbPixelVideo.DataSource = listSort;
cmbPixelVideo.DisplayMember = "text";
cmbPixelVideo.ValueMember = "index";
}
private void FrmCamera_Load(object sender, EventArgs e)
{
try
{
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
this.SetStyle(ControlStyles.DoubleBuffer, true);
// 枚举所有视频输入设备
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
imgData = new ImageData();
if (videoDevices.Count == 0)
throw new ApplicationException();
SetCmbDevice();
cmbCamera.SelectedIndex = 0;
var _cameraDevice = new VideoCaptureDevice(videoDevices[0].MonikerString);
SetCmbPixelVideo(_cameraDevice.VideoCapabilities);
CameraConn();
videoSourcePlayer1.Start();
//int cameraIndex = Convert.ToInt32(IniConfigHelper.ReadSetting(ini_file, "Option", "CameraIndex", "0"));
//cmbCamera.SelectedValue = cameraIndex;
SetPatientInfo();
SetRangValue();
Result = new DialogResponse() { code = -1 };
//this.Refresh();
//this.Invalidate();
if (requset.VideoDevice == -1)
{
MessageBox.Show("请在客户端参数中设置视频设备");
}
else
{
cmbCamera.SelectedValue = requset.VideoDevice;
}
if (requset.VideoPixel == -1)
{
MessageBox.Show("请在客户端参数中设置视频分辨率");
}
else {
cmbPixelVideo.SelectedValue = requset.VideoPixel;
}
}
catch (ApplicationException)
{
//cmbCamera.Items.Add("没有本地设备");
videoDevices = null;
}
catch (Exception ex)
{
//cmbCamera.Items.Add("没有本地设备");
videoDevices = null;
MessageBox.Show("初始化失败:" + ex.Message);
}
}
private void cmbCamera_SelectedValueChanged(object sender, EventArgs e)
{
try
{
if (this.cmbCamera.SelectedValue == null) return;
if (videoDevices.Count == 0 || videoDevices.Count == 1) return;
int index = Convert.ToInt32(this.cmbCamera.SelectedValue);
var _cameraDevice = new VideoCaptureDevice(videoDevices[index].MonikerString);
SetCmbPixelVideo(_cameraDevice.VideoCapabilities);
}
catch (Exception ex)
{
MessageBox.Show("切换摄像头失败:" + ex.Message);
}
}
private void cmbPixelVideo_SelectedValueChanged(object sender, EventArgs e)
{
try
{
if (videoSourcePlayer1.VideoSource == null) return;
//if (cmbCamera.Enabled) return;
VideoCaptureDevice videoSource = (VideoCaptureDevice)videoSourcePlayer1.VideoSource;
int index = Convert.ToInt32(cmbPixelVideo.SelectedValue);
videoSource.VideoResolution = videoSource.VideoCapabilities[index];
videoSourcePlayer1.VideoSource = videoSource;
if (videoSourcePlayer1.IsRunning)
{
videoSourcePlayer1.SignalToStop();
videoSourcePlayer1.WaitForStop();
}
videoSourcePlayer1.Start();
}
catch (Exception ex)
{
MessageBox.Show("切换分辨率异常:" + ex.Message);
}
}
private void btnDeviceProperty_Click(object sender, EventArgs e)
{
VideoCaptureDevice videoSource = (VideoCaptureDevice)videoSourcePlayer1.VideoSource;
videoSource.DisplayPropertyPage(IntPtr.Zero);
}
private void tbBrightness_ValueChanged(object sender, EventArgs e)
{
try
{
setValue(VideoProcAmpProperty.Brightness);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void tbContrast_ValueChanged(object sender, EventArgs e)
{
try
{
setValue(VideoProcAmpProperty.Contrast);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void tbHue_ValueChanged(object sender, EventArgs e)
{
try
{
setValue(VideoProcAmpProperty.Hue);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void tbSaturation_ValueChanged(object sender, EventArgs e)
{
try
{
setValue(VideoProcAmpProperty.Saturation);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void tbGamma_ValueChanged(object sender, EventArgs e)
{
try
{
setValue(VideoProcAmpProperty.Gamma);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnPhotoGraph_Click(object sender, EventArgs e)
{
try
{
if (!videoSourcePlayer1.IsRunning) return;
this.btnPhotoGraph.Enabled = false;
Bitmap imgMap = this.videoSourcePlayer1.GetCurrentVideoFrame();
int row = dgvImg.Rows.Add();
dgvImg.Rows[row].Cells[0].Value = 1;
dgvImg.Rows[row].Cells[1].Value = imgMap;
dgvImg.Rows[row].Cells[2].Value = getFilePath();
dgvImg.Refresh();
}
catch (Exception ex)
{
MessageBox.Show("采集图像异常:" + ex.Message);
}
finally
{
this.btnPhotoGraph.Enabled = true;
}
}
private string getFilePath()
{
DateTime dt = DateTime.Now;
string year = dt.Year.ToString();
string month = dt.Month.ToString();
string day = dt.Day.ToString();
string file_path = ImageHelper.GetLoaclPath(requset.ImagePath, requset.AsbitemName);
string fileName = string.Format("{0}\\{1}_{2}.{3}", file_path, requset.BarCode, "0001", "jpg");
if (File.Exists(fileName))
{
for (int i = 1; i < requset.Total; i++)
{
int index = fileName.IndexOf("_");
string idStr = fileName.Substring(index + 1, 4);
int id = Convert.ToInt32(idStr);
id++;
string temp = Convert.ToString(id).PadLeft(4, '0');
fileName = string.Format("{0}\\{1}_{2}.{3}", file_path, requset.BarCode, temp, "jpg");
if (!File.Exists(fileName)) break;
}
}
return fileName;
}
private void btnClose_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrEmpty(requset.ImagePath))
{
Result.message = "本地图片目录未设置";
return;
}
if (string.IsNullOrEmpty(requset.BarCode))
{
Result.message = "条形码不能为空";
return;
}
if (videoSourcePlayer1.IsRunning)
{
videoSourcePlayer1.SignalToStop();
videoSourcePlayer1.WaitForStop();
}
List<ImageResponse> list = new List<ImageResponse>();
foreach(DataGridViewRow row in dgvImg.Rows)
{
Bitmap imgMap = (Bitmap)row.Cells[1].Value;
string fileName = row.Cells[2].Value.ToString();
ImageCodecInfo Encoder = ImageHelper.GetEncoder(ImageFormat.Jpeg);
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, requset.Quality);
myEncoderParameters.Param[0] = myEncoderParameter;
if (File.Exists(fileName)) File.Delete(fileName);
imgMap.Save(fileName, Encoder, myEncoderParameters);
System.Drawing.Image imge = System.Drawing.Image.FromFile(fileName);
ImageResponse imageResponse = new ImageResponse()
{
Image = ImageHelper.ConvertImageToBase64(imge),
FilePath = fileName,
ImageFormat ="jpg"
};
list.Add(imageResponse);
}
Result=new DialogResponse() {
code = 1,
data= list
};
this.DialogResult= DialogResult.OK;
}
catch (Exception ex)
{
MessageBox.Show("采集图像异常:" + ex.Message);
}
}
private void btnImageShare_Click(object sender, EventArgs e)
{
if (dgvImg.Rows.Count == 0) return;
Bitmap imgMap = (Bitmap)dgvImg.Rows[dgvImg.CurrentCell.RowIndex].Cells[1].Value;
string fileName = dgvImg.Rows[dgvImg.CurrentCell.RowIndex].Cells[2].Value.ToString();
MemoryStream mstr = new MemoryStream();
imgMap.Save(mstr, ImageFormat.Jpeg);
FrmImage frm=new FrmImage(mstr, fileName);
frm.ShowDialog();
if (frm.DialogResult != DialogResult.OK) return;
dgvImg.Rows[dgvImg.CurrentCell.RowIndex].Cells[1].Value = ImageData.Image;
}
private void btnImageContrast_Click(object sender, EventArgs e)
{
try
{
//string path_current = string.Format("{0}\\ShenTun\\ComparisonPicture\\{1}\\{2}\\{3}", System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day);
string path_current = string.Format("{0}\\ShenTun\\ComparisonPicture", System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments));
if (!Directory.Exists(path_current))
{
Directory.CreateDirectory(path_current);
}
string path1 = string.Format("{0}\\{1}.{2}", path_current, "p1", "jpg");
if(File.Exists(path1))File.Delete(path1);
string path2 = string.Format("{0}\\{1}.{2}", path_current, "p2", "jpg");
if (File.Exists(path2)) File.Delete(path2);
if (dgvImg.Rows.Count == 0) return;
int count = 0;
for (int i = 0; i < dgvImg.Rows.Count; i++) {
if (dgvImg.Rows[i].Cells[0].Value.ToString().Equals("1")) count++;
}
if (count != 2)
{
MessageBox.Show("图片数量必须为2张选中");
return;
}
count = 0;
foreach (DataGridViewRow row in dgvImg.Rows)
{
if (row.Cells[0].Value.ToString().Equals("1") && count == 0)
{
Bitmap imgMap = (Bitmap)row.Cells[1].Value;
imgMap.Save(path1, ImageFormat.Jpeg);
count++;
}
else if(row.Cells[0].Value.ToString().Equals("1") && count != 0)
{
Bitmap imgMap = (Bitmap)row.Cells[1].Value;
imgMap.Save(path2, ImageFormat.Jpeg);
}
}
FrmComparison frm = new FrmComparison(path1, path2);
frm.ShowDialog();
}
catch(Exception ex)
{
MessageBox.Show("操作图像比对异常:" + ex.Message);
}
}
}
}