当前位置: 首页 > news >正文

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 英文

http://www.lryc.cn/news/436658.html

相关文章:

  • UEFI——访问PCI/PCIE设备(二)
  • 决策树算法的介绍与应用
  • 杰发科技Bootloader(3)—— 基于7801的APP切到Boot
  • Leetcode面试经典150题-138.随机链表的复制
  • freemarker模板学习笔记
  • 高亚科技与广东海悟携手,打造全流程电子竞标管理平台!
  • 240908-结合DBGPT与Ollama实现RAG本地知识检索增强
  • AMD ThinkSystem服务器上的 Linux 和 C 状态设置 - Lenovo ThinkSystem
  • Redis过期删除和缓存淘汰
  • Golang | Leetcode Golang题解之第401题二进制手表
  • TON智能合约stdlib_ext库:扩展功能一览
  • LabVIEW开发FPGA方法与FIFO数据丢失处理
  • Python中的内存池机制
  • 智能家居系统(基于STM32F103C8T6标准库+FreeRTOS+Qt串口开发实现)
  • [数据集][目标检测]脊椎检测数据集VOC+YOLO格式1137张1类别
  • 大受欢迎的游戏却又意外被作者下架的《Flappy Bird》将重返iPhone
  • Flutter类
  • 深入解析全连接层:PyTorch 中的 nn.Linear、nn.Parameter 及矩阵运算
  • 缓存对象反序列化失败
  • F28335的存储器与寄存器
  • Python在AOIP(Audio Over IP)方面的应用探讨
  • C++20标准对线程库的改进:更安全、更高效的并发编程
  • 外包干了三年,快要废了。。。
  • 微服务网关终极进化:设计模式驱动的性能与可用性优化(四)
  • Java中的服务端点日志记录:AOP与SLF4J
  • 黑马头条第八天实战(上)
  • swift qwen2-vl推理及加载lora使用案例
  • 如何使用 Choreographer 进行帧率优化
  • 稳定驱动之选SiLM5350系列SiLM5350MDBCA-DG单通道隔离栅极驱动器(带内部钳位):工业自动化的可靠伙伴
  • 鸿蒙OpenHarmony【轻量系统芯片移植】内核移植