tijian_tieying/web/dccdc.Selfhelp/frm_CodeReader.cs
2025-02-20 12:14:39 +08:00

196 lines
6.4 KiB
C#

using AForge.Video;
using AForge.Video.DirectShow;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Windows.Forms;
using ZXing;
namespace dccdc.Selfhelp
{
public partial class frm_CodeReader : Form
{
private int time = 8;
public frm_CodeReader()
{
InitializeComponent();
}
public frm_CodeReader(string msg):this()
{
label1.Text = msg;
}
//private FilterInfoCollection videoDevices;
private void frm_CodeReader_Load(object sender, EventArgs e)
{
//videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
this.BackColor = Color.FromArgb(71, 216, 233);
int port = 0;
this.TopMost = true;
string sport = System.Configuration.ConfigurationManager.AppSettings["commPort"];
if (!int.TryParse(sport, out port))
{
port = 1;
}
try
{
extend.close_comm();
extend.open_comm(port);
extend.send_comm(11);
}
catch (Exception ex)
{
Status s = new Status(ex.Message);
s.ShowDialog();
this.Close();
}
start_Click(null, null);
}
private VideoCaptureDevice videoSource;
private void start_Click(object sender, EventArgs e)
{
//视频捕获设备
videoSource = new VideoCaptureDevice(extend.Config.AppSettings.Settings["vid"].Value);
//捕获到新画面时触发
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
//先关一下,下面再打开。避免重复打开的错误
CloseVideoSource();
//设置画面大小
string fbl = extend.Config.AppSettings.Settings["vsize"].Value;
//videoSource.DesiredFrameSize = new Size(int.Parse(fbl.Split('x')[0]), int.Parse(fbl.Split('y')[0]));
//启动视频组件
var videoCapabilities = videoSource.VideoCapabilities;
int h = int.Parse(fbl.Split('x')[1]);
int w = int.Parse(fbl.Split('x')[0]);
//this.Width = w;
//this.Height = h;
foreach (var vc in videoCapabilities)
{
if (vc.FrameSize.Width == w && vc.FrameSize.Height == h)
videoSource.VideoResolution = vc;
}
videoSource.Start();
}
private void CloseVideoSource()
{
if (!(videoSource == null))
if (videoSource.IsRunning)
{
videoSource.SignalToStop();
videoSource = null;
}
}
/// <summary>
/// 全局变量,保存每一次捕获的图像
/// </summary>
Bitmap img = null;
string code = "";
public string CodeStr { get { return code; } }
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
img = (Bitmap)eventArgs.Frame.Clone();
Bitmap bm = new Bitmap(768, 1024);
Graphics g = Graphics.FromImage(bm);
g.FillRectangle(Brushes.White, new Rectangle(0, 0, 768, 1024));
img.RotateFlip(RotateFlipType.Rotate270FlipXY);
g.DrawImage(img, new Point((768 - img.Width) / 2, (1024 - img.Height) / 2));
MemoryStream ms = new MemoryStream();
img.Save(ms, ImageFormat.Bmp);
ms.Position = 0;
var bt = ms.ToArray();
var imgcode = Image.FromStream(ms);
LuminanceSource source = new RGBLuminanceSource(bt, img.Width, img.Height);
BinaryBitmap bitmap = new BinaryBitmap(new ZXing.Common.HybridBinarizer(source));
Result result = null;
try
{
//开始解码
result = new MultiFormatReader().decode(bitmap);
}
catch (ReaderException re)
{
Status s = new Status(re.Message);
s.ShowDialog();
s.Close();
this.DialogResult = DialogResult.Cancel;
}
if (result != null)
{
if (code == result.Text)
{
DialogResult = DialogResult.OK;
}
code = result.Text;
}
var imgzz = Image.FromFile(Application.StartupPath + "\\image\\zz.png");
g.DrawString(time.ToString("00"), new Font("微软雅黑", 36), Brushes.Yellow, new Point(650, 15));
g.DrawImage(imgzz, new Point(0, 0));
pictureBox1.Image = bm;
}
private void btn_test_Click(object sender, EventArgs e)
{
start_Click(sender, e);
}
/*
private void getCamList()
{
try
{
//AForge.Video.DirectShow.FilterInfoCollection 设备枚举类
var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
//清空列表框
comboBox1.Items.Clear();
if (videoDevices.Count == 0)
throw new ApplicationException();
//全局变量,标示设备摄像头设备是否存在
DeviceExist = true;
//加入设备
foreach (FilterInfo device in videoDevices)
{
comboBox1.Items.Add(device.Name);
}
//默认选择第一项
comboBox1.SelectedIndex = 0;
}
catch (ApplicationException)
{
DeviceExist = false;
comboBox1.Items.Add("未找到可用设备");
}
}*/
private void frm_CodeReader_FormClosing(object sender, FormClosingEventArgs e)
{
CloseVideoSource();
extend.send_comm(12);
extend.close_comm();
}
private void timer1_Tick(object sender, EventArgs e)
{
time--;
if (time <= 0)
{
this.Close();
}
}
}
}