a
This commit is contained in:
27
TestProject1/TestProject1.csproj
Normal file
27
TestProject1/TestProject1.csproj
Normal file
@@ -0,0 +1,27 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Aspose.PDF" Version="24.8.0" />
|
||||
<PackageReference Include="coverlet.collector" Version="3.2.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
|
||||
<PackageReference Include="OpenCvSharp4" Version="4.10.0.20240616" />
|
||||
<PackageReference Include="OpenCvSharp4.Extensions" Version="4.10.0.20240616" />
|
||||
<PackageReference Include="OpenCvSharp4.runtime.win" Version="4.10.0.20240616" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
272
TestProject1/UnitTest1.cs
Normal file
272
TestProject1/UnitTest1.cs
Normal file
@@ -0,0 +1,272 @@
|
||||
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\<5C><><EFBFBD><EFBFBD><EFBFBD>嵥\<5C><>ŵ\<5C><><EFBFBD><EFBFBD>ǩ<EFBFBD>յ<EFBFBD>\P2401001-XNZ01-POORD007585-<2D>麣<EFBFBD><E9BAA3>ŵ<EFBFBD><C5B5>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˾-500.pdf");
|
||||
|
||||
|
||||
//<2F>ĵ<EFBFBD>Ŀ¼<C4BF><C2BC>·<EFBFBD><C2B7><EFBFBD><EFBFBD>
|
||||
string dataDir = @"C:\Users\Fareoh\Desktop\<5C><><EFBFBD><EFBFBD><EFBFBD>嵥\<5C><>ŵ\<5C><><EFBFBD><EFBFBD>ǩ<EFBFBD>յ<EFBFBD>\Test\";
|
||||
Document pdfDocument = new Document(dataDir + "P2401001-XNZ01-POORD007585-<2D>麣<EFBFBD><E9BAA3>ŵ<EFBFBD><C5B5>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˾-500.pdf");
|
||||
|
||||
//Mat src;
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>
|
||||
//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);
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>Ե<EFBFBD> JPEG <20>豸
|
||||
//<2F><><EFBFBD>ȡ<EFBFBD><C8A1>߶ȡ<DFB6><C8A1>ֱ<EFBFBD><D6B1>ʡ<EFBFBD><CAA1><EFBFBD><EFBFBD><EFBFBD>
|
||||
//<2F><><EFBFBD><EFBFBD> [0-100]<5D><>100 Ϊ<><CEAA><EFBFBD><EFBFBD>ֵ
|
||||
//<2F><><EFBFBD><EFBFBD> Resolution <20><><EFBFBD><EFBFBD>
|
||||
Resolution resolution = new Resolution(300);
|
||||
//JpegDevice jpegDevice = new JpegDevice(500, 700, <20>ֱ<EFBFBD><D6B1><EFBFBD>, 100);
|
||||
JpegDevice jpegDevice = new JpegDevice(resolution, 100);
|
||||
//ת<><D7AA><EFBFBD>ض<EFBFBD>ҳ<EFBFBD>沢<EFBFBD><E6B2A2>ͼ<EFBFBD>浽<F1B1A3B4><E6B5BD><EFBFBD><EFBFBD>
|
||||
jpegDevice.Process(pdfDocument.Pages[pageCount], imageStream);
|
||||
|
||||
//<2F>ر<EFBFBD><D8B1><EFBFBD>
|
||||
imageStream.Close();
|
||||
}
|
||||
}
|
||||
Console.WriteLine("PDF pages are converted to individual images successfully!");
|
||||
|
||||
//CollectionUtils
|
||||
|
||||
Mat src = new Mat(@"C:\Users\Fareoh\OneDrive\ͼƬ\work\<5C><>ά<EFBFBD><CEAC>ԭͼƬ.jpg");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC>
|
||||
//Mat src = Cv2.ImRead("path_to_your_image.jpg");
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (x, y, width, height)
|
||||
Rect region = new Rect(2000, 400, 500, 500);
|
||||
|
||||
// <20><><EFBFBD>ü<EFBFBD><C3BC>ⷽ<EFBFBD><E2B7BD>
|
||||
//string result = DetectQRCodeInRegion(src, region);
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
//Console.WriteLine("Decoded QR Code: " + result);
|
||||
//DecodeQRCodes(src, region);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void TestMethod2()
|
||||
{
|
||||
//<2F>ĵ<EFBFBD>Ŀ¼<C4BF><C2BC>·<EFBFBD><C2B7><EFBFBD><EFBFBD>
|
||||
string dataDir = @"C:\Users\Fareoh\Desktop\<5C><><EFBFBD><EFBFBD><EFBFBD>嵥\<5C><>ŵ\<5C><><EFBFBD><EFBFBD>ǩ<EFBFBD>յ<EFBFBD>\Test\";
|
||||
string dataPutDir = @"C:\Users\Fareoh\Desktop\<5C><><EFBFBD><EFBFBD><EFBFBD>嵥\<5C><>ŵ\<5C><><EFBFBD><EFBFBD>ǩ<EFBFBD>յ<EFBFBD>\";
|
||||
|
||||
var dirInfoDi = new Dictionary<string, string>();
|
||||
List<string> QRCodeList = new List<string>();
|
||||
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); // <20><> Bitmap ת<><D7AA>Ϊ Mat
|
||||
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public override void CustomEvents(CustomEventsArgs e)
|
||||
{
|
||||
if (e.Key.ToUpper() == "FFileUpdate".ToUpper())
|
||||
{ //<2F><><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4>ļ<EFBFBD><C4BC>б仯
|
||||
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)
|
||||
{
|
||||
//<2F><>ȡ<EFBFBD>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
|
||||
string text = (jSONArray[0] as Dictionary)["ServerFileName"].ToString();
|
||||
//<2F>ļ<EFBFBD><C4BC>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5><EFBFBD>ʱĿ¼
|
||||
string directory = "FileUpLoadServices\\UploadFiles";
|
||||
//<2F>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
|
||||
string fileFullPath = PathUtils.GetPhysicalPath(directory, text);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
base.CustomEvents(e);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="src"></param>
|
||||
/// <param name="region"></param>
|
||||
/// <returns></returns>
|
||||
public static string DetectQRCodeInRegion(Mat src, Rect region)
|
||||
{
|
||||
// <20>ü<EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
Mat roi = new Mat(src, region);
|
||||
|
||||
// תΪ<D7AA>Ҷ<EFBFBD>ͼ<EFBFBD><CDBC>
|
||||
Mat gray = new Mat();
|
||||
Cv2.CvtColor(roi, gray, ColorConversionCodes.BGR2GRAY);
|
||||
|
||||
// <20><>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
Mat binary = new Mat();
|
||||
Cv2.Threshold(gray, binary, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD>ͽ<EFBFBD><CDBD><EFBFBD><EFBFBD><EFBFBD>ά<EFBFBD><CEAC>
|
||||
QRCodeDetector qrCodeDetector = new QRCodeDetector();
|
||||
Point2f[] points;
|
||||
string decodedText = qrCodeDetector.DetectAndDecode(binary, out points);
|
||||
|
||||
// <20><><EFBFBD>ؽ<EFBFBD><D8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
return decodedText;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ֻʶ<D6BB><CAB6><EFBFBD><EFBFBD>ά<EFBFBD><CEAC>
|
||||
/// </summary>
|
||||
/// <param name="src"></param>
|
||||
/// <param name="region"></param>
|
||||
private static void DecodeQRCodes(Mat src, Rect region, out string[] qrCodeTexts)
|
||||
{
|
||||
qrCodeTexts = null;
|
||||
|
||||
// <20>ü<EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
Mat roi = new Mat(src, region);
|
||||
|
||||
// תΪ<D7AA>Ҷ<EFBFBD>ͼ<EFBFBD><CDBC>
|
||||
Mat gray = new Mat();
|
||||
Cv2.CvtColor(roi, gray, ColorConversionCodes.BGR2GRAY);
|
||||
|
||||
// <20><>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
Mat binary = new Mat();
|
||||
Cv2.Threshold(gray, binary, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);
|
||||
|
||||
//<2F><>ά<EFBFBD><CEAC>ʶ<EFBFBD><CAB6>
|
||||
using (QRCodeDetector qRCodeDetector = new QRCodeDetector())
|
||||
{
|
||||
Point2f[] points;
|
||||
var hasQRCode = qRCodeDetector.DetectMulti(binary, out points);
|
||||
if (hasQRCode)
|
||||
{
|
||||
qRCodeDetector.DecodeMulti(binary, points, out qrCodeTexts);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ֻʶ<D6BB><CAB6><EFBFBD><EFBFBD>ά<EFBFBD><CEAC>
|
||||
/// </summary>
|
||||
private static void DecodeQRCodes()
|
||||
{
|
||||
var img = Cv2.ImRead(@"C:\Users\Fareoh\OneDrive\ͼƬ\work\<5C><>ά<EFBFBD><CEAC>ԭͼƬ.jpg");
|
||||
string[] qrCodeTexts = null;
|
||||
//<2F><>ά<EFBFBD><CEAC>ʶ<EFBFBD><CAB6>
|
||||
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($"<22><><EFBFBD>{qrCodeTexts.Length}<7D><><EFBFBD><EFBFBD>ά<EFBFBD>룺");
|
||||
for (int i = 0; i < qrCodeTexts.Length; i++)
|
||||
{
|
||||
Console.WriteLine($"<22><>{(i + 1)}<7D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>{qrCodeTexts[i]}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user