88 lines
2.7 KiB
C#
88 lines
2.7 KiB
C#
using System.Drawing;
|
|
using System.Drawing.Imaging;
|
|
using System.IO;
|
|
using System.Web;
|
|
using System.Web.Mvc;
|
|
using ZXing;
|
|
using ZXing.Common;
|
|
using ZXing.QrCode;
|
|
|
|
public class utils
|
|
{
|
|
/// <summary>
|
|
/// Genera immagina barcode richeista secondo dimensioni e formato
|
|
/// </summary>
|
|
/// <param name="dataStr"></param>
|
|
/// <param name="hSize">altezza</param>
|
|
/// <param name="formato">formati 1D di ZXing</param>
|
|
/// <param name="pureBarcode">indica se SOLO barcode</param>
|
|
/// <returns></returns>
|
|
public static ActionResult getImage(string dataStr, int hSize, BarcodeFormat formato, bool pureBarcode)
|
|
{
|
|
// se vuoto steamware...
|
|
if (dataStr == null || dataStr == "")
|
|
{
|
|
dataStr = "www.steamware.net";
|
|
}
|
|
dataStr = dataStr.ToUpper();
|
|
// url decode...
|
|
dataStr = HttpUtility.UrlDecode(dataStr);
|
|
// creo!
|
|
EncodingOptions options = new EncodingOptions
|
|
{
|
|
Height = hSize,
|
|
Margin = 0,
|
|
PureBarcode = pureBarcode
|
|
};
|
|
BarcodeWriter writer = new BarcodeWriter();
|
|
writer.Format = formato;
|
|
writer.Options = options;
|
|
// scrivo bitmap
|
|
System.Drawing.Bitmap pixelData = writer.Write(dataStr);
|
|
|
|
// Return Image
|
|
MemoryStream ms = new MemoryStream();
|
|
pixelData.Save(ms, ImageFormat.Png);
|
|
ms.Position = 0;
|
|
return new FileStreamResult(ms, "image/png");
|
|
}
|
|
/// <summary>
|
|
/// Genera immagina barcode richeista secondo dimensioni e formato
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="hSize"></param>
|
|
/// <param name="wSize"></param>
|
|
/// <param name="formato"></param>
|
|
/// <returns></returns>
|
|
public static ActionResult getImage2D(ref string id, int hSize, int wSize, BarcodeFormat formato)
|
|
{
|
|
// se vuoto steamware...
|
|
if (id == null || id == "")
|
|
{
|
|
id = "www.steamware.net";
|
|
}
|
|
id = id.ToUpper();
|
|
// url decode...
|
|
id = HttpUtility.UrlDecode(id);
|
|
// creo!
|
|
QrCodeEncodingOptions options = new QrCodeEncodingOptions
|
|
{
|
|
DisableECI = true,
|
|
CharacterSet = "UTF-8",
|
|
Width = wSize,
|
|
Height = hSize,
|
|
Margin = 0
|
|
};
|
|
BarcodeWriter writer = new BarcodeWriter();
|
|
writer.Format = formato;
|
|
writer.Options = options;
|
|
// scrivo bitmap
|
|
System.Drawing.Bitmap pixelData = writer.Write(id);
|
|
|
|
// Return Image
|
|
MemoryStream ms = new MemoryStream();
|
|
pixelData.Save(ms, ImageFormat.Png);
|
|
ms.Position = 0;
|
|
return new FileStreamResult(ms, "image/png");
|
|
}
|
|
} |