/// <summary> /// Calls the my Azure API to shorten the long URL into an adddress like shl.pw/2v and returns QR Code image of the shortened URL /// </summary> /// <param name="sLongURL">URL to shorten and generate image of</param> /// <param name="iSize">Size of image to generate with ZXing</param> /// <param name="iCropBorder">Sometimes too much white space around QR Code and needs cropping</param> /// <returns>The (optionally cropped) QRCode Image of the shortened URL from ZXing</returns> public static QREncoder GetQREncoderObject(string sLongURL) { string url = "https://pwqrazurefunctionapp.azurewebsites.net/api/ShortenURLPost?code=bxwoszCCCjEGB2KTqiw/tcNCoNCKMdu9jdcbB2xiXcUoxPhMHWRTDA=="; try { // build WSG file link var linkToShorten = new ShortenThisURLWithAzure { Input = sLongURL }; HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url); HttpClient connectClient = new HttpClient(); connectClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var body = JSONHelper.SerializeJSon <ShortenThisURLWithAzure>(linkToShorten); request.Content = new StringContent(body, Encoding.UTF8, "application/json"); var response = connectClient.SendAsync(request).Result; string responseContent = response.Content.ReadAsStringAsync().Result; BPSUtilities.WriteLog(responseContent); // this is dumb ShortenedURLResponseFromAzure shortUrl = JSONHelper.DeserializeJSon <ShortenedURLResponseFromAzure>(responseContent.Replace("[", "").Replace("]", "")); if (shortUrl != null) { if (!string.IsNullOrEmpty(shortUrl.ShortUrl)) { BPSUtilities.WriteLog($"'{shortUrl.LongUrl}' shortened to '{shortUrl.ShortUrl}'"); QREncoder enc = new QREncoder(); enc.ErrorCorrection = ErrorCorrection.H; // this leads to a 33 x 33 qrcode enc.ModuleSize = 1; enc.QuietZone = 4; enc.Encode(shortUrl.ShortUrl); return(enc); } } else { BPSUtilities.WriteLog("Short URL not generated."); } } catch (Exception ex) { BPSUtilities.WriteLog("Error: " + ex.Message); BPSUtilities.WriteLog(ex.StackTrace); } return(null); }
/// <summary> /// Calls the my Azure API to shorten the long URL into an adddress like shl.pw/2v and returns QR Code image of the shortened URL /// </summary> /// <param name="sLongURL">URL to shorten and generate image of</param> /// <param name="iSize">Size of image to generate with ZXing</param> /// <param name="iCropBorder">Sometimes too much white space around QR Code and needs cropping</param> /// <returns>The (optionally cropped) QRCode Image of the shortened URL from ZXing</returns> public static Image GetQRCodeImage(string sLongURL, int iSize, int iCropBorder) { string url = "https://pwqrazurefunctionapp.azurewebsites.net/api/ShortenURLPost?code=bxwoszCCCjEGB2KTqiw/tcNCoNCKMdu9jdcbB2xiXcUoxPhMHWRTDA=="; Image img = null; try { // build WSG file link var linkToShorten = new ShortenThisURLWithAzure { Input = sLongURL }; HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url); HttpClient connectClient = new HttpClient(); connectClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var body = JSONHelper.SerializeJSon <ShortenThisURLWithAzure>(linkToShorten); request.Content = new StringContent(body, Encoding.UTF8, "application/json"); var response = connectClient.SendAsync(request).Result; string responseContent = response.Content.ReadAsStringAsync().Result; BPSUtilities.WriteLog(responseContent); // this is dumb ShortenedURLResponseFromAzure shortUrl = JSONHelper.DeserializeJSon <ShortenedURLResponseFromAzure>(responseContent.Replace("[", "").Replace("]", "")); if (shortUrl != null) { if (!string.IsNullOrEmpty(shortUrl.ShortUrl)) { BPSUtilities.WriteLog($"'{shortUrl.LongUrl}' shortened to '{shortUrl.ShortUrl}'"); img = (Image)CreateQRCode(shortUrl.ShortUrl, iSize); BPSUtilities.WriteLog($"Original QR Code size is {img.Width} x {img.Height}."); if (img.Width > iSize) { Image img3 = ResizeImage(img, iSize, iSize, ImageFormat.Png); img = img3; } BPSUtilities.WriteLog(string.Format("QR Code is {0} X {1}.", img.Width, img.Height, iCropBorder)); } } else { BPSUtilities.WriteLog("Short URL not generated."); } } catch (Exception ex) { BPSUtilities.WriteLog("Error: " + ex.Message); BPSUtilities.WriteLog(ex.StackTrace); } return(img); }