代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;
using ZXing;
using System.IO;
using System.Diagnostics;
namespace WindowsFormsApp6
{
public partial class Form1 : Form
{
FilterInfoCollection videoDevices;//摄像头设备集合
VideoCaptureDevice videoSource;//捕获设备源
const int delaytime = 500; //扫码间隔时间 ms
int totalTime; //扫码最长时间
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
comboBox1.Items.Clear();
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);//获取拍照设备列表
for (int i = 0; i < videoDevices.Count; i++)
comboBox1.Items.Add(videoDevices[i].Name);
comboBox1.Text = comboBox1.Items[0].ToString();
}
private void button2_Click(object sender, EventArgs e)
{
if (comboBox1.Text == null)
return;
ShutCamera();
videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
videoSourcePlayer1.VideoSource = videoSource;
videoSourcePlayer1.Start();
textBox1.Text = "开始识读";
timer1.Interval = delaytime;
timer1.Start();
totalTime = 0;
}
private void button3_Click(object sender, EventArgs e)
{
ShutCamera();
timer1.Stop();
}
// 关闭并释放摄像头
public void ShutCamera()
{
if (videoSourcePlayer1.VideoSource != null)
{
videoSourcePlayer1.SignalToStop();
videoSourcePlayer1.WaitForStop();
videoSourcePlayer1.VideoSource = null;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (totalTime >= 20000)// 最多读20秒
{
timer1.Stop();
ShutCamera();
textBox1.Text = "未识别到二维码";
return;
}
Bitmap barcodeImage;
barcodeImage = videoSourcePlayer1.GetCurrentVideoFrame();//拍摄
if (barcodeImage != null)
{
// 识读条形码
BarcodeReader reader = new BarcodeReader();
reader.Options.CharacterSet = "UTF-8";
reader.Options.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.CODE_128 };
Result resultBarcode = reader.Decode(barcodeImage);
if (resultBarcode != null)
{
textBox1.Text = "";
textBox1.AppendText(resultBarcode.Text);
timer1.Stop();
ShutCamera();
return;
}
}
totalTime += delaytime;
textBox1.Text = totalTime.ToString() + " ms";
//if (totalTime == 3000)
// picture.Save("测试图片.bmp");
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
string searchNum = textBox1.Text.Trim();
//string folderPath = "C:\Users\HUI.CAI\Desktop\HELLO"; // 指定要搜索的文件夹路径
string folderPath;
folderPath = textBox2.Text.Trim();
foreach (string filePath in Directory.GetFiles(folderPath, "*.pdf"))
{
string fileName = Path.GetFileNameWithoutExtension(filePath);
if (fileName.Length >= 8)
{
string firstEightDigits = fileName.Substring(0, 8);
if (firstEightDigits == searchNum)
{
Process.Start(filePath);
return; // 打开第一个匹配的pdf文件后退出循环
}
}
}
MessageBox.Show("未找到匹配的pdf文件。");
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text.Length > 9)
{
textBox1.Text = textBox1.Text.Substring(0, 9);
}
else if (textBox1.Text.Length == 9)
{
textBox1.Text = textBox1.Text.Substring(0, 8);
}
}
}
}