本文整理汇总了C#中ZXing.BarcodeWriter.Encode方法的典型用法代码示例。如果您正苦于以下问题:C# BarcodeWriter.Encode方法的具体用法?C# BarcodeWriter.Encode怎么用?C# BarcodeWriter.Encode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ZXing.BarcodeWriter
的用法示例。
在下文中一共展示了BarcodeWriter.Encode方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateBarcode
/// <summary>
/// Generates the barcode.
/// </summary>
/// <param name="barcodeType">Type of the barcode.</param>
/// <param name="value">The value.</param>
/// <returns></returns>
public BarcodeObject GenerateBarcode(string barcodeType, string value)
{
// Determine the barcode type
BarcodeFormat codeType = CalculateType(barcodeType);
try
{
BarcodeWriter writer = new BarcodeWriter();
writer.Format = codeType;
writer.Options = new EncodingOptions {Height = 100, Width = 300};
BitMatrix bitMatrix = writer.Encode(value);
Bitmap bitmap = writer.Write(value);
// Convert to the image bytes
return ConvertToBase64String(bitmap);
}
catch (Exception exception)
{
throw new Exception("GenerateBarcode is failing", exception);
}
}
示例2: BarEncode
private Bitmap BarEncode( string text, BarcodeFormat barFormat = BarcodeFormat.CODE_128, bool isbn = false )
{
var width = 256;
var height = 128;
var margin = 0;
if ( string.IsNullOrEmpty( text ) ) return ( new Bitmap( width, height ) );
string barText = text;
int maxText = 16;
switch ( barFormat )
{
case BarcodeFormat.CODE_128:
width = 256;
height = 72;
margin = 7;
maxText = 232;
break;
case BarcodeFormat.EAN_13:
//height = (int) ( width * 25.93 / 37.29 );
height = (int) ( width * 26.26 / 37.29 );
if ( isbn )
{
string isbn13 = calcISBN_13(barText);
string isbn10 = calcISBN_10(barText);
if ( string.IsNullOrEmpty( isbn13 ) ) return ( new Bitmap( width, height ) );
//if ( string.IsNullOrEmpty( isbn10 ) ) return ( new Bitmap( width, height ) );
barText = isbn13;
}
break;
default:
return ( new Bitmap( width, height ) );
}
var bw = new BarcodeWriter();
bw.Options.Width = width;
bw.Options.Height = height;
//bw.Options.PureBarcode = false;
bw.Options.Hints.Add( EncodeHintType.MARGIN, margin );
bw.Options.Hints.Add( EncodeHintType.DISABLE_ECI, true );
bw.Options.Hints.Add( EncodeHintType.CHARACTER_SET, "UTF-8" );
bw.Renderer = new BitmapRenderer();
bw.Format = barFormat;
if ( barText.Length > maxText )
{
barText = barText.Substring( 0, maxText );
}
try
{
BitMatrix bm = bw.Encode( barText );
int[] rectangle = bm.getEnclosingRectangle();
var bmW = rectangle[2];
var bmH = rectangle[3];
//bw.Options.Width = ( bmW <= 256 ) ? ( bmW + 32 ) : (int)( bmW * ( 1 + 32 / 256 ) );
bw.Options.Width = (int)( bmW * 1.25);
Bitmap barcodeBitmap = bw.Write(barText);
return ( barcodeBitmap );
}
catch ( WriterException )
{
MessageBox.Show( this, I18N._( "Text data can not be encoded to barcode!" ), I18N._( "Error" ), MessageBoxButtons.OK, MessageBoxIcon.Error );
return ( new Bitmap( width, height ) );
}
}
示例3: GenByZXingNet
///
/// 生成二维码
///
/// 二维码信息
/// 图片
private Bitmap GenByZXingNet(string msg)
{
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
writer.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");//编码问题
writer.Options.Hints.Add(
EncodeHintType.ERROR_CORRECTION,
ZXing.QrCode.Internal.ErrorCorrectionLevel.H
);
const int codeSizeInPixels = 100; //设置图片长宽
writer.Options.Height = writer.Options.Width = codeSizeInPixels;
writer.Options.Margin = 0;//设置边框
ZXing.Common.BitMatrix bm = writer.Encode(msg);
Bitmap img = writer.Write(bm);
return img;
}
示例4: GetQrCode
private Bitmap GetQrCode( string text, Image overlay )
{
var width = 512;
var height = 512;
var margin = 0;
int MAX_TEXT = 750;
if (String.IsNullOrEmpty(text))
{
Bitmap blankBitmap = new Bitmap(width, height);
return (blankBitmap);
}
var qrText = text;
if (qrText.Length > MAX_TEXT)
{
qrText = qrText.Substring(0, MAX_TEXT);
}
var bw = new ZXing.BarcodeWriter();
var encOptions = new ZXing.Common.EncodingOptions
{
Width = width,
Height = height,
Margin = margin,
PureBarcode = true
};
encOptions.Hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);
encOptions.Hints.Add(EncodeHintType.DISABLE_ECI, true);
encOptions.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
encOptions.Hints.Add(EncodeHintType.PDF417_COMPACT, true);
encOptions.Hints.Add(EncodeHintType.PDF417_COMPACTION, ZXing.PDF417.Internal.Compaction.AUTO);
bw.Renderer = new BitmapRenderer();
bw.Options = encOptions;
bw.Format = BarcodeFormat.QR_CODE;
try
{
BitMatrix bm = bw.Encode( text );
int[] rectangle = bm.getEnclosingRectangle();
var bmW = rectangle[2];
var bmH = rectangle[3];
//bw.Options.Width = (int) ( bmW * 1.1 );
//bw.Options.Height = (int) ( bmH * 1.1 );
Bitmap barcodeBitmap = bw.Write(qrText);
int deltaHeigth = barcodeBitmap.Height - overlay.Height;
int deltaWidth = barcodeBitmap.Width - overlay.Width;
using (Graphics g = Graphics.FromImage(barcodeBitmap))
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.DrawImage(overlay, new Point(deltaWidth / 2, deltaHeigth / 2));
}
return (barcodeBitmap);
}
catch (WriterException)
{
return (new Bitmap(width, height));
}
}