using CameraCardDemo.Infrastructure; using OpenCvSharp; using OpenCvSharp.Extensions; using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Globalization; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace CameraCardDemo { public partial class MainForm : Form { private CancellationTokenSource _cts = new CancellationTokenSource(); private List _cameras = new List(); private VideoCapture _videoCapture; private Rectangle _rect; private Pen _pen; private int _cvrStatus = 0; public MainForm() { InitializeComponent(); } private void MainFrom_Load(object sender, EventArgs e) { foreach (Control item in this.idcGroupBox.Controls) { if (item is Label && item.Name.EndsWith("Value")) { (item as Label).Text = ""; } } this.CreateRect(); Task.Run(async () => { while (!this._cts.IsCancellationRequested) { this.ScanCameras(); await Task.Delay(1000); } }); Task.Run(async () => { while (!this._cts.IsCancellationRequested) { this.ScanCardReader(); await Task.Delay(1000); } }); Task.Run(() => { while (!this._cts.IsCancellationRequested) { try { if (this._cvrStatus == 1 && CVRSDK.CVR_Authenticate() == 1 && CVRSDK.CVR_Read_FPContent() == 1) { var nameBytes = new byte[128]; var length = 128; CVRSDK.GetPeopleName(ref nameBytes[0], ref length); var name = System.Text.Encoding.GetEncoding("GB2312").GetString(nameBytes); this.Invoke(new Action(() => { this.FillData(); this.log.AppendText(name); this.log.AppendText(Environment.NewLine); })); } } catch (Exception ex) { this.Invoke(new Action(() => { this.log.AppendText(ex.Message); this.log.AppendText(Environment.NewLine); })); } Thread.Sleep(600); } }); } private void CreateRect() { this._pen = new Pen(Color.Green); this._pen.DashStyle = DashStyle.Dash; var width = 351 / 2; var height = 448 / 2; this._rect = new Rectangle(this.cameraRender.Width / 2 - width / 2, this.cameraRender.Height / 2 - height / 2, 351 / 2, 448 / 2); } private void ScanCameras() { this.Invoke(new Action(() => { var cameras = CameraHelper.GetDevices(); if (!this._cameras.SequenceEqual(cameras)) { this._cameras = cameras; this.cameraSelector.Items.Clear(); this.cameraSelector.Text = string.Empty; foreach (var item in _cameras) { this.cameraSelector.Items.Add(item); } if (this.cameraSelector.Items.Count > 0) { this.cameraSelector.SelectedIndex = 0; } else { this.cameraSelector.Text = "没有检测到USB摄像头"; } } })); } private void ScanCardReader() { for (var usbPort = 1001; usbPort <= 1016; usbPort++) { this._cvrStatus = CVRSDK.CVR_InitComm(usbPort); if (this._cvrStatus == 1) { break; } } if (this._cvrStatus != 1) { this.Invoke(new Action(() => { this.idcGroupBox.Text = $"身份证(设备未连接)"; })); } else { this.Invoke(new Action(() => { this.idcGroupBox.Text = $"身份证(设备已连接)"; })); } } public void FillData() { try { byte[] imgData = new byte[40960]; int length = 40960; CVRSDK.GetBMPData(ref imgData[0], ref length); using (var myStream = new MemoryStream()) { for (int i = 0; i < length; i++) { myStream.WriteByte(imgData[i]); } Image myImage = Image.FromStream(myStream); idcPicture.Image = myImage; } byte[] name = new byte[128]; length = 128; CVRSDK.GetPeopleName(ref name[0], ref length); byte[] cnName = new byte[128]; length = 128; CVRSDK.GetPeopleChineseName(ref cnName[0], ref length); byte[] number = new byte[128]; length = 128; CVRSDK.GetPeopleIDCode(ref number[0], ref length); byte[] peopleNation = new byte[128]; length = 128; CVRSDK.GetPeopleNation(ref peopleNation[0], ref length); byte[] peopleNationCode = new byte[128]; length = 128; CVRSDK.GetNationCode(ref peopleNationCode[0], ref length); byte[] validtermOfStart = new byte[128]; length = 128; CVRSDK.GetStartDate(ref validtermOfStart[0], ref length); byte[] birthday = new byte[128]; length = 128; CVRSDK.GetPeopleBirthday(ref birthday[0], ref length); byte[] address = new byte[128]; length = 128; CVRSDK.GetPeopleAddress(ref address[0], ref length); byte[] validtermOfEnd = new byte[128]; length = 128; CVRSDK.GetEndDate(ref validtermOfEnd[0], ref length); byte[] signdate = new byte[128]; length = 128; CVRSDK.GetDepartment(ref signdate[0], ref length); byte[] sex = new byte[128]; length = 128; CVRSDK.GetPeopleSex(ref sex[0], ref length); byte[] samid = new byte[128]; CVRSDK.CVR_GetSAMID(ref samid[0]); bool bCivic = true; byte[] certType = new byte[32]; length = 32; CVRSDK.GetCertType(ref certType[0], ref length); string strType = System.Text.Encoding.ASCII.GetString(certType); int nStart = strType.IndexOf("I"); if (nStart != -1) bCivic = false; var model = new IdcModel { Type = bCivic ? "居民身份证" : "外国人永居证", Name = GetString(name), CnName = bCivic ? "" : GetString(name), Sex = GetString(sex), Nation = GetString(peopleNation), NationCode = GetString(peopleNationCode), Birthday = GetDataString(GetString(birthday)), IdCardNo = GetString(number), Address = bCivic ? GetString(address) : "", Department = GetString(signdate), ValidDateStart = GetString(validtermOfStart), ValidDateEnd = GetString(validtermOfEnd), SamID = GetString(samid), }; //using (var ms = new MemoryStream()) //{ // idcPicture.Image.Save(ms, ImageFormat.Bmp); // model.Image = Convert.ToBase64String(ms.ToArray()); //} if (bCivic) { this.idcAddressLabel.Visible = true; this.idcAddressValue.Visible = true; this.idcCnNameLabel.Visible = false; this.idcCnNameValue.Visible = false; this.idcAddressLabel.Visible = true; this.idcAddressValue.Visible = true; this.idcAddressValue.Text = model.Address; this.idcNameLable.Text = "民族:"; this.idcNationCodeLabel.Text = "民族代码:"; this.idcIdCardNoLabel.Text = "身份证号"; } else { this.idcAddressLabel.Visible = false; this.idcAddressValue.Visible = false; this.idcCnNameLabel.Visible = true; this.idcCnNameValue.Visible = true; this.idcAddressLabel.Visible = false; this.idcAddressValue.Visible = false; this.idcCnNameValue.Text = model.CnName; this.idcNameLable.Text = "国籍:"; this.idcNationCodeLabel.Text = "国籍代码:"; this.idcIdCardNoLabel.Text = "证件号码"; } this.idcNameValue.Text = model.Name; this.idcSexValue.Text = model.Sex; this.idcNationValue.Text = model.Nation; this.idcNationCodeValue.Text = model.NationCode; this.idcBirthdayValue.Text = model.Birthday; this.idcIdCardNoValue.Text = model.IdCardNo; this.idcDepartmentValue.Text = model.Department; this.idcValidDateValue.Text = $"{ GetDataString(model.ValidDateStart, '.')}-{GetDataString(model.ValidDateEnd, '.')}"; this.idcSamIDValue.Text = model.SamID; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private string GetString(byte[] data) { return Encoding.GetEncoding("GB2312").GetString(data).Replace("\0", "").Trim(); } private string GetDataString(string value, char sep = '-') { return DateTime.ParseExact(value, "yyyyMMdd", CultureInfo.InvariantCulture).ToString($"yyyy{sep}MM{sep}dd"); } private void cameraSelector_SelectedIndexChanged(object sender, EventArgs e) { Release(); this._videoCapture = new VideoCapture((sender as ComboBox).SelectedIndex); if (this._videoCapture.IsOpened()) { Task.Run(() => { while (this._videoCapture.IsOpened()) { try { using (var mat = new Mat()) { this._videoCapture.Read(mat); this.cameraRender.Image = mat.ToBitmap(); } } catch { this._videoCapture.Release(); break; } } }); } } private void shotBtn_Click(object sender, EventArgs e) { var bitmap = this.ClipImage(); bitmap = this.ResizeImage(bitmap); pictureBox1.Image = bitmap; var eps = new EncoderParameters(1); eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 70L); bitmap.Save("temp.jpeg", ImageFormat.Jpeg); } private Bitmap ClipImage() { var image = this.cameraRender.Image; var width = this._rect.Width * image.Width / this.cameraRender.Width; var height = this._rect.Height * width / this._rect.Width; var bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb); bitmap.SetResolution(350, 350); using (var g = Graphics.FromImage(bitmap)) { g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), new Rectangle((image.Width - bitmap.Width) / 2, (image.Height - bitmap.Height) / 2, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel); } return bitmap; } private Bitmap ResizeImage(Bitmap image) { var width = 358; var height = 441; var bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb); bitmap.SetResolution(350, 350); using (var g = Graphics.FromImage(bitmap)) { g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel); } return bitmap; } private void cameraRender_Paint(object sender, PaintEventArgs e) { e.Graphics.DrawRectangle(this._pen, _rect); } private void Release() { if (this._videoCapture != null && _videoCapture.IsOpened()) { this._videoCapture.Release(); } } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { this._cts.Cancel(); Release(); this._pen.Dispose(); CVRSDK.CVR_CloseComm(); } private void tabControl2_DrawItem(object sender, DrawItemEventArgs e) { } } }