a
This commit is contained in:
92
GZ.LJY000.PiLot/HandleQRCodeByFileTool.cs
Normal file
92
GZ.LJY000.PiLot/HandleQRCodeByFileTool.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using OpenCvSharp;
|
||||
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.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
namespace GZ.LJY000.PiLot
|
||||
{
|
||||
public static class HandleQRCodeByFileTool
|
||||
{
|
||||
|
||||
/// <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;
|
||||
}
|
||||
|
||||
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