Example #1
0
        static void Main(string[] args)
        {
            using var straightQrCode = new Mat();
            var qr = new QRCodeDetector();


            var capture = new VideoCapture();

            capture.Open(0);

            using (var normalWindow = new Window("QRCode"))
            {
                var normalFrame = new Mat();
                var srFrame     = new Mat();
                while (true)
                {
                    capture.Read(normalFrame);
                    normalWindow.ShowImage(normalFrame);


                    var QRCodeContent = qr.DetectAndDecode(normalFrame, out var point);
                    if (QRCodeContent != "")
                    {
                        Console.WriteLine(QRCodeContent);
                    }


                    Cv2.WaitKey(500);
                }
            }
        }
        public void DetectAndDecode()
        {
            using var obj = new QRCodeDetector();
            int x = 100;
            int y = 200;

            using var withQr = ImageWithQrCode(x, y, out int w, out int h);
            ShowImagesWhenDebugMode(withQr);

            using var straightQrCode = new Mat();
            var decodedString = obj.DetectAndDecode(withQr, out var points, straightQrCode);

            Assert.Equal(4, points.Length);
            Assert.Equal(102, points[0].X);
            Assert.Equal(201, points[0].Y);
            Assert.Equal(199, points[1].X);
            Assert.Equal(201, points[1].Y);
            Assert.Equal(199, points[2].X);
            Assert.Equal(299, points[2].Y);
            Assert.Equal(102, points[3].X);
            Assert.Equal(299, points[3].Y);

            Assert.False(straightQrCode.Empty());
            Assert.Equal("https://github.com/opencv/opencv", decodedString);
        }
Example #3
0
        public static void QrCodeTest()
        {
            Glb.DrawMatAndHist0(Glb.matSrc);

            var matGray = Glb.matSrc.CvtColor(ColorConversionCodes.BGR2GRAY);

            Glb.DrawMatAndHist1(matGray);

            QRCodeDetector detector = new QRCodeDetector();

            Point2f[] points;
            string    text = detector.DetectAndDecode(matGray, out points);

            Console.WriteLine("points : " + string.Join(",", points.Select(pt => pt.ToString())));
            Console.WriteLine(text);

            var matRet = matGray.CvtColor(ColorConversionCodes.GRAY2BGR);
            var pts    = points.Select(pt => new OpenCvSharp.Point(pt.X, pt.Y));
            var ptss   = Enumerable.Repeat(pts, 1);

            matRet.Polylines(ptss, true, Scalar.Lime);
            Glb.DrawMatAndHist2(matRet);

            matGray.Dispose();
            matRet.Dispose();
        }
        public void ExplicitlyDetectMulti()
        {
            using var obj = new QRCodeDetector();

            using var withQr    = Image("qr_multi.png");
            using var pointsMat = new Mat();
            ShowImagesWhenDebugMode(withQr);

            bool detected = obj.DetectMulti(withQr, out var points);

            Assert.True(detected);
            Assert.Equal(8, points.Length);

            var expectedPoints = new[]
            {
                new Point2f(39, 39),
                new Point2f(260, 39),
                new Point2f(260, 260),
                new Point2f(39, 260),

                new Point2f(334, 334),
                new Point2f(565, 334),
                new Point2f(565, 565),
                new Point2f(334, 565),
            };

            AreEquivalent(expectedPoints, points);
        }
        public void ExplicitlyDetectAndDecode()
        {
            using var obj = new QRCodeDetector();
            const int x = 100;
            const int y = 200;

            using var withQr    = ImageWithQrCode(x, y, out var w, out var h);
            using var pointsMat = new Mat();
            ShowImagesWhenDebugMode(withQr);

            bool detected = obj.Detect(withQr, out var points);

            Assert.True(detected);
            Assert.Equal(4, points.Length);
            Assert.Equal(102, points[0].X, 6);
            Assert.Equal(201, points[0].Y, 6);
            Assert.Equal(199, points[1].X, 6);
            Assert.Equal(201, points[1].Y, 6);
            Assert.Equal(199, points[2].X, 6);
            Assert.Equal(299, points[2].Y, 6);
            Assert.Equal(102, points[3].X, 6);
            Assert.Equal(299, points[3].Y, 6);

            using var straightQrCode = new Mat();
            obj.Decode(withQr, points);
            var decodedString = obj.Decode(withQr, points, straightQrCode);

            Assert.False(straightQrCode.Empty());
            Assert.Equal("https://github.com/opencv/opencv", decodedString);
        }
Example #6
0
        private string  RecongizeQRCode(Mat mat)
        {
            Mat            dstMat     = new Mat();
            QRCodeDetector QRdetecter = new QRCodeDetector();
            bool           res        = QRdetecter.Detect(mat, dstMat); //检测
            string         str        = QRdetecter.Decode(mat, dstMat); //解码

            return(str);
        }
        public void DecodeMultibyteString()
        {
            using var obj    = new QRCodeDetector();
            using var withQr = Image("qr_multibyte_letters.png");

            var decodedString = obj.DetectAndDecode(withQr, out var points);

            Assert.Equal(4, points.Length);
            Assert.Equal("Helloこんにちは你好안녕하세요", decodedString);
        }
        public void DecodeSinglebyteString()
        {
            using var obj    = new QRCodeDetector();
            using var withQr = Image("qr_singlebyte_letters.png");

            var decodedString = obj.DetectAndDecode(withQr, out var points);

            Assert.Equal(4, points.Length);
            Assert.Equal("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[]^_`{|}", decodedString);
        }
        public void EmptyDetectMulti()
        {
            var lenna = Image("lenna.png");

            using var obj = new QRCodeDetector();

            bool detected = obj.DetectMulti(lenna, out var points);

            Assert.False(detected);
            Assert.Empty(points);
        }
Example #10
0
    void OnNewSample(Frame frame)
    {
        if (isDetecting)
        {
            return;
        }

        using (var frameSet = frame.AsFrameSet())
        {
            using (var colorFrame = frameSet.ColorFrame)
            {
                if (colorFrame == null)
                {
                    return;
                }
                // set isDetecting true to avoid crashes
                isDetecting = true;
                int cfWidth  = colorFrame.Width;
                int cfHeight = colorFrame.Height;
                // transform realsense frame into cv mat format
                using (var image = new Mat(cfHeight, cfWidth, MatType.CV_8UC3, colorFrame.Data))
                {
                    using (qrCodeDetector = new QRCodeDetector())
                    {
                        // Show generated Image in separate window
                        // Window.ShowImages(image);
                        // check frame if qrcode is existing
                        bool detected = qrCodeDetector.Detect(image, out var points);

                        if (detected)
                        {
                            using (var straightQrCode = new Mat())
                            {
                                qrCodeDetector.Decode(image, points);
                                var decodedString = qrCodeDetector.Decode(image, points, straightQrCode);
                                if (!string.IsNullOrEmpty(decodedString))
                                {
                                    if (decodedString == "tiger")
                                    {
                                        actionEventQueue.Enqueue("spawnTiger");
                                    }
                                    if (decodedString == "house")
                                    {
                                        actionEventQueue.Enqueue("spawnHouse");
                                    }
                                }
                            }
                        }
                        isDetecting = false;
                    }
                }
            }
        }
    }
Example #11
0
    // Use this for initialization
    void Start()
    {
        webCamTextureToMatHelper = gameObject.GetComponent <WebCamTextureToMatHelper>();

        detector = new QRCodeDetector();

        #if UNITY_ANDROID && !UNITY_EDITOR
        // Avoids the front camera low light issue that occurs in only some Android devices (e.g. Google Pixel, Pixel2).
        webCamTextureToMatHelper.avoidAndroidFrontCameraLowLightIssue = true;
        #endif
        webCamTextureToMatHelper.Initialize();
    }
        private void Run()
        {
            Texture2D imgTexture = Resources.Load("link_github_ocv") as Texture2D;

            Mat imgMat = new Mat(imgTexture.height, imgTexture.width, CvType.CV_8UC4);

            Utils.texture2DToMat(imgTexture, imgMat);
            Debug.Log("imgMat.ToString() " + imgMat.ToString());


            Mat grayMat = new Mat();

            Imgproc.cvtColor(imgMat, grayMat, Imgproc.COLOR_RGBA2GRAY);

            Mat points          = new Mat();
            Mat straight_qrcode = new Mat();

            QRCodeDetector detector = new QRCodeDetector();

            bool result = detector.detect(grayMat, points);

            if (result)
            {
                string decode_info = detector.decode(grayMat, points, straight_qrcode);

                Debug.Log(decode_info);
                Debug.Log(points.dump());
                Debug.Log(straight_qrcode.dump());

                // draw QRCode contour.
                float[] points_arr = new float[8];
                points.get(0, 0, points_arr);
                Imgproc.line(imgMat, new Point(points_arr [0], points_arr [1]), new Point(points_arr [2], points_arr [3]), new Scalar(255, 0, 0, 255), 2);
                Imgproc.line(imgMat, new Point(points_arr [2], points_arr [3]), new Point(points_arr [4], points_arr [5]), new Scalar(255, 0, 0, 255), 2);
                Imgproc.line(imgMat, new Point(points_arr [4], points_arr [5]), new Point(points_arr [6], points_arr [7]), new Scalar(255, 0, 0, 255), 2);
                Imgproc.line(imgMat, new Point(points_arr [6], points_arr [7]), new Point(points_arr [0], points_arr [1]), new Scalar(255, 0, 0, 255), 2);

                Imgproc.putText(imgMat, "DECODE INFO: " + decode_info, new Point(5, imgMat.rows() - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 0.7, new Scalar(255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
            }

            Texture2D texture = new Texture2D(imgMat.cols(), imgMat.rows(), TextureFormat.RGBA32, false);

            Utils.matToTexture2D(imgMat, texture);

            gameObject.GetComponent <Renderer> ().material.mainTexture = texture;
        }
        public static string DecodeQR(string inputImagePath)
        {
            Mat            inputImage = new Mat(inputImagePath);
            QRCodeDetector qrDecoder  = new QRCodeDetector();

            Point2f[] point2Fs;
            string    data = qrDecoder.DetectAndDecode(inputImage, out point2Fs);

            if (data.Length > 0)

            {
                Display(inputImage, point2Fs, inputImagePath);
                inputImage.Dispose();
                qrDecoder.Dispose();
                return(data);
            }

            else
            {
                throw new Exception("QR Not Found");
                inputImage.Dispose();
                qrDecoder.Dispose();
            }
        }
        public void ExplicitlyDecodeMulti()
        {
            var expectedDecodedStrings = new[]
            {
                "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[]^_`{|}",
                "Helloこんにちは你好안녕하세요"
            };

            using var obj = new QRCodeDetector();

            using var withQr    = Image("qr_multi.png");
            using var pointsMat = new Mat();
            ShowImagesWhenDebugMode(withQr);

            bool detected = obj.DetectMulti(withQr, out var points);

            Assert.True(detected);
            Assert.Equal(8, points.Length);

            bool decoded = obj.DecodeMulti(withQr, points, out var decodedStrings, out var straightQrCode);

            Assert.True(decoded);
            Assert.Equal(2, decodedStrings.Length);
            AreEquivalent(expectedDecodedStrings, decodedStrings);

            foreach (var mat in straightQrCode)
            {
                ShowImagesWhenDebugMode(mat);
            }

            bool decodedWithoutStraightQrCode = obj.DecodeMulti(withQr, points, out decodedStrings);

            Assert.True(decodedWithoutStraightQrCode);
            Assert.Equal(2, decodedStrings.Length);
            AreEquivalent(expectedDecodedStrings, decodedStrings);
        }