Example #1
0
        private Bitmap DecodeMat(Mat mat)
        {
            Result[]? results = reader.DecodeBuffer(mat.Data, mat.Cols, mat.Rows, (int)mat.Step(), BarcodeQRCodeReader.ImagePixelFormat.IPF_RGB_888);
            if (results != null)
            {
                foreach (Result result in results)
                {
                    string output = "Text: " + result.Text + Environment.NewLine + "Format: " + result.Format1 + Environment.NewLine;
                    textBox1.AppendText(output);
                    textBox1.AppendText(Environment.NewLine);
                    int[]? points = result.Points;
                    if (points != null)
                    {
                        OpenCvSharp.Point[] all = new OpenCvSharp.Point[4];
                        int xMin = points[0], yMax = points[1];
                        all[0] = new OpenCvSharp.Point(xMin, yMax);
                        for (int i = 2; i < 7; i += 2)
                        {
                            int x = points[i];
                            int y = points[i + 1];
                            OpenCvSharp.Point p = new OpenCvSharp.Point(x, y);
                            xMin       = x < xMin ? x : xMin;
                            yMax       = y > yMax ? y : yMax;
                            all[i / 2] = p;
                        }
                        OpenCvSharp.Point[][] contours = new OpenCvSharp.Point[][] { all };
                        Cv2.DrawContours(mat, contours, 0, new Scalar(0, 0, 255), 2);
                        if (result.Text != null)
                        {
                            Cv2.PutText(mat, result.Text, new OpenCvSharp.Point(xMin, yMax), HersheyFonts.HersheySimplex, 1, new Scalar(0, 0, 255), 2);
                        }
                    }
                }
            }
            else
            {
                textBox1.AppendText("No barcode detected!" + Environment.NewLine);
            }

            Bitmap bitmap = BitmapConverter.ToBitmap(mat);

            return(bitmap);
        }