using System;
using System.Drawing;
using System.IO;
using System.Net;
using MessagingToolkit.QRCode.Codec;
namespace QRLogo
{
///
/// The QR_Logo_Ver6 Class creates a Version 6 QR Code from given data, while incorporating a given image, thus creating a QRLogo
///
public class QR_Logo_Ver6
{
#region Declare Class Parameters
///
/// The constants in the base paremeters are chosen solely for the esthetic value, as they output eye pleasing results.
///
#region Declare Work Area Base Parameters
private const int BaseX = 80;
private const int BaseY = 70;
private const int BaseWidth = 60;
private const int BaseHeight = 70;
#endregion
///
/// The constant in the base parameters are chose for functionality. A version 6, byte encoded, High error correction code best suits image insertion.
///
#region Declare QRCode Base Parameters
private const int Scale = 4;
private const int Version = 6;
private const QRCodeEncoder.ENCODE_MODE Mode = QRCodeEncoder.ENCODE_MODE.BYTE;
private const QRCodeEncoder.ERROR_CORRECTION ErrCorrection = QRCodeEncoder.ERROR_CORRECTION.H;
#endregion
#region Declare Work Area Parameters
private int startPointX; // The X value of the (0,0) point of the work area
private int startPointY; // The Y value of the (0,0) point of the work area
private int workAreaWidth; // The Width of the work area
private int workAreaHeight; // The Height of the work area
#endregion
#region Declare QRCode Parameters
private QRCodeEncoder encodeData; // QRCodeEncdoer object that encodes the data passes to it to a bitmap of a QRCode.
private Bitmap QRCode; // A bitmap of a QRCode.
#endregion
///
/// The constants in the base paremeters are chosen bot for the esthetic value, as they output eye pleasing results, and for functional reasons so as to maintain a working QRCode.
///
#region Declare Pre-Processing Parameters
private const int MaxPercentLogo = 20;
private const int MaxPercentWidth = 60;
private const int MaxPercentHeight = 60;
private const int ResizePercent = 60;
private bool reduceImage;
#endregion
#endregion
///
/// Initializes a new instance of the QR_Logo_Ver6 class.
///
/// The data to be encoded into the QR Code
/// The logo to be incorporated into the QR Code to create a QRLogo
public QR_Logo_Ver6(string data, Bitmap logo)
{
#region Assign Variables
#region Assign QRCode Parameters
encodeData = new QRCodeEncoder();
encodeData.QRCodeScale = Scale;
encodeData.QRCodeVersion = Version;
encodeData.QRCodeEncodeMode = Mode;
encodeData.QRCodeErrorCorrect = ErrCorrection;
#endregion
#region Assign Work Area Parameters
startPointX = BaseX;
startPointY = BaseY;
workAreaWidth = BaseWidth;
workAreaHeight = BaseHeight;
#endregion
#region Assign Pre-Processing Parameters
reduceImage = true;
#endregion
#endregion
#region Encode QRCode
data = TinyURL(data);
QRCode = encodeData.Encode(data);
#endregion
#region Pre-Process
#region Optimise Sizes
while (logo.Height * logo.Width > (MaxPercentLogo * (QRCode.Height * QRCode.Width)) / 100 || logo.Width > MaxPercentWidth * QRCode.Width / 100 || logo.Height > MaxPercentHeight * QRCode.Height / 100)
{
if (reduceImage)
{
logo = new Bitmap(logo, new Size(ResizePercent * logo.Width / 100, ResizePercent * logo.Height / 100));
reduceImage = false;
}
else
{
encodeData.QRCodeScale = Convert.ToInt16(encodeData.QRCodeScale++);
QRCode = encodeData.Encode(data);
}
}
#endregion
#region Re-Assign Work Area Parameters
startPointX = (QRCode.Width / 2) - (logo.Width / 2);
startPointY = (QRCode.Height / 2) - (logo.Height / 2);
workAreaWidth = logo.Width;
workAreaHeight = logo.Height;
#endregion
#endregion
#region insert
for (int y = 0; y < workAreaHeight; y++)
{
for (int x = 0; x < workAreaWidth; x++)
{
QRCode.SetPixel(startPointX + x, startPointY + y, logo.GetPixel(x, y));
}
}
#endregion
}
///
/// This method performs a call to the TinyURL API for a given URL
///
/// This is the URL that's shortened by the method
/// A TinyURL shortened version of the parameter "shorten"
public static string TinyURL(string shorten)
{
HttpWebRequest APIRequest;
HttpWebResponse APIResponse;
TextReader resultReader;
string tinyURL;
APIRequest = (HttpWebRequest)WebRequest.Create("http://tinyurl.com/api-create.php?url=" + shorten);
APIResponse = (HttpWebResponse)APIRequest.GetResponse();
resultReader = new StreamReader(APIResponse.GetResponseStream());
tinyURL = resultReader.ReadToEnd();
return tinyURL;
}
///
/// The method saves a give SQLogos bitmap value to a file
///
/// The path at which the bitmap should be saved
public void Save(string saveLocation)
{
this.QRCode.Save(saveLocation);
}
}
}