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

特别注意需要引入 库文 ZXing

可跳转:

记录【WinForm】C#学习使用ZXing.Net生成条码过程_c# zxing-CSDN博客

cs 复制代码
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博客

相关推荐
暴走约伯11 小时前
【VFX工具链源码拆解 09】终极工业化闭环:基于 ID 规则驱动的全自动场景建档、骨骼挂载与男转女场景迁移
unity·c#·游戏引擎·材质
用户788477316341 天前
WinForms 迁移 WPF:Upgrade Assistant 覆盖 30%,剩下 70% 我手写了六周
c#
寂寞的船1 天前
【Unity知识速递】第13期 Unity 内存泄漏排查实战:从 Memory Profiler 到 Lua 引用泄漏的生产级全链路诊断
unity·junit·c#·游戏引擎·lua
用户3721574261352 天前
C# 提取 PDF 附件:包括文档附件和页面注释附件
c#
寂寞的船2 天前
【Unity Bug挑战】第12期 Awaitable 二次 await 死锁之谜 —— 为什么同一个异步操作等两次就卡死?
unity·c#·游戏引擎·bug
huaqianzkh4 天前
WinForm C#:循环修改控件UI闪烁卡顿?SuspendLayout / ResumeLayout 与 BeginUpdate 优化
开发语言·ui·c#
用户3721574261354 天前
C# 实现 Excel 单元格保护:锁定公式并允许编辑指定区域
c#·ai编程·编程语言
公子小六5 天前
基于.NET的Windows窗体编程之WinForms数据持久化(1)
windows·microsoft·c#·.net·winforms
三雷科技5 天前
第6章第4节:自定义分词器
开发语言·c#
zhanghaha13145 天前
C# 系列教程_12 字符串 string 保姆级系统教程(面向新手,大量示例 + 原理讲解)
开发语言·数据库·c#