using Aspose.Pdf; using Aspose.Pdf.AI; using Aspose.Pdf.Devices; using Aspose.Pdf.Drawing; using OpenCvSharp; using OpenCvSharp.Extensions; using System; using System.Collections.Specialized; using System.Drawing; using System.Runtime.InteropServices; using static System.Net.Mime.MediaTypeNames; //using System.Drawing.Common; //using OpenCvSharp.Extensions; //using ZXing; namespace TestProject1 { [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { try { //DecodeQRCodes(); //var doc = PdfReader.Open(@"C:\Users\Fareoh\Desktop\工作清单\派诺\发货签收单\P2401001-XNZ01-POORD007585-珠海兴诺能源技术有限公司-500.pdf"); //文档目录的路径。 string dataDir = @"C:\Users\Fareoh\Desktop\工作清单\派诺\发货签收单\Test\"; Document pdfDocument = new Document(dataDir + "P2401001-XNZ01-POORD007585-珠海兴诺能源技术有限公司-500.pdf"); //Mat src; //打开文档 //Document pdfDocument = new Document(dataDir + "PagesToImages.pdf"); for (int pageCount = 1; pageCount <= pdfDocument.Pages.Count; pageCount++) { using (FileStream imageStream = new FileStream(dataDir + "image" + pageCount + "_out" + ".jpg", FileMode.Create)) { //src = new Mat(); //var height = src.Rows; //var width = src.Cols; //var channels = src.Channels(); //var mattype = src.Type(); //var src = new Mat(height, width, MatType.CV_8UC3); //int length = height * width * 3; // or src.Height * src.Step; //Marshal.Copy(data, 0, src.Data, length); //创建具有指定属性的 JPEG 设备 //宽度、高度、分辨率、质量 //质量 [0-100],100 为最大值 //创建 Resolution 对象 Resolution resolution = new Resolution(300); //JpegDevice jpegDevice = new JpegDevice(500, 700, 分辨率, 100); JpegDevice jpegDevice = new JpegDevice(resolution, 100); //转换特定页面并将图像保存到流中 jpegDevice.Process(pdfDocument.Pages[pageCount], imageStream); //关闭流 imageStream.Close(); } } Console.WriteLine("PDF pages are converted to individual images successfully!"); //CollectionUtils Mat src = new Mat(@"C:\Users\Fareoh\OneDrive\图片\work\二维码原图片.jpg"); // 加载图像 //Mat src = Cv2.ImRead("path_to_your_image.jpg"); // 定义检测区域 (x, y, width, height) Rect region = new Rect(2000, 400, 500, 500); // 调用检测方法 //string result = DetectQRCodeInRegion(src, region); // 输出结果 //Console.WriteLine("Decoded QR Code: " + result); //DecodeQRCodes(src, region); } catch (Exception ex) { } } [TestMethod] public void TestMethod2() { //文档目录的路径。 string dataDir = @"C:\Users\Fareoh\Desktop\工作清单\派诺\发货签收单\Test\"; string dataPutDir = @"C:\Users\Fareoh\Desktop\工作清单\派诺\发货签收单\"; var dirInfoDi = new Dictionary(); List QRCodeList = new List(); try { 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); DecodeQRCodes(src, region, out string[] qrCodeTexts); if (qrCodeTexts != null) { QRCodeList.AddRange(qrCodeTexts); dirInfoDi.Add(qrCodeTexts[0], dataPutDir); } } } var pdffiles = diInfos.GetFiles("*.PDF"); Bitmap bitmap = new Bitmap("image.jpg"); Mat mat = BitmapConverter.ToMat(bitmap); // 将 Bitmap 转换为 Mat } catch (Exception ex) { Console.WriteLine(ex.Message); } } public override void CustomEvents(CustomEventsArgs e) { if (e.Key.ToUpper() == "FFileUpdate".ToUpper()) { //触发事件是上传文件有变化 if (e.EventName.ToUpper() == "FILECHANGED") { JSONObject jSONObject = KDObjectConverter.DeserializeObject(e.EventArgs); if (jSONObject != null) { JSONArray jSONArray = new JSONArray(jSONObject["NewValue"].ToString()); if (jSONArray.Count > 0) { //获取上传的文件名 string text = (jSONArray[0] as Dictionary)["ServerFileName"].ToString(); //文件上传到服务端的临时目录 string directory = "FileUpLoadServices\\UploadFiles"; //文件的完整路径 string fileFullPath = PathUtils.GetPhysicalPath(directory, text); } } } } base.CustomEvents(e); } /// /// /// /// /// /// public static string DetectQRCodeInRegion(Mat src, Rect region) { // 裁剪指定区域 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); // 检测和解码二维码 QRCodeDetector qrCodeDetector = new QRCodeDetector(); Point2f[] points; string decodedText = qrCodeDetector.DetectAndDecode(binary, out points); // 返回解码结果 return decodedText; } /// /// 只识别二维码 /// /// /// private static void DecodeQRCodes(Mat src, Rect region, out string[] qrCodeTexts) { qrCodeTexts = null; // 裁剪指定区域 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); //二维码识别 using (QRCodeDetector qRCodeDetector = new QRCodeDetector()) { Point2f[] points; var hasQRCode = qRCodeDetector.DetectMulti(binary, out points); if (hasQRCode) { qRCodeDetector.DecodeMulti(binary, points, out qrCodeTexts); } } } /// /// 只识别二维码 /// private static void DecodeQRCodes() { var img = Cv2.ImRead(@"C:\Users\Fareoh\OneDrive\图片\work\二维码原图片.jpg"); string[] qrCodeTexts = null; //二维码识别 using (QRCodeDetector qRCodeDetector = new QRCodeDetector()) { Point2f[] points; var hasQRCode = qRCodeDetector.DetectMulti(img, out points); if (hasQRCode) { qRCodeDetector.DecodeMulti(img, points, out qrCodeTexts); } } if (qrCodeTexts != null) { Console.WriteLine($"检测到{qrCodeTexts.Length}个二维码:"); for (int i = 0; i < qrCodeTexts.Length; i++) { Console.WriteLine($"第{(i + 1)}个的内容为:{qrCodeTexts[i]}"); } } } } }