/// <summary>
        /// Both detects and decodes QR code.
        /// To simplify the usage, there is a only API: detectAndDecode
        /// </summary>
        /// <param name="inputImage">supports grayscale or color(BGR) image.</param>
        /// <param name="bbox">optional output array of vertices of the found QR code quadrangle.Will be empty if not found.</param>
        /// <param name="results">list of decoded string.</param>
        public void DetectAndDecode(InputArray inputImage, out Mat[] bbox, out string[] results)
        {
            if (inputImage == null)
            {
                throw new ArgumentNullException(nameof(inputImage));
            }
            inputImage.ThrowIfDisposed();

            using var bboxVec = new VectorOfMat();
            using var texts   = new VectorOfString();
            NativeMethods.HandleException(
                NativeMethods.wechat_qrcode_WeChatQRCode_detectAndDecode(
                    ptr, inputImage.CvPtr, bboxVec.CvPtr, texts.CvPtr));

            bbox    = bboxVec.ToArray();
            results = texts.ToArray();
            GC.KeepAlive(this);
            GC.KeepAlive(inputImage);
        }
Example #2
0
        /// <summary>
        /// Decodes QR codes in image once it's found by the detect() method.
        /// Returns UTF8-encoded output string or empty string if the code cannot be decoded.
        /// </summary>
        /// <param name="img">grayscale or color (BGR) image containing QR code.</param>
        /// <param name="points">Quadrangle vertices found by detect() method (or some other algorithm).</param>
        /// <param name="decodedInfo">UTF8-encoded output vector of string or empty vector of string if the codes cannot be decoded. </param>
        /// <param name="straightQrCode">The optional output image containing rectified and binarized QR code</param>
        /// <param name="isOutputStraightQrCode"><see langword="true"/> to output <paramref name="straightQrCode"/></param>
        /// <returns></returns>
        protected bool DecodeMulti(InputArray img, IEnumerable <Point2f> points, out string?[] decodedInfo, out Mat[] straightQrCode, bool isOutputStraightQrCode)
        {
            if (img == null)
            {
                throw new ArgumentNullException(nameof(img));
            }
            if (points == null)
            {
                throw new ArgumentNullException(nameof(points));
            }

            img.ThrowIfDisposed();

            using var decodedInfoVec = new VectorOfString();
            using var pointsVec      = new VectorOfPoint2f(points);

            int ret;

            if (isOutputStraightQrCode)
            {
                using var straightQrCodeVec = new VectorOfMat();
                NativeMethods.HandleException(
                    NativeMethods.objdetect_QRCodeDetector_decodeMulti(
                        ptr, img.CvPtr, pointsVec.CvPtr, decodedInfoVec.CvPtr, straightQrCodeVec.CvPtr, out ret));
                straightQrCode = straightQrCodeVec.ToArray();
            }
            else
            {
                NativeMethods.HandleException(
                    NativeMethods.objdetect_QRCodeDetector_decodeMulti_NoStraightQrCode(
                        ptr, img.CvPtr, pointsVec.CvPtr, decodedInfoVec.CvPtr, out ret));
                straightQrCode = Array.Empty <Mat>();
            }

            // decode utf-8 bytes.
            decodedInfo = decodedInfoVec.ToArray();

            GC.KeepAlive(img);
            GC.KeepAlive(points);
            GC.KeepAlive(this);

            return(ret != 0);
        }