特别注意需要引入 库文 ZXing
可跳转:
记录【WinForm】C#学习使用ZXing.Net生成条码过程_c# zxing-CSDN博客
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
using System.Drawing;
using ZXing;
using System.Diagnostics;
using ImageMagick;
using System.IO;
namespace ZXing1
{
class Program
{
static void Main(string[] args)
{
// 创建一个 Stopwatch 实例
Stopwatch stopwatch = new Stopwatch();
// 启动计时
stopwatch.Start();
GetSN();
// 获取总用时
TimeSpan timeElapsed = stopwatch.Elapsed;
Console.WriteLine($"视觉识别总用时: {timeElapsed.TotalSeconds} S");
Console.Read();
}
static List<string> CheckRETURN = new List<string>();
public static void GetSN()
{
string outLab = Environment.CurrentDirectory + "\\WIN_1.jpg";
string SnRetu = Getlab(outLab);
Console.WriteLine(SnRetu);
}
public static void Caijian(string inputPath, string outputPath, int x, int y, int width, int height)
{
// 加载原始图片
using (Bitmap originalBitmap = new Bitmap(inputPath))
{
//Bitmap rotatedBitmap = RotateImage(originalBitmap, 90);//根据需求旋转90
// 定义裁剪区域(x, y, width, height)
Rectangle cropArea = new Rectangle(x, y, width, height); // 修改这些值以适应你的需求
// 创建裁剪后的图片
using (Bitmap croppedBitmap = CropImage(originalBitmap, cropArea))
{
// 保存裁剪后的图片
croppedBitmap.Save(outputPath);
//blackwrit(outputPath, outputPath);
}
}
}
static Bitmap CropImage(Bitmap source, Rectangle cropArea)
{
// 确保裁剪区域在源图像范围内
if (cropArea.X < 0 || cropArea.Y < 0 || cropArea.Right > source.Width || cropArea.Bottom > source.Height)
{
throw new ArgumentException("裁剪区域超出了图像边界");
}
Bitmap croppedImage = new Bitmap(cropArea.Width, cropArea.Height);
using (Graphics g = Graphics.FromImage(croppedImage))
{
g.DrawImage(source, new Rectangle(0, 0, cropArea.Width, cropArea.Height),
cropArea, GraphicsUnit.Pixel);
}
return croppedImage;
}
public static string Getlab(string imagePath)
{
var barcodeBitmap = (Bitmap)System.Drawing.Image.FromFile(imagePath);
var barcodeReader = new BarcodeReader
{
Options = new ZXing.Common.DecodingOptions
{
// 支持多种条形码格式,包括二维码
PossibleFormats = new List<BarcodeFormat>
{
BarcodeFormat.CODE_128, BarcodeFormat.CODE_39, BarcodeFormat.EAN_13,
BarcodeFormat.EAN_8, BarcodeFormat.UPC_A, BarcodeFormat.UPC_E,
BarcodeFormat.QR_CODE, BarcodeFormat.DATA_MATRIX
}
}
};
var result = barcodeReader.DecodeMultiple(barcodeBitmap);
string strlab = string.Empty;
if (result != null)
{
strlab = result[0].ToString();
int op = 0;
foreach (var item in result)
{
Console.WriteLine("识别内容: " + item.Text);
DrawBarcodeRectangle(barcodeBitmap, item);
op++;
}
Console.WriteLine($"共识别条码:{op}个");
}
else
{
strlab = "FAIL";
Console.WriteLine("未能识别内容");
}
Console.WriteLine("========================模板识别========================");
return strlab;
}
public static void DrawBarcodeRectangle(Bitmap frame, ZXing.Result result)
{
// 获取条码位置的四个角点
var points = result.ResultPoints;
using (Graphics g = Graphics.FromImage(frame))
{
Pen pen = new Pen(Color.Green, 10); // 设置绘制矩形的颜色和宽度
if (points.Length == 2)
{
// 对于只检测到两个点的情况(例如一维条码),画一条线
g.DrawLine(pen, new PointF(points[0].X, points[0].Y), new PointF(points[1].X, points[1].Y));
float x = Math.Min(points[0].X, points[1].X);
float y = Math.Min(points[0].Y, points[1].Y);
g.DrawString(result.Text, new Font("Arial", 32, FontStyle.Bold), new SolidBrush(Color.Red), x,y); // 将文字显示在矩形框的上方
}
else if (points.Length >= 4)
{
// 如果有四个点,画出矩形框
g.DrawPolygon(pen, new PointF[]
{
new PointF(points[0].X, points[0].Y),
new PointF(points[1].X, points[1].Y),
new PointF(points[2].X, points[2].Y),
new PointF(points[3].X, points[3].Y)
});
}
}
// 显示结果图像
ShowImage(frame);
}
// 用画图工具显示图片
public static void ShowImage(Bitmap image)
{
// 使用 System.Windows.Forms.PictureBox 或者你喜好的方法来显示图像
using (var form = new System.Windows.Forms.Form())
{
form.Text = "Barcode Detected";
var pictureBox = new System.Windows.Forms.PictureBox
{
Image = image,
Dock = System.Windows.Forms.DockStyle.Fill,
SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
};
form.Controls.Add(pictureBox);
form.ShowDialog();
}
}
}
}
使用效果:
如果对条码不知道裁剪的方法如何使用可阅读我的另一篇文章:
c# 视觉识别图片文字_c# 识别二维码-CSDN博客