127 lines
4.3 KiB
C#
127 lines
4.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
using System.IO;
|
|
using ZXing;
|
|
using ZXing.Common;
|
|
using ZXing.QrCode;
|
|
using System.Drawing.Imaging;
|
|
using System.Drawing;
|
|
using ZXing.Rendering;
|
|
using System.Data;
|
|
|
|
namespace EgwCoreLib.Razor
|
|
{
|
|
/// <summary>
|
|
/// Classe gestione rendering QRCode, da XZing
|
|
///
|
|
/// https://github.com/micjahn/ZXing.Net/
|
|
/// https://stackoverflow.com/questions/72114518/show-an-image-stream-to-client-in-blazor-server-without-javascript
|
|
///
|
|
/// valutare output SVG
|
|
/// https://www.nayuki.io/page/qr-code-generator-library#third-party-ports
|
|
/// https://github.com/manuelbl/QrCodeGenerator
|
|
/// https://github.com/manuelbl/QrCodeGenerator/blob/master/Demo-ASP.NET-Core/QrCodeController.cs
|
|
/// </summary>
|
|
public class BCodeUtils
|
|
{
|
|
/// <summary>
|
|
/// Genera immagina barcode richiesta 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>
|
|
/// <param name="forceUpper">indica se forzare i caratteri a maiuscolo</param>
|
|
/// <returns></returns>
|
|
public static Stream getImage(string dataStr, int hSize, BarcodeFormat formato, bool pureBarcode, bool forceUpper)
|
|
{
|
|
// se vuoto steamware...
|
|
if (dataStr == null || dataStr == "")
|
|
{
|
|
dataStr = "www.steamware.net";
|
|
}
|
|
if(forceUpper)
|
|
{
|
|
dataStr = dataStr.ToUpper();
|
|
}
|
|
// url decode...
|
|
dataStr = HttpUtility.UrlDecode(dataStr);
|
|
// creo!
|
|
EncodingOptions options = new EncodingOptions
|
|
{
|
|
Height = hSize,
|
|
Margin = 0,
|
|
PureBarcode = pureBarcode
|
|
};
|
|
var writer = new ZXing.Windows.Compatibility.BarcodeWriter()
|
|
{
|
|
Format = formato,
|
|
Options = options
|
|
};
|
|
//writer.Format = formato;
|
|
//writer.Options = options;
|
|
// scrivo bitmap
|
|
Bitmap pixelData = writer.Write(dataStr);
|
|
|
|
// Return Image
|
|
MemoryStream ms = new MemoryStream();
|
|
pixelData.Save(ms, ImageFormat.Png);
|
|
ms.Position = 0;
|
|
return ms;
|
|
//return new FileStreamResult(ms, "image/png");
|
|
}
|
|
/// <summary>
|
|
/// Genera immagina barcode richiesta secondo dimensioni e formato
|
|
/// </summary>
|
|
/// <param name="dataStr"></param>
|
|
/// <param name="hSize">altezza</param>
|
|
/// <param name="wSize"></param>
|
|
/// <param name="formato">formati 1D di ZXing</param>
|
|
/// <param name="forceUpper">indica se forzare i caratteri a maiuscolo</param>
|
|
/// <returns></returns>
|
|
public static Stream getImage2D(string dataStr, int hSize, int wSize, BarcodeFormat formato, bool forceUpper)
|
|
{
|
|
// se vuoto steamware...
|
|
if (dataStr == null || dataStr == "")
|
|
{
|
|
dataStr = "www.egalware.com";
|
|
}
|
|
if (forceUpper)
|
|
{
|
|
dataStr = dataStr.ToUpper();
|
|
}
|
|
// url decode...
|
|
dataStr = HttpUtility.UrlDecode(dataStr);
|
|
// creo!
|
|
QrCodeEncodingOptions options = new QrCodeEncodingOptions
|
|
{
|
|
DisableECI = true,
|
|
CharacterSet = "UTF-8",
|
|
Width = wSize,
|
|
Height = hSize,
|
|
Margin = 0
|
|
};
|
|
var writer = new ZXing.Windows.Compatibility.BarcodeWriter()
|
|
{
|
|
Format = formato,
|
|
Options = options
|
|
};
|
|
// svrivo svg...
|
|
var svgImage = writer.Write(dataStr);
|
|
// scrivo bitmap
|
|
Bitmap pixelData = writer.Write(dataStr);
|
|
|
|
// Return Image
|
|
MemoryStream ms = new MemoryStream();
|
|
pixelData.Save(ms, ImageFormat.Png);
|
|
ms.Position = 0;
|
|
return ms;
|
|
//return new FileStreamResult(ms, "image/png");
|
|
}
|
|
}
|
|
}
|