使用incam可以很轻松的增加2维码,这里通过另外一种方式玩转二维码的加法,
使用谷歌zxing.dll类库实现,将文字信息转为bitmap点阵后,在Genesis绘制点即可。
一.incam增加二维码效果
二.通过代码增加方PAD实现效果:
三.代码实现:
Genesis增加二维码代码
private void addQR()
{
CodeHelper qr = new CodeHelper();
Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");//设置内容编码
hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);//错码率 L水平 7 % M水平 15 % Q水平 25 % H水平 30 %
hint.Add(EncodeHintType.QR_VERSION, );//大小 (V-1)*4 + 21(V是版本号)
hint.Add(EncodeHintType.MARGIN, );//边距
ZXing.Common.BitMatrix codeArray = qr.enMatrix("at00101ca0", , , BarcodeFormat.QR_CODE, hint); //at00101ca0为增加二维码内容
add add_ = new add();
gPP gp = new gPP();
gp.width = ;//PAD大小
gp.symbols = "s";//PAD形状为方PAD
for (int x = ; x < codeArray.Width; x++)
{
for (int y = ; y < codeArray.Height; y++)
{
if (codeArray[x, y])
{
gp.p.x = x * 0.5;
gp.p.y = y * 0.5;
add_.pad(gp); //增加方PAD PAD
}
}
}
}
zxing.dll重新封装后的二维码类
public class CodeHelper
{ public string CHARSET = "UTF-8"; public static int SaveTo(Bitmap bitmap, string filepath, string filename)
{
try
{
bitmap.Save(filepath + filename); return ;
}
catch (System.Exception ex)
{
return -;
}
}
/// <summary>
///字符转二维码
/// </summary>
/// <param name="content"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="barcodeFormat"></param>
/// <param name="hints"></param>
/// <returns></returns>
public Bitmap encode(string content, int width, int height, BarcodeFormat barcodeFormat, IDictionary<EncodeHintType, object> hints)
{
BitMatrix byteMatrix = new MultiFormatWriter().encode(content, barcodeFormat, width, height, hints);
Bitmap bitmap = toBitmap(byteMatrix);
return bitmap;
} /// <summary>
///字符转二维码
/// </summary>
/// <param name="content"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="barcodeFormat"></param>
/// <param name="hints"></param>
/// <returns></returns>
public string enString(string content, int width, int height, BarcodeFormat barcodeFormat, IDictionary<EncodeHintType, object> hints)
{
BitMatrix byteMatrix = new MultiFormatWriter().encode(content, barcodeFormat, width, height, hints);
return toString(byteMatrix);
}
/// <summary>
///字符转二维码
/// </summary>
/// <param name="content"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="barcodeFormat"></param>
/// <param name="hints"></param>
/// <returns></returns>
public BitMatrix enMatrix(string content, int width, int height, BarcodeFormat barcodeFormat, IDictionary<EncodeHintType, object> hints)
{
BitMatrix byteMatrix = new MultiFormatWriter().encode(content, barcodeFormat, width, height, hints);
return byteMatrix;
}
/// <summary>
/// 字符转二维码
/// </summary>
/// <param name="content"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="barcodeFormat"></param>
/// <returns></returns>
public Bitmap encode(string content, int width, int height, BarcodeFormat barcodeFormat)
{
BitMatrix byteMatrix = new MultiFormatWriter().encode(content, barcodeFormat, width, height); Bitmap bitmap = toBitmap(byteMatrix);
return bitmap;
}
/// <summary>
/// BitMatrix转Bitmap
/// </summary>
/// <param name="matrix"></param>
/// <returns></returns>
private static String toString(BitMatrix matrix)
{
StringBuilder bitString = new StringBuilder(); for (int x = ; x < matrix.Width; x++)
{
bitString.Append("\r\n");
for (int y = ; y < matrix.Height; y++)
{
//bitmap.SetPixel(x, y, matrix[x, y] ? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF"));
bitString.Append(matrix[x, y] ? "■" : " ");
}
}
return bitString.ToString ();
}
/// <summary>
/// BitMatrix转Bitmap
/// </summary>
/// <param name="matrix"></param>
/// <returns></returns>
private static Bitmap toBitmap(BitMatrix matrix)
{
int width = matrix.Width;
int height = matrix.Height;
Bitmap bitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); for (int x = ; x < width; x++)
{
for (int y = ; y < height; y++)
{
bitmap.SetPixel(x, y, matrix[x, y] ? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF"));
}
}
return bitmap;
} /// <summary>
/// 字符转二维码并保存
/// </summary>
/// <param name="content"></param>
/// <param name="path"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="barcodeFormat"></param>
/// <returns></returns>
public int encodeAndSave(string content, string path, int width, int height, BarcodeFormat barcodeFormat)
{
return SaveTo(encode(content, width, height, barcodeFormat), path, "");
}
/// <summary>
/// bmp图片路径转换??
/// </summary>
/// <param name="bmppath"></param>
/// <param name="hints"></param>
/// <param name="result"></param>
/// <returns></returns>
public static int decode(string bmppath, IDictionary<DecodeHintType, object> hints, out Result result)
{
Bitmap bmap = new Bitmap(bmppath);
BitmapLuminanceSource source = new BitmapLuminanceSource(bmap);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
result = new MultiFormatReader().decode(bitmap, hints);
return ;
}
}