您的位置:首页 > 游戏 > 手游 > c# 视觉识别图片文字 二维码

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

2024/11/17 14:12:35 来源:https://blog.csdn.net/weixin_56830970/article/details/142180293  浏览:    关键词: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 英文

版权声明:

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

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