c# 视觉识别图片文字 二维码

 识别字体的方法和需要引入的类库:

1.二维码识别  插件 ZXing.Net 
using System;
using System.Drawing; // 如果你使用的是System.Drawing.Common
using ZXing;

class Program
{
    static void Main()
    {
        string imagePath = "path_to_your_qr_code_image.png";
        var barcodeBitmap = (Bitmap)Image.FromFile(imagePath);
        var barcodeReader = new BarcodeReader();
        var result = barcodeReader.Decode(barcodeBitmap);

        if (result != null)
        {
            Console.WriteLine("二维码内容: " + result.Text);
        }
        else
        {
            Console.WriteLine("未能识别二维码内容");
        }
    }
}

2.识别字符 插件:Tesseract
using System;
using System.Drawing;
using Tesseract;

class Program
{
    static void Main()
    {
        string imagePath = "path_to_your_image.png";

        // 确保已下载并引用了 Tesseract 的语言数据文件(.traineddata)
        string tessdataPath = "path_to_tessdata"; // 通常包含 "eng.traineddata" 等文件

        using (var engine = new TesseractEngine(tessdataPath, "eng", EngineMode.Default))
        {
            using (var img = Pix.LoadFromFile(imagePath))
            {
                using (var page = engine.Process(img))
                {
                    Console.WriteLine("识别结果: " + page.GetText());
                    Console.WriteLine("置信度: " + page.GetMeanConfidence());
                }
            }
        }
    }
}

裁剪图片:

 public static void caijian(string inputPath, string outputPath,int x,int y,int width,int height)
        {
            // 加载原始图片
            using (Bitmap originalBitmap = new Bitmap(inputPath))
            {
                // 定义裁剪区域(x, y, width, height)
                Rectangle cropArea = new Rectangle(x, y, width, height); // 修改这些值以适应你的需求

                // 创建裁剪后的图片
                using (Bitmap croppedBitmap = CropImage(originalBitmap, cropArea))
                {
                    // 保存裁剪后的图片
                    croppedBitmap.Save(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;
        }

 裁切图片需要引入类:

完整项目代码奉上: 

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; // 如果你使用的是System.Drawing.Common
using ZXing;
using Tesseract;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string outimagePath1 = Environment.CurrentDirectory + "\\outLab.jpg";
            string outimagePath2 = Environment.CurrentDirectory + "\\outStart.jpg";
            string imagePath3 = Environment.CurrentDirectory + "\\333330.jpg";
            caijian(imagePath3, outimagePath1, 1600, 1250, 1500, 350);
            Getlab(outimagePath1);
            caijian(imagePath3, outimagePath2,475,873,738,673);
            GetChistring(outimagePath2);

            Console.Read();
        }


        public static void caijian(string inputPath, string outputPath,int x,int y,int width,int height)
        {
            // 加载原始图片
            using (Bitmap originalBitmap = new Bitmap(inputPath))
            {
                // 定义裁剪区域(x, y, width, height)
                Rectangle cropArea = new Rectangle(x, y, width, height); // 修改这些值以适应你的需求

                // 创建裁剪后的图片
                using (Bitmap croppedBitmap = CropImage(originalBitmap, cropArea))
                {
                    // 保存裁剪后的图片
                    croppedBitmap.Save(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 void GetChistring(string imagePath)
        {
            try
            {
                // 确保已下载并引用了 Tesseract 的语言数据文件(.traineddata)
                //var engine = new TesseractEngine(tessdataPath, "chi_sim", EngineMode.LstmOnly)
                string tessdataPath = Environment.CurrentDirectory+ "\\tessdata"; // 通常包含 "eng.traineddata" 等文件
                var engine = new TesseractEngine(tessdataPath, "chi_sim", EngineMode.Default | EngineMode.LstmOnly);
                var img = Pix.LoadFromFile(imagePath);
                var page = engine.Process(img);
                Console.WriteLine("识别结果: " + page.GetText());
                Console.WriteLine("置信度: " + page.GetMeanConfidence());
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
        public static void GetENstring(string imagePath)
        {
            try
            {
                // 确保已下载并引用了 Tesseract 的语言数据文件(.traineddata)
                //var engine = new TesseractEngine(tessdataPath, "chi_sim", EngineMode.LstmOnly)
                string tessdataPath = Environment.CurrentDirectory + "\\tessdata"; // 通常包含 "eng.traineddata" 等文件
                var engine = new TesseractEngine(tessdataPath, "eng", EngineMode.Default | EngineMode.LstmOnly);
                var img = Pix.LoadFromFile(imagePath);
                var page = engine.Process(img);
                Console.WriteLine("识别结果: " + page.GetText());
                Console.WriteLine("置信度: " + page.GetMeanConfidence());
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
        public static void 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.Decode(barcodeBitmap);

            if (result != null)
            {
                Console.WriteLine("识别内容: " + result.Text);
            }
            else
            {
                Console.WriteLine("未能识别内容");
            }
        }
    }
}

 将图片用Windows画图打开确认像素点 和大小进行裁剪后交给算法识别;

GitCode - 全球开发者的开源社区,开源代码托管平台

下载训练模型:chi_sim.traineddata 中文

下载训练模型:eng.traineddata 英文

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值