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