欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > 菜鸟驿站二维码/一维码 取件识别功能

菜鸟驿站二维码/一维码 取件识别功能

2024/11/29 21:12:31 来源:https://blog.csdn.net/weixin_56830970/article/details/143849038  浏览:    关键词:菜鸟驿站二维码/一维码 取件识别功能

特别注意需要引入 库文  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博客

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com