a
This commit is contained in:
452
UnitTestProject1/UnitTest1.cs
Normal file
452
UnitTestProject1/UnitTest1.cs
Normal file
@@ -0,0 +1,452 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using OpenCvSharp;
|
||||
using OpenCvSharp.Extensions;
|
||||
using Spire.OCR;
|
||||
using Spire.Pdf;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using static UnitTestProject1.UnitTest1;
|
||||
|
||||
namespace UnitTestProject1
|
||||
{
|
||||
[TestClass]
|
||||
public class UnitTest1
|
||||
{
|
||||
[TestMethod]
|
||||
public void TestMethod1()
|
||||
{
|
||||
try
|
||||
{
|
||||
string dataDir = @"C:\Users\Fareoh\Desktop\工作清单\派诺\发货签收单\Test\";
|
||||
string fileName = @"P2401001-XNZ01-POORD007585-珠海兴诺能源技术有限公司-500.pdf";
|
||||
//Document pdfDocument = new Document(dataDir + "P2401001-XNZ01-POORD007585-珠海兴诺能源技术有限公司-500.pdf");
|
||||
|
||||
//var data = ExtractTextFromScannedPDF(dataDir + fileName);
|
||||
var pdfFilePath = dataDir + fileName;
|
||||
// 创建一个列表来存储提取的文本
|
||||
List<string> extractedText = new List<string>();
|
||||
|
||||
// 创建 PdfDocument 类的实例
|
||||
using (PdfDocument document = new PdfDocument())
|
||||
{
|
||||
// 加载 PDF 文档
|
||||
document.LoadFromFile(pdfFilePath);
|
||||
|
||||
// 将页面转换为图片
|
||||
using (Image image = document.SaveAsImage(0, 500, 500))
|
||||
{
|
||||
// 创建流来保存图片数据
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
// 将图片以 PNG 格式保存到流中
|
||||
image.Save(stream, ImageFormat.Png);
|
||||
stream.Position = 0;
|
||||
|
||||
Mat src = BitmapConverter.ToMat(new Bitmap(image)); //bitmap转 mat
|
||||
|
||||
var srcWidth = src.Width;
|
||||
var srcHeight = src.Height;
|
||||
int x = srcWidth / 2;
|
||||
int y = 0;
|
||||
int width = srcWidth / 2;
|
||||
int height = srcHeight / 2;
|
||||
|
||||
Rect region = new Rect(x, y, width, height);
|
||||
DecodeQRCodes(src, region, out string[] qrCodeTexts);
|
||||
//Bitmap bitmap = BitmapConverter.ToBitmap(mat); // mat 转 bitmap
|
||||
|
||||
// 从流中扫描文本并将其添加到列表中
|
||||
//string text = ScanTextFromImageStream(stream);
|
||||
//extractedText.Add(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 加载图像
|
||||
//Mat src = Cv2.ImRead("path_to_your_image.jpg");
|
||||
|
||||
// 定义检测区域 (x, y, width, height)
|
||||
//Rect region = new Rect(2000, 400, 500, 500);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void TestMethod3()
|
||||
{
|
||||
//文档目录的路径。
|
||||
//string dataDir = @"C:\Users\Fareoh\Desktop\工作清单\派诺\发货签收单\Test\";
|
||||
string dataPutDir = @"C:\Users\Fareoh\Desktop\工作清单\派诺\发货签收单\";
|
||||
|
||||
var dirInfoDi = new Dictionary<string, string>();
|
||||
List<string> QRCodeList = new List<string>();
|
||||
try
|
||||
{
|
||||
string fileName = "P202309180030-沈阳广耀科技有限公司-4.jpg";
|
||||
Mat src = new Mat(dataPutDir + fileName);
|
||||
|
||||
var srcWidth = src.Width;
|
||||
var srcHeight = src.Height;
|
||||
int x = srcWidth / 2;
|
||||
int y = 0;
|
||||
int width = srcWidth / 2;
|
||||
int height = srcHeight / 2;
|
||||
|
||||
Rect region = new Rect(x, y, width, height);
|
||||
DecodeQRCodes(src, region, out string[] qrCodeTexts);
|
||||
if (qrCodeTexts != null)
|
||||
{
|
||||
QRCodeList.AddRange(qrCodeTexts);
|
||||
dirInfoDi.Add(qrCodeTexts[0], fileName);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private void DecodeQRCodes(Mat src, out string[] qrCodeTexts)
|
||||
{
|
||||
qrCodeTexts = null;
|
||||
|
||||
// 裁剪指定区域
|
||||
//Mat roi = new Mat(src, region);
|
||||
Mat roi = src;
|
||||
|
||||
// 转为灰度图像
|
||||
Mat gray = new Mat();
|
||||
Cv2.CvtColor(roi, gray, ColorConversionCodes.BGR2GRAY);
|
||||
|
||||
// 二值化处理
|
||||
Mat binary = new Mat();
|
||||
Cv2.Threshold(gray, binary, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);
|
||||
|
||||
//二维码识别
|
||||
using (QRCodeDetector qRCodeDetector = new QRCodeDetector())
|
||||
{
|
||||
Point2f[] points;
|
||||
var hasQRCode = qRCodeDetector.DetectMulti(binary, out points);
|
||||
if (hasQRCode)
|
||||
{
|
||||
qRCodeDetector.DecodeMulti(binary, points, out qrCodeTexts);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestMethod2()
|
||||
{
|
||||
//文档目录的路径。
|
||||
//string dataDir = @"C:\Users\Fareoh\Desktop\工作清单\派诺\发货签收单\Test\";
|
||||
string dataPutDir = @"C:\Users\Fareoh\Desktop\工作清单\派诺\发货签收单\";
|
||||
|
||||
try
|
||||
{
|
||||
var result = new List<AnalyzeResult>();
|
||||
|
||||
var diInfos = new DirectoryInfo(dataPutDir);
|
||||
var jpgfiles = diInfos.GetFiles("*.JPG");
|
||||
if (jpgfiles != null && jpgfiles.Count() > 0)
|
||||
{
|
||||
foreach (var file in jpgfiles)
|
||||
{
|
||||
Mat src = new Mat(file.FullName);
|
||||
var srcWidth = src.Width;
|
||||
var srcHeight = src.Height;
|
||||
int x = srcWidth / 2;
|
||||
int y = 0;
|
||||
int width = srcWidth / 2;
|
||||
int height = srcHeight / 2;
|
||||
|
||||
Rect region = new Rect(x, y, width, height);
|
||||
var resData = DecodeQRCodes(src, region, false);
|
||||
if (resData != null)
|
||||
{
|
||||
resData.fileName = file.FullName;
|
||||
result.Add(resData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var pdffiles = diInfos.GetFiles("*.PDF");
|
||||
|
||||
// 创建一个列表来存储提取的文本
|
||||
List<string> extractedText = new List<string>();
|
||||
|
||||
foreach (var pdffile in pdffiles)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 创建 PdfDocument 类的实例
|
||||
using (PdfDocument document = new PdfDocument())
|
||||
{
|
||||
// 加载 PDF 文档
|
||||
document.LoadFromFile(pdffile.FullName);
|
||||
|
||||
// 将页面转换为图片
|
||||
//using (Image image = document.SaveAsImage(0, 1653, 2338))
|
||||
using (Image image = document.SaveAsImage(0, 200, 200))
|
||||
{
|
||||
// 创建流来保存图片数据
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
// 将图片以 PNG 格式保存到流中
|
||||
image.Save(stream, ImageFormat.Png);
|
||||
stream.Position = 0;
|
||||
|
||||
using (Mat src = BitmapConverter.ToMat(new Bitmap(image)))
|
||||
{
|
||||
var srcWidth = src.Width;
|
||||
var srcHeight = src.Height;
|
||||
int x = srcWidth / 2;
|
||||
int y = 0;
|
||||
int width = srcWidth / 2;
|
||||
int height = srcHeight / 2;
|
||||
|
||||
Rect region = new Rect(x, y, width, height);
|
||||
|
||||
var resData = DecodeQRCodes(src, region, false);
|
||||
if (resData != null)
|
||||
{
|
||||
resData.fileName = pdffile.FullName;
|
||||
result.Add(resData);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private Mat SetRgion(Mat src, Rect region)
|
||||
{
|
||||
Mat dst = new Mat(src, region);
|
||||
|
||||
var srcWidth = dst.Width;
|
||||
var srcHeight = dst.Height;
|
||||
int x = srcWidth / 2;
|
||||
int y = 0;
|
||||
int width = srcWidth / 2;
|
||||
int height = srcHeight / 2;
|
||||
|
||||
var newRegion = new Rect(x, y, width, height);
|
||||
|
||||
Mat resDst = new Mat(dst, newRegion);
|
||||
|
||||
return resDst;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private void DecodeQRCodes(Mat src, Rect region, out string[] qrCodeTexts)
|
||||
{
|
||||
qrCodeTexts = null;
|
||||
|
||||
// 裁剪指定区域
|
||||
//Mat roi = SetRgion(src, region);
|
||||
Mat roi = new Mat(src, region);
|
||||
|
||||
//Mat roi = src;
|
||||
//this.pictureBox1.Image = BitmapConverter.ToBitmap(roi);
|
||||
// 转为灰度图像
|
||||
Mat gray = new Mat();
|
||||
Cv2.CvtColor(roi, gray, ColorConversionCodes.BGR2GRAY);
|
||||
|
||||
// 二值化处理
|
||||
Mat binary = new Mat();
|
||||
Cv2.Threshold(gray, binary, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);
|
||||
|
||||
|
||||
//二维码识别
|
||||
using (QRCodeDetector qRCodeDetector = new QRCodeDetector())
|
||||
{
|
||||
Point2f[] points;
|
||||
var hasQRCode = qRCodeDetector.DetectMulti(binary, out points);
|
||||
if (hasQRCode)
|
||||
{
|
||||
qRCodeDetector.DecodeMulti(binary, points, out qrCodeTexts);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 识别二维码
|
||||
/// </summary>
|
||||
/// <param name="src"></param>
|
||||
/// <param name="region"></param>
|
||||
/// <param name="isReturn"></param>
|
||||
/// <returns></returns>
|
||||
private static AnalyzeResult DecodeQRCodes(Mat src, Rect region, bool isReturn)
|
||||
{
|
||||
AnalyzeResult analyzeResult = null;
|
||||
|
||||
// 裁剪指定区域
|
||||
using (Mat roi = new Mat(src, region))
|
||||
{
|
||||
// 转为灰度图像
|
||||
using (Mat gray = new Mat())
|
||||
{
|
||||
Cv2.CvtColor(roi, gray, ColorConversionCodes.BGR2GRAY);
|
||||
|
||||
// 二值化处理
|
||||
using (Mat binary = new Mat())
|
||||
{
|
||||
Cv2.Threshold(gray, binary, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);
|
||||
|
||||
//二维码识别
|
||||
using (QRCodeDetector qRCodeDetector = new QRCodeDetector())
|
||||
{
|
||||
Point2f[] points;
|
||||
var hasQRCode = qRCodeDetector.DetectMulti(binary, out points);
|
||||
if (hasQRCode)
|
||||
{
|
||||
qRCodeDetector.DecodeMulti(binary, points, out string[] qrCodeTexts);
|
||||
|
||||
analyzeResult = new AnalyzeResult();
|
||||
analyzeResult.name = qrCodeTexts[0];
|
||||
analyzeResult.points = points;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isReturn)
|
||||
return null;
|
||||
|
||||
var srcWidth = roi.Width;
|
||||
var srcHeight = roi.Height;
|
||||
int x = srcWidth / 2;
|
||||
int y = 0;
|
||||
int width = srcWidth / 2;
|
||||
int height = srcHeight / 2;
|
||||
|
||||
var newRegion = new Rect(x, y, width, height);
|
||||
|
||||
analyzeResult = DecodeQRCodes(roi, newRegion, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return analyzeResult;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 识别二维码
|
||||
/// </summary>
|
||||
/// <param name="src"></param>
|
||||
/// <param name="region"></param>
|
||||
/// <param name="isReturn"></param>
|
||||
/// <returns></returns>
|
||||
private AnalyzeResult DecodeQRCodes2(Mat src, Rect region, bool isReturn)
|
||||
{
|
||||
// 裁剪指定区域
|
||||
Mat roi = new Mat(src, region);
|
||||
|
||||
// 转为灰度图像
|
||||
Mat gray = new Mat();
|
||||
Cv2.CvtColor(roi, gray, ColorConversionCodes.BGR2GRAY);
|
||||
|
||||
// 二值化处理
|
||||
Mat binary = new Mat();
|
||||
Cv2.Threshold(gray, binary, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);
|
||||
|
||||
AnalyzeResult analyzeResult = null;
|
||||
//二维码识别
|
||||
using (QRCodeDetector qRCodeDetector = new QRCodeDetector())
|
||||
{
|
||||
Point2f[] points;
|
||||
var hasQRCode = qRCodeDetector.DetectMulti(binary, out points);
|
||||
if (hasQRCode)
|
||||
{
|
||||
qRCodeDetector.DecodeMulti(binary, points, out string[] qrCodeTexts);
|
||||
|
||||
analyzeResult = new AnalyzeResult();
|
||||
analyzeResult.name = qrCodeTexts[0];
|
||||
analyzeResult.points = points;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isReturn)
|
||||
return null;
|
||||
|
||||
var srcWidth = roi.Width;
|
||||
var srcHeight = roi.Height;
|
||||
int x = srcWidth / 2;
|
||||
int y = 0;
|
||||
int width = srcWidth / 2;
|
||||
int height = srcHeight / 2;
|
||||
|
||||
var newRegion = new Rect(x, y, width, height);
|
||||
|
||||
analyzeResult = DecodeQRCodes(roi, newRegion, true);
|
||||
}
|
||||
}
|
||||
|
||||
return analyzeResult;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 从流中扫描文本
|
||||
public static string ScanTextFromImageStream(Stream stream)
|
||||
{
|
||||
// 创建 OcrScanner 类的实例
|
||||
using (OcrScanner ocrScanner = new OcrScanner())
|
||||
{
|
||||
// 从流中扫描文本
|
||||
ocrScanner.Scan(stream, OCRImageFormat.Png);
|
||||
IOCRText text = ocrScanner.Text;
|
||||
|
||||
// 返回文本
|
||||
return text.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class AnalyzeResult
|
||||
{
|
||||
public string fileName { get; set; }
|
||||
|
||||
public string name { get; set; }
|
||||
|
||||
public Point2f[] points { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user