PKHeX/Misc/QR.cs

100 lines
5.2 KiB
C#
Raw Normal View History

using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Web;
using System.Windows.Forms;
namespace PKHeX
{
public partial class QR : Form
{
2015-02-26 07:12:38 +00:00
public QR(Image qr, Image pkm, string top, string bottom, string left, string right)
{
InitializeComponent();
Font font = Main.unicode ? FontLabel.Font : PKX.getPKXFont((float)8.25);
2015-02-26 07:12:38 +00:00
Image preview = new Bitmap(45, 45);
using (Graphics gfx = Graphics.FromImage(preview))
{
gfx.FillRectangle(new SolidBrush(Color.White), 0, 0, preview.Width, preview.Height);
gfx.DrawImage(pkm, preview.Width / 2 - pkm.Width / 2, preview.Height / 2 - pkm.Height / 2);
}
2015-03-14 02:59:51 +00:00
// Layer on Preview Image
Image pic = Util.LayerImage(qr, preview, qr.Width / 2 - preview.Width / 2, qr.Height / 2 - preview.Height / 2, 1);
2015-02-27 01:22:53 +00:00
// Layer on Text
2015-03-14 02:59:51 +00:00
const int stretch = 50;
Height += stretch;
Image newpic = new Bitmap(PB_QR.Width, PB_QR.Height);
using (Graphics g = Graphics.FromImage(newpic))
{
g.FillRectangle(new SolidBrush(Color.White), 0, 0, newpic.Width, newpic.Height);
g.DrawImage(pic, 0, 0);
g.DrawString(top, font, Brushes.Black, new PointF(18, qr.Height - 5));
g.DrawString(bottom, font, Brushes.Black, new PointF(18, qr.Height + 8));
g.DrawString(left.Replace(Environment.NewLine, "/").Replace("//", " ").Replace(":/", ": "), font, Brushes.Black, new PointF(18, qr.Height + 20));
g.DrawString(right, font, Brushes.Black, new PointF(18, qr.Height + 32));
}
2015-03-14 02:59:51 +00:00
PB_QR.BackgroundImage = newpic;
}
2015-02-25 04:42:05 +00:00
private void PB_QR_Click(object sender, EventArgs e)
{
if (DialogResult.Yes != Util.Prompt(MessageBoxButtons.YesNo, "Copy QR Image to Clipboard?")) return;
try { Clipboard.SetImage(PB_QR.BackgroundImage); }
catch { Util.Alert("Failed to set Image to Clipboard"); }
}
// QR Utility
internal static byte[] getQRData()
{
// Fetch data from QR code...
string address;
try { address = Clipboard.GetText(); }
catch { Util.Alert("No text (url) in clipboard."); return null; }
try { if (address.Length < 4 || address.Substring(0, 3) != "htt") { Util.Alert("Clipboard text is not a valid URL:", address); return null; } }
catch { Util.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
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(webURL);
HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse();
var reader = new StreamReader(httpWebReponse.GetResponseStream());
string data = reader.ReadToEnd();
if (data.Contains("could not find")) { Util.Alert("Reader could not find QR data in the image."); return null; }
if (data.Contains("filetype not supported")) { Util.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
string pkstr = data.Substring(data.IndexOf("#", StringComparison.Ordinal) + 1); // Trim intro
pkstr = pkstr.Substring(0, pkstr.IndexOf("\",\"error\":null}]}]", StringComparison.Ordinal)); // Trim outro
if (pkstr.Contains("nQR-Code:")) pkstr = pkstr.Substring(0, pkstr.IndexOf("nQR-Code:", StringComparison.Ordinal)); // Remove multiple QR codes in same image
pkstr = pkstr.Replace("\\", ""); // Rectify response
try { return Convert.FromBase64String(pkstr); }
catch { Util.Alert("QR string to Data failed.", pkstr); return null; }
}
catch { Util.Alert("Unable to connect to the internet to decode QR code."); return null; }
}
internal static Image getQRImage(byte[] data, string server)
{
string qrdata = Convert.ToBase64String(data);
string message = server + qrdata;
string webURL = "http://chart.apis.google.com/chart?chs=365x365&cht=qr&chl=" + HttpUtility.UrlEncode(message);
try
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(webURL);
HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream stream = httpWebReponse.GetResponseStream();
if (stream != null) return Image.FromStream(stream);
}
catch
{
if (DialogResult.Yes != Util.Prompt(MessageBoxButtons.YesNo, "Unable to connect to the internet to receive QR code.", "Copy QR URL to Clipboard?"))
return null;
try { Clipboard.SetText(webURL); }
catch { Util.Alert("Failed to set text to Clipboard"); }
}
return null;
}
}
}