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 button4_Click(object sender, EventArgs e) { string searchNum = textBox1.Text.Trim(); searchNum = searchNum.Substring(0, 8); // 只取前8位 string 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文件。"); } 修改为扫描出条形码在文本框中只显示8位并打开第一个匹配的pdf文件后退出循环
时间: 2024-03-26 21:39:50 浏览: 173
好的,我理解你的问题了。请将代码修改为以下内容:
```
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 = " ";
string barcodeText = resultBarcode.Text.Length >= 8 ? resultBarcode.Text.Substring(0, 8) : resultBarcode.Text;
textBox1.AppendText(barcodeText);
timer1.Stop();
ShutCamera();
string 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 == barcodeText)
{
Process.Start(filePath);
return; // 打开第一个匹配的pdf文件后退出循环
}
}
}
MessageBox.Show("未找到匹配的pdf文件。");
return;
}
}
totalTime += delaytime;
textBox1.Text = totalTime.ToString() + " ms";
//if (totalTime == 3000)
// picture.Save("测试图片.bmp");
}
```
修改后的代码会先判断识别到的条形码长度是否超过8位,如果超过则只取前8位,然后在文本框中显示。接着会遍历指定文件夹中的所有PDF文件,找到第一个文件名前8位与条形码匹配的文件并打开,然后退出循环。如果没有找到匹配的文件,则弹出提示框。
阅读全文
相关推荐
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)