c1469b83a8
- Aggiornamento x metodo JSON encoded (x evitare problemi authKey)
299 lines
10 KiB
C#
299 lines
10 KiB
C#
using System.Drawing.Imaging;
|
|
using System.IO;
|
|
using System.Web;
|
|
using System.Web.Mvc;
|
|
using ZXing;
|
|
using ZXing.QrCode;
|
|
using Newtonsoft.Json;
|
|
using System.Collections.Generic;
|
|
using ZXing.Common;
|
|
using System.Reflection;
|
|
using System.Linq;
|
|
|
|
namespace QR_GEN.Controllers
|
|
{
|
|
public class HomeController : Controller
|
|
{
|
|
/// <summary>
|
|
/// Struct x richiesta serializzata/deserializzata
|
|
/// </summary>
|
|
public struct qrRequest
|
|
{
|
|
public string valore;
|
|
}
|
|
/// <summary>
|
|
/// Payload da deserializzare json
|
|
/// </summary>
|
|
public struct qrPayload
|
|
{
|
|
public string baseUrl { get; set; }
|
|
public string[] parameters { get; set; }
|
|
}
|
|
|
|
public ActionResult Index()
|
|
{
|
|
Assembly callingAssembly = Assembly.GetCallingAssembly();
|
|
ViewBag.Version = callingAssembly.GetName().Version;
|
|
string text = "Steamware";
|
|
object[] customAttributes = callingAssembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute), inherit: true);
|
|
if (customAttributes.Length != 0)
|
|
{
|
|
text = ((AssemblyCompanyAttribute)customAttributes[0]).Company;
|
|
}
|
|
ViewBag.CopyRight = text;
|
|
return View();
|
|
}
|
|
|
|
public RedirectResult steamware()
|
|
{
|
|
return Redirect("https://www.steamware.net");
|
|
}
|
|
#if false
|
|
/// <summary>
|
|
/// Restituisce un QR code dato valore
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public ActionResult QR(string id)
|
|
{
|
|
int hSize = 600;
|
|
int wSize = 600;
|
|
BarcodeFormat formato = BarcodeFormat.QR_CODE;
|
|
return getImage2D(ref id, hSize, wSize, formato);
|
|
}
|
|
/// <summary>
|
|
/// Restituisce un DataMatrix code dato valore
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public ActionResult DataMatrix(string id)
|
|
{
|
|
int hSize = 300;
|
|
int wSize = 600;
|
|
BarcodeFormat formato = BarcodeFormat.DATA_MATRIX;
|
|
return getImage2D(ref id, hSize, wSize, formato);
|
|
}
|
|
/// <summary>
|
|
/// Restituisce un Aztec code dato valore
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public ActionResult Aztec(string id)
|
|
{
|
|
int hSize = 600;
|
|
int wSize = 600;
|
|
BarcodeFormat formato = BarcodeFormat.AZTEC;
|
|
return getImage2D(ref id, hSize, wSize, formato);
|
|
}
|
|
/// <summary>
|
|
/// Restituisce un Aztec code dato valore
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public ActionResult Pdf417(string id)
|
|
{
|
|
int hSize = 300;
|
|
int wSize = 600;
|
|
BarcodeFormat formato = BarcodeFormat.PDF_417;
|
|
return getImage2D(ref id, hSize, wSize, formato);
|
|
}
|
|
/// <summary>
|
|
/// Restituisce un Code39 1D dato valore
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="pureBarcode">mostrare valore SOLO come codice (false --> testo sotto)</param>
|
|
/// <returns></returns>
|
|
public ActionResult code39(string id, bool pureBarcode = false)
|
|
{
|
|
int hSize = 70;
|
|
BarcodeFormat formato = BarcodeFormat.CODE_39;
|
|
return getImage(id, hSize, formato, pureBarcode);
|
|
}
|
|
/// <summary>
|
|
/// Restituisce un Code128 1D dato valore
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="pureBarcode">mostrare valore SOLO come codice (false --> testo sotto)</param>
|
|
/// <returns></returns>
|
|
public ActionResult Code128(string id, bool pureBarcode = false)
|
|
{
|
|
int hSize = 70;
|
|
BarcodeFormat formato = BarcodeFormat.CODE_128;
|
|
return getImage(id, hSize, formato, pureBarcode);
|
|
}
|
|
#endif
|
|
|
|
/// <summary>
|
|
/// Restituisce un QR code dato valore
|
|
/// </summary>
|
|
/// <param name="id">id x impostare SITO x jumper</param>
|
|
/// <param name="val">valore delle opzioni da embeddare</param>
|
|
/// <returns></returns>
|
|
public ActionResult QR_site(string id, string val)
|
|
{
|
|
string cmdArg = "";
|
|
// se vuoto stea,ware...
|
|
if (id == null || id == "")
|
|
{
|
|
cmdArg = "www.steamware.net";
|
|
}
|
|
#if false
|
|
// se arriva modo json --> deserializzo
|
|
if (id.IndexOf("{") >= 0)
|
|
{
|
|
var richiesta = JsonConvert.DeserializeObject<qrRequest>(id);
|
|
id = richiesta.valore;
|
|
}
|
|
#endif
|
|
|
|
|
|
#if false
|
|
// url decode...
|
|
id = HttpUtility.UrlDecode(id);
|
|
#endif
|
|
|
|
// valutare se usare file web.config, file .ini, file conf generico...
|
|
|
|
string tgtUrl = "";
|
|
// se l'
|
|
|
|
// in base a richiesta compongo stringa ...
|
|
switch (id)
|
|
{
|
|
case "CTRACK_ELET_SCAL":
|
|
tgtUrl = @"ctrack.steamware.net/elet-scal/jumper?USER_NAME=info@elettronicascalvina.it&UserAuthkey=asdfbqhewrqg7802345bhasdfg78&CodOpr=";
|
|
break;
|
|
case "CTRACK_ELET_SCAL_UFF":
|
|
tgtUrl = @"http://192.168.1.117/CTRACK/jumper?USER_NAME=info@elettronicascalvina.it&UserAuthkey=asdfbqhewrqg7802345bhasdfg78&CodOpr=";
|
|
break;
|
|
case "CTRACK_SERIATE":
|
|
tgtUrl = "https://iis01.egalware.com/CTRACK/jumper?USER_NAME=info@elettronicascalvina.it&UserAuthkey=asdfbqhewrqg7802345bhasdfg78&CodOpr=";
|
|
break;
|
|
case "CTRACK_COLCOM":
|
|
tgtUrl = @"http://192.168.0.113/MP/CTRACK/jumper?USER_NAME=opr_mon&UserAuthkey=AK_sdurb4930r2d&CodOpr=";
|
|
break;
|
|
case "JSON":
|
|
// devo DESERIALIZZARE...
|
|
var obj = JsonConvert.DeserializeObject<qrPayload>(val);
|
|
// metto tutto in tgtUrl...
|
|
tgtUrl = string.Format(obj.baseUrl, obj.parameters);
|
|
// svuoto val...
|
|
val = "";
|
|
break;
|
|
case "JSON_ENC":
|
|
// devo DESERIALIZZARE...
|
|
var objRaw = JsonConvert.DeserializeObject<qrPayload>(val);
|
|
// eseguo urlencode dei parametri...
|
|
var objEnc = objRaw.parameters.Select(x => HttpUtility.UrlEncode(x)).ToArray();
|
|
// metto tutto in tgtUrl...
|
|
tgtUrl = string.Format(objRaw.baseUrl, objEnc);
|
|
// svuoto val...
|
|
val = "";
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
cmdArg = string.Format("{0}{1}", tgtUrl, val);
|
|
// creo!
|
|
QrCodeEncodingOptions options = new QrCodeEncodingOptions
|
|
{
|
|
DisableECI = true,
|
|
CharacterSet = "UTF-8",
|
|
Width = 600,
|
|
Height = 600,
|
|
Margin = 0
|
|
};
|
|
var writer = new BarcodeWriter();
|
|
writer.Format = BarcodeFormat.QR_CODE;
|
|
writer.Options = options;
|
|
// scrivo bitmap
|
|
var pixelData = writer.Write(cmdArg);
|
|
|
|
// Return Image
|
|
MemoryStream ms = new MemoryStream();
|
|
pixelData.Save(ms, ImageFormat.Png);
|
|
ms.Position = 0;
|
|
return new FileStreamResult(ms, "image/png");
|
|
}
|
|
|
|
|
|
#if false
|
|
/// <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>
|
|
private 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>
|
|
private 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 = 600,
|
|
Height = 600,
|
|
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");
|
|
}
|
|
#endif
|
|
}
|
|
} |