modified: Program.cs
This commit is contained in:
+79
-40
@@ -6,6 +6,7 @@ using ZXing.Windows.Compatibility;
|
|||||||
using ZXing.Common;
|
using ZXing.Common;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
|
// using Emgu.CV.CvEnum;
|
||||||
|
|
||||||
|
|
||||||
namespace BarcodeReaderExample
|
namespace BarcodeReaderExample
|
||||||
@@ -21,7 +22,7 @@ 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, 5);
|
camera.Set(VideoCaptureProperties.Fps, 60);
|
||||||
|
|
||||||
|
|
||||||
Mat hey = new Mat("photo.png");
|
Mat hey = new Mat("photo.png");
|
||||||
@@ -54,17 +55,42 @@ namespace BarcodeReaderExample
|
|||||||
if (result != null)
|
if (result != null)
|
||||||
{
|
{
|
||||||
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
|
||||||
|
|
||||||
// Cv2.Circle(frame, (int)pos1[0], (int)pos1[1], 5, Scalar.Red, 2);
|
// 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)pos2[0], (int)pos2[1], 5, Scalar.Green, 2);
|
||||||
// Cv2.Circle(frame, (int)pos3[0], (int)pos3[1], 5, Scalar.Blue, 2);
|
// Cv2.Circle(frame, (int)pos3[0], (int)pos3[1], 5, Scalar.Blue, 2);
|
||||||
|
PointF[] location;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
|
||||||
|
location = Calculate(pos1, pos2, pos3, new float[2] { points[3].X, points[3].Y });
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// DrawLine(location);
|
||||||
|
|
||||||
|
|
||||||
Calculate(pos1, pos2, pos3, new float[2] { points[3].X, points[3].Y });
|
var points1 = new List<OpenCvSharp.Point>
|
||||||
|
{
|
||||||
|
GetPoint(location[0]),
|
||||||
|
GetPoint(location[1]),
|
||||||
|
GetPoint(location[3]),
|
||||||
|
GetPoint(location[2])
|
||||||
|
};
|
||||||
|
|
||||||
|
// Zagnieźdź w dodatkową kolekcję:
|
||||||
|
IEnumerable<IEnumerable<OpenCvSharp.Point>> contours = new[] { points1 };
|
||||||
|
|
||||||
|
|
||||||
|
Cv2.FillPoly(frame, contours, Scalar.White);
|
||||||
|
|
||||||
|
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.Circle(frame, (int)(RightBottomX), (int)(RightBottomY), 5, Scalar.Green, 2);
|
// Cv2.Circle(frame, (int)(RightBottomX), (int)(RightBottomY), 5, Scalar.Green, 2);
|
||||||
|
|
||||||
@@ -74,12 +100,13 @@ namespace BarcodeReaderExample
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// frame.SaveImage($"photos/photo_{photocount}.png");
|
// frame.SaveImage($"photos/photo_{photocount}.png");
|
||||||
// photocount++;
|
// photocount++;
|
||||||
|
|
||||||
|
|
||||||
Cv2.ImShow("Camera preview", frame);
|
Cv2.ImShow("Camera preview", frame);
|
||||||
Thread.Sleep(1000 / 24);
|
// Thread.Sleep(1000 / 24);
|
||||||
// Console.WriteLine("tick");
|
// Console.WriteLine("tick");
|
||||||
int key = Cv2.WaitKey(1);
|
int key = Cv2.WaitKey(1);
|
||||||
if (key == 27 || key == 'q')
|
if (key == 27 || key == 'q')
|
||||||
@@ -89,54 +116,66 @@ namespace BarcodeReaderExample
|
|||||||
Cv2.DestroyAllWindows();
|
Cv2.DestroyAllWindows();
|
||||||
|
|
||||||
}
|
}
|
||||||
static void Calculate(float[] pos1, float[] pos2, float[] pos3, float[] pos4)
|
static OpenCvSharp.Point GetPoint(PointF pointf)
|
||||||
|
{
|
||||||
|
return new OpenCvSharp.Point(pointf.X,pointf.Y);
|
||||||
|
}
|
||||||
|
static PointF[] Calculate(float[] pos1, float[] pos2, float[] pos3, float[] pos4)
|
||||||
{
|
{
|
||||||
// Your 4 detected points (change the names if needed)
|
// Your 4 detected points (change the names if needed)
|
||||||
PointF p1 = new PointF(pos1[0], pos1[1]);
|
PointF p1 = new PointF(pos1[0], pos1[1]);
|
||||||
PointF p2 = new PointF(pos2[0], pos2[1]);
|
PointF p2 = new PointF(pos2[0], pos2[1]);
|
||||||
PointF p3 = new PointF(pos3[0], pos3[1]);
|
PointF p3 = new PointF(pos3[0], pos3[1]);
|
||||||
PointF p4 = new PointF(pos4[0], pos4[1]);
|
PointF p4 = new PointF(pos4[0], pos4[1]);
|
||||||
|
|
||||||
// 1. Find the center of the QR
|
// 1. Find the center of the QR
|
||||||
float centerX = (p1.X + p2.X + p3.X + p4.X) / 4f;
|
float centerX = (p1.X + p2.X + p3.X + p4.X) / 4f;
|
||||||
float centerY = (p1.Y + p2.Y + p3.Y + p4.Y) / 4f;
|
float centerY = (p1.Y + p2.Y + p3.Y + p4.Y) / 4f;
|
||||||
PointF center = new PointF(centerX, centerY);
|
PointF center = new PointF(centerX, centerY);
|
||||||
|
|
||||||
// 2. How much bigger you want the box (1.0 = exact size, 1.15 = 15% bigger)
|
// 2. How much bigger you want the box (1.0 = exact size, 1.15 = 15% bigger)
|
||||||
float scale = 1.35f; // ← change this value
|
float scale = 1.35f; // ← change this value
|
||||||
|
|
||||||
// 3. Move every corner away from the center
|
// 3. Move every corner away from the center
|
||||||
PointF Expand(PointF p)
|
PointF Expand(PointF p)
|
||||||
{
|
{
|
||||||
float dx = p.X - center.X;
|
float dx = p.X - center.X;
|
||||||
float dy = p.Y - center.Y;
|
float dy = p.Y - center.Y;
|
||||||
return new PointF(center.X + dx * scale, center.Y + dy * scale);
|
return new PointF(center.X + dx * scale, center.Y + dy * scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
float scaleLB = 1.75f; // ← change this value
|
float scaleLB = 1.75f; // ← change this value
|
||||||
PointF ExpandLB(PointF p)
|
PointF ExpandLB(PointF p)
|
||||||
{
|
{
|
||||||
float dx = p.X - center.X;
|
float dx = p.X - center.X;
|
||||||
float dy = p.Y - center.Y;
|
float dy = p.Y - center.Y;
|
||||||
return new PointF(center.X + dx * scaleLB, center.Y + dy * scaleLB);
|
return new PointF(center.X + dx * scaleLB, center.Y + dy * scaleLB);
|
||||||
}
|
}
|
||||||
|
|
||||||
PointF topLeft = Expand(p3); // change order if needed
|
PointF topLeft = Expand(p3); // change order if needed
|
||||||
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};
|
||||||
|
return Location;
|
||||||
|
// 4. Now draw the 4 lines
|
||||||
|
// Example with System.Drawing or Unity or whatever you use:
|
||||||
|
|
||||||
// 4. Now draw the 4 lines
|
|
||||||
// Example with System.Drawing or Unity or whatever you use:
|
|
||||||
DrawLine(topLeft, topRight);
|
|
||||||
DrawLine(topRight, bottomRight);
|
|
||||||
DrawLine(bottomRight, bottomLeft);
|
|
||||||
DrawLine(bottomLeft, topLeft);
|
|
||||||
}
|
}
|
||||||
static void DrawLine(PointF p1, PointF p2)
|
static void DrawLine(PointF p1, PointF p2)
|
||||||
{
|
{
|
||||||
Cv2.Line(frame, (int)p1.X, (int)p1.Y, (int)p2.X, (int)p2.Y, Scalar.Red, 2);
|
Cv2.Line(frame, (int)p1.X, (int)p1.Y, (int)p2.X, (int)p2.Y, Scalar.Red, 2);
|
||||||
|
|
||||||
|
}
|
||||||
|
static void DrawLine(PointF[] Location)
|
||||||
|
{
|
||||||
|
DrawLine(Location[0], Location[1]);
|
||||||
|
|
||||||
|
|
||||||
|
DrawLine(Location[1], Location[2]);
|
||||||
|
DrawLine(Location[2], Location[3]);
|
||||||
|
DrawLine(Location[3], Location[0]);
|
||||||
|
|
||||||
}
|
}
|
||||||
static void DrawLine(float x1, float y1, float x2, float y2)
|
static void DrawLine(float x1, float y1, float x2, float y2)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user