Пример #1
0
        internal static byte[] GetQRData(string address)
        {
            // Fetch data from QR code...
            try { if (address.Length < 4 || !address.StartsWith("http"))
                  {
                      WinFormsUtil.Alert(MsgQRUrlFailPath, address); return(Array.Empty <byte>());
                  }
            }
            catch { WinFormsUtil.Alert(MsgQRUrlFailPath, address); return(Array.Empty <byte>()); }
            string webURL = DecodeAPI + HttpUtility.UrlEncode(address);
            string data;

            try
            {
                data = NetUtil.GetStringFromURL(webURL);
                if (data.Contains("could not find"))
                {
                    WinFormsUtil.Alert(MsgQRUrlFailImage); return(Array.Empty <byte>());
                }
                if (data.Contains("filetype not supported"))
                {
                    WinFormsUtil.Alert(MsgQRUrlFailType, address); return(Array.Empty <byte>());
                }
            }
            catch { WinFormsUtil.Alert(MsgQRUrlFailConnection); return(Array.Empty <byte>()); }

            // Quickly convert the json response to a data string
            try { return(DecodeQRJson(data)); }
            catch (Exception e) { WinFormsUtil.Alert(MsgQRUrlFailConvert, e.Message); return(Array.Empty <byte>()); }
        }
Пример #2
0
        internal static byte[] GetQRData(string address)
        {
            // Fetch data from QR code...
            try { if (address.Length < 4 || !address.StartsWith("http"))
                  {
                      WinFormsUtil.Alert("Clipboard text is not a valid URL:", address); return(null);
                  }
            }
            catch { WinFormsUtil.Alert("Clipboard text is not a valid URL:", address); return(null); }
            string webURL = DecodeAPI + HttpUtility.UrlEncode(address);
            string data;

            try
            {
                data = NetUtil.GetStringFromURL(webURL);
                if (data.Contains("could not find"))
                {
                    WinFormsUtil.Alert("Reader could not find QR data in the image."); return(null);
                }
                if (data.Contains("filetype not supported"))
                {
                    WinFormsUtil.Alert("Input URL is not valid. Double check that it is an image (jpg/png).", address); return(null);
                }
            }
            catch { WinFormsUtil.Alert("Unable to connect to the internet to decode QR code."); return(null); }

            // Quickly convert the json response to a data string
            try { return(DecodeQRJson(data)); }
            catch (Exception e) { WinFormsUtil.Alert("QR string to Data failed.", e.Message); return(null); }
        }
Пример #3
0
        internal static byte[] GetQRData(string address)
        {
            // Fetch data from QR code...
            try { if (address.Length < 4 || address.Substring(0, 3) != "htt")
                  {
                      WinFormsUtil.Alert("Clipboard text is not a valid URL:", address); return(null);
                  }
            }
            catch { WinFormsUtil.Alert("Clipboard text is not a valid URL:", address); return(null); }
            string webURL = "http://api.qrserver.com/v1/read-qr-code/?fileurl=" + HttpUtility.UrlEncode(address);

            try
            {
                string data = NetUtil.GetStringFromURL(webURL);
                if (data.Contains("could not find"))
                {
                    WinFormsUtil.Alert("Reader could not find QR data in the image."); return(null);
                }
                if (data.Contains("filetype not supported"))
                {
                    WinFormsUtil.Alert("Input URL is not valid. Double check that it is an image (jpg/png).", address); return(null);
                }
                // Quickly convert the json response to a data string
                const string cap   = "\",\"error\":null}]}]";
                const string intro = "[{\"type\":\"qrcode\",\"symbol\":[{\"seq\":0,\"data\":\"";
                if (!data.StartsWith(intro))
                {
                    throw new FormatException();
                }

                string pkstr = data.Substring(intro.Length);
                if (pkstr.Contains("nQR-Code:")) // Remove multiple QR codes in same image
                {
                    pkstr = pkstr.Substring(0, pkstr.IndexOf("nQR-Code:", StringComparison.Ordinal));
                }
                pkstr = pkstr.Substring(0, pkstr.IndexOf(cap, StringComparison.Ordinal)); // Trim outro
                try
                {
                    if (!pkstr.StartsWith("http") && !pkstr.StartsWith(QR6PathBad)) // G7
                    {
                        string fstr = Regex.Unescape(pkstr);
                        byte[] raw  = Encoding.Unicode.GetBytes(fstr);
                        // Remove 00 interstitials and retrieve from offset 0x30, take PK7 Stored Size (always)
                        return(raw.ToList().Where((c, i) => i % 2 == 0).Skip(0x30).Take(0xE8).ToArray());
                    }
                    // All except G7
                    pkstr = pkstr.Substring(pkstr.IndexOf("#", StringComparison.Ordinal) + 1); // Trim URL
                    pkstr = pkstr.Replace("\\", "");                                           // Rectify response

                    return(Convert.FromBase64String(pkstr));
                }
                catch { WinFormsUtil.Alert("QR string to Data failed."); return(null); }
            }
            catch { WinFormsUtil.Alert("Unable to connect to the internet to decode QR code."); return(null); }
        }