This commit is contained in:
Kq
2026-08-12 19:11:44 +02:00
parent e6a8c2f6e5
commit 4059a3b67b
+83 -70
View File
@@ -7,6 +7,8 @@ using ZXing.Common;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Drawing; using System.Drawing;
using System.Net; using System.Net;
using System.Runtime.InteropServices.Marshalling;
using Emgu.CV.Face;
// using Emgu.CV.CvEnum; // using Emgu.CV.CvEnum;
@@ -24,10 +26,11 @@ namespace BarcodeReaderExample
camera.Set(VideoCaptureProperties.FrameWidth, 1280); camera.Set(VideoCaptureProperties.FrameWidth, 1280);
camera.Set(VideoCaptureProperties.FrameHeight, 720); camera.Set(VideoCaptureProperties.FrameHeight, 720);
camera.Set(VideoCaptureProperties.Fps, 24); int fps = 60;
if(Directory.Exists("cache")) camera.Set(VideoCaptureProperties.Fps, fps);
if (Directory.Exists("cache"))
{ {
Directory.Delete("cache",true); Directory.Delete("cache", true);
} }
Directory.CreateDirectory("cache"); Directory.CreateDirectory("cache");
@@ -35,6 +38,9 @@ namespace BarcodeReaderExample
Mat[] Cached = new Mat[10]; Mat[] Cached = new Mat[10];
int imgX = 0;
int imgY = 0;
using var rgb = new Mat(); using var rgb = new Mat();
// OpenCV stores camera frames as BGR. // OpenCV stores camera frames as BGR.
@@ -44,35 +50,39 @@ namespace BarcodeReaderExample
Cv2.NamedWindow("Camera preview", WindowFlags.AutoSize); Cv2.NamedWindow("Camera preview", WindowFlags.AutoSize);
int photocount = 0; // Mat src = Cv2.ImRead("test.png");
Mat src = Cv2.ImRead("test.png"); int whenWasLastTimeQrDetected = 0;
Mat Lastimage = new Mat();
IEnumerable<IEnumerable<OpenCvSharp.Point>> contours = null;
// PointF[] location;
string[] files = new string[20];
int files_index = 0;
string[] files = new string[10];
int files_index =0;
while (true) while (true)
{ {
// if (!camera.Read(frame) || frame.Empty())
// {
// Console.Error.WriteLine("Nie udało się odczytać klatki");
// break;
// }
camera.Read(frame); camera.Read(frame);
Result result = ReadQrCode(frame);
Console.WriteLine(Lastimage.Empty());
if (!(Lastimage.Empty()))
{
Mat roi2 = frame[new Rect(0, 0, Lastimage.Width, Lastimage.Height)];
Lastimage.CopyTo(roi2);
}
Result result = GetResult(frame);
if (result != null) if (result != null)
{ {
whenWasLastTimeQrDetected = 5;
List<ResultPoint> points = result.ResultPoints.ToList(); List<ResultPoint> points = result.ResultPoints.ToList();
float[] pos1 = new float[2] { points[0].X, points[0].Y }; /// lewy dolny róg float[] pos1 = new float[2] { points[0].X, points[0].Y }; /// lewy dolny róg
float[] pos2 = new float[2] { points[2].X, points[2].Y }; ///prawy górny róg float[] pos2 = new float[2] { points[2].X, points[2].Y }; ///prawy górny róg
float[] pos3 = new float[2] { points[1].X, points[1].Y }; ///lewy górny róg float[] pos3 = new float[2] { points[1].X, points[1].Y }; ///lewy górny róg
int width = (int)(pos2[0] - pos3[0]); int width = (int)(pos2[0] - pos3[0]);
int heigh = (int)(pos1[1] - pos3[1]); int heigh = (int)(pos1[1] - pos3[1]);
// Cv2.Circle(frame, (int)pos1[0], (int)pos1[1], 5, Scalar.Red, 2);
// Cv2.Circle(frame, (int)pos2[0], (int)pos2[1], 5, Scalar.Green, 2);
// Cv2.Circle(frame, (int)pos3[0], (int)pos3[1], 5, Scalar.Blue, 2);
PointF[] location; PointF[] location;
try try
{ {
@@ -83,19 +93,6 @@ namespace BarcodeReaderExample
{ {
continue; continue;
} }
// Mat roi = frame[new Rect((int)pos3[0],(int)pos3[1], src.Width+3, src.Height)];
Mat roi = frame[new Rect((int)pos3[0], (int)pos3[1], width,heigh)];
src.CopyTo(roi);
// Console.WriteLine($"szerokość:{width}");
// Console.WriteLine($"wysokosc:{heigh}");
// Console.WriteLine($"x:{pos3[0]}");
// Console.WriteLine($"y:{pos3[1]}");
// DrawLine(location); //draw red squre
var points1 = new List<OpenCvSharp.Point> var points1 = new List<OpenCvSharp.Point>
{ {
GetPoint(location[0]), GetPoint(location[0]),
@@ -103,23 +100,28 @@ namespace BarcodeReaderExample
GetPoint(location[3]), GetPoint(location[3]),
GetPoint(location[2]) GetPoint(location[2])
}; };
int qrWidth = GetPoint(location[1]).X - GetPoint(location[0]).X;
// Zagnieźdź w dodatkową kolekcję: int qrHeigh = GetPoint(location[2]).Y - GetPoint(location[0]).Y;
IEnumerable<IEnumerable<OpenCvSharp.Point>> contours = new[] { points1 };
// Cv2.FillPoly(frame, contours, Scalar.White); // fill with white imgX = GetPoint(location[0]).X;
imgY = GetPoint(location[0]).Y;
contours = new[] { points1 };
Cv2.PutText(frame, result.Text, new OpenCvSharp.Point((int)(pos2[0]-pos3[0])/2+(int)pos3[0]-160, (int)(pos2[1]-pos3[1])/2+(int)pos3[1]+60), HersheyFonts.HersheySimplex, 0.8, Scalar.Black, 2);
Cv2.FillPoly(frame, contours, Scalar.White); // fill with white
//Write Text
// Cv2.PutText(frame, result.Text, new OpenCvSharp.Point((int)(pos2[0]-pos3[0])/2+(int)pos3[0]-160, (int)(pos2[1]-pos3[1])/2+(int)pos3[1]+60), HersheyFonts.HersheySimplex, 0.8, Scalar.Black, 2);
if (!files.Contains(result.Text)) if (!files.Contains(result.Text))
{ {
files[files_index] = result.Text; files[files_index] = result.Text;
Console.WriteLine("downloading: "+result.Text); Console.WriteLine("downloading: " + result.Text);
using (var client = new WebClient()) using (var client = new WebClient())
{ {
client.DownloadFile(result.Text, "cache/"+files_index + ".png"); client.DownloadFile(result.Text, "cache/" + files_index + ".png");
// Thread.Sleep(2000); // Thread.Sleep(2000);
} }
@@ -128,37 +130,54 @@ namespace BarcodeReaderExample
} }
else else
{ {
// try
// {
int toload = files.IndexOf(result.Text); int toload = files.IndexOf(result.Text);
Mat toDraw= new Mat();
InputArray inputArray = toDraw;
// Console.WriteLine($"{qrWidth} , {qrHeigh}");
try
{
Cv2.Resize(Cached[toload],toDraw,new OpenCvSharp.Size(qrWidth,qrHeigh));
}
catch
{continue;}
Lastimage = toDraw;
// toDraw.Resize(1,3);
try
{
Mat roi2 = frame[new Rect(GetPoint(location[0]).X, GetPoint(location[0]).Y, toDraw.Width, toDraw.Height)];
toDraw.CopyTo(roi2);
}
catch{}
Mat roi2 = frame[new Rect((int)pos3[0], (int)pos3[1], Cached[toload].Width, Cached[toload].Height)];
Cached[toload].CopyTo(roi2);
// Console.WriteLine("work");
// }
// catch { }
} }
// Cv2.draw }
else if (whenWasLastTimeQrDetected > 0)
{
Cv2.FillPoly(frame, contours, Scalar.White); // fill with white
// Cv2.Circle(frame, (int)(RightBottomX), (int)(RightBottomY), 5, Scalar.Green, 2); try
{
Mat roi2 = frame[new Rect(imgX, imgY, Lastimage.Width, Lastimage.Height)];
Lastimage.CopyTo(roi2);
}
catch{}
whenWasLastTimeQrDetected--;
//Cv2.Rectangle(frame, new Point((int)X, (int)Y), new Point ((int)Width, (int)Height), Scalar.Red, 2);
// Console.WriteLine(result.Text);
} }
// frame.SaveImage($"photos/photo_{photocount}.png");
// photocount++;
Cv2.ImShow("Camera preview", frame); Cv2.ImShow("Camera preview", frame);
Thread.Sleep(1000 / 24); Thread.Sleep(1000 / fps);
// Console.WriteLine("tick");
int key = Cv2.WaitKey(1); int key = Cv2.WaitKey(1);
if (key == 27 || key == 'q') if (key == 27 || key == 'q')
break; break;
@@ -167,9 +186,13 @@ namespace BarcodeReaderExample
Cv2.DestroyAllWindows(); Cv2.DestroyAllWindows();
} }
static void rotate()
{
}
static OpenCvSharp.Point GetPoint(PointF pointf) static OpenCvSharp.Point GetPoint(PointF pointf)
{ {
return new OpenCvSharp.Point(pointf.X,pointf.Y); return new OpenCvSharp.Point(pointf.X, pointf.Y);
} }
static PointF[] Calculate(float[] pos1, float[] pos2, float[] pos3, float[] pos4) static PointF[] Calculate(float[] pos1, float[] pos2, float[] pos3, float[] pos4)
{ {
@@ -207,7 +230,7 @@ namespace BarcodeReaderExample
PointF topRight = Expand(p2); PointF topRight = Expand(p2);
PointF bottomRight = ExpandLB(p4); PointF bottomRight = ExpandLB(p4);
PointF bottomLeft = Expand(p1); PointF bottomLeft = Expand(p1);
PointF[] Location = new PointF[] {topLeft,topRight,bottomLeft,bottomRight}; PointF[] Location = new PointF[] { topLeft, topRight, bottomLeft, bottomRight };
return Location; return Location;
// 4. Now draw the 4 lines // 4. Now draw the 4 lines
// Example with System.Drawing or Unity or whatever you use: // Example with System.Drawing or Unity or whatever you use:
@@ -234,7 +257,7 @@ namespace BarcodeReaderExample
} }
static Mat frame = new Mat(); static Mat frame = new Mat();
static Result GetResult(Mat frame) static Result ReadQrCode(Mat frame)
{ {
QRCodeReader reader = new QRCodeReader(); QRCodeReader reader = new QRCodeReader();
@@ -288,13 +311,3 @@ namespace BarcodeReaderExample
} }
} }