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.
iot/labs/CameraCardDemo/MainForm.cs

220 lines
7.2 KiB

using CameraCardDemo.Infrastructure;
using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CameraCardDemo
{
public partial class MainForm : Form
{
private CancellationTokenSource _cameraScanCts = new CancellationTokenSource();
private List<string> _cameras = new List<string>();
private VideoCapture _videoCapture;
private CancellationToken cancellationToken = new CancellationToken();
private Rectangle _rect;
private Pen _pen;
public MainForm()
{
InitializeComponent();
}
private void MainFrom_Load(object sender, EventArgs e)
{
this.CreateRect();
Task.Run(async () =>
{
while (!this._cameraScanCts.IsCancellationRequested)
{
this.ScanCameras();
await Task.Delay(1000);
}
});
try
{
//读卡器
InitCardReader();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
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 InitCardReader()
{
int iPort, iRetUSB = 0;
for (iPort = 1001; iPort <= 1016; iPort++)
{
iRetUSB = CVRSDK.CVR_InitComm(iPort);
if (iRetUSB == 1)
{
break;
}
}
if (iRetUSB == 1)
{
Debug.WriteLine("读卡器初始化成功");
Task.Run(() =>
{
while (!this.cancellationToken.IsCancellationRequested)
{
if (CVRSDK.CVR_Authenticate() == 1 && CVRSDK.CVR_Read_FPContent() == 1)
{
var name = new byte[128];
var length = 128;
CVRSDK.GetPeopleName(ref name[0], ref length);
MessageBox.Show(System.Text.Encoding.GetEncoding("GB2312").GetString(name));
}
Thread.Sleep(600);
}
});
}
else
{
//MessageBox.Show("读卡器初始化失败");
}
}
private void cameraSelector_SelectedIndexChanged(object sender, EventArgs e)
{
Release();
this._videoCapture = new VideoCapture((sender as ComboBox).SelectedIndex);
if (this._videoCapture.IsOpened())
{
//this.ResizeCamera();
Task.Run(() =>
{
while (this._videoCapture.IsOpened())
{
try
{
using (var mat = new Mat())
{
this._videoCapture.Read(mat);
this.cameraRender.Image = mat.ToBitmap();
}
}
catch (Exception ex)
{
this._videoCapture.Release();
break;
}
}
});
}
}
private void Release()
{
if (this._videoCapture != null && _videoCapture.IsOpened())
{
this._videoCapture.Release();
}
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
this._cameraScanCts.Cancel();
Release();
this._pen.Dispose();
}
private void button1_Click(object sender, EventArgs e)
{
this._cameras.Add("14123");
}
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(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);
}
}
}