本系列教程会解答您在使用条形码生成控件TBarCode SDK产品时遇到的绝大部分疑惑。
TBarCode SDK是一款可以在任意应用程序和打印机下生成和打印所有条码的条码软件组件。TBarCode SDK对于MicrosoftOffice 用户以及软件开发者提供条码打印。使用此款条码软件组件您可以以完美效果生成和打印所有用于工业和商业条码符 。
【TBarCode SDK最新版下载】
一. 什么是创建和绘制条形码的最佳方式/strong>
通常,我们建议API调用的此过程:
- 设置所有条形码属性; 类型,文字,尺寸模式,模块宽度,方向,DPI等。
- 调用BCCreate() – 之后不要更改任何属性。
- 对于线性(非多行)条形码,选择所需的高度(取决于您自己)。
- 准备边界矩形:使用默认宽度和所需高度(步骤3)。如果需要,旋转矩形(交换宽度和高度为90/270°)。
- 检索条形码大小:BCGetBarcodeSize() – 使用准备好的(旋转的)边界矩形(步骤4)。
- 现在使用步骤5中返回的大小值设置绘图矩形的宽度和高度。此处还要考虑方向。
- 用这个矩形绘制条形码。
二. 我应该使用特定的模块宽度吗/strong>
我们建议尽可能使用自定义模块宽度 – 调整窄条宽度可以更好地控制输出。它还允许您遵守特定的标签要求和打印机的输出分辨率。可能有一个例外:对于邮政条码,条形码大小必须在精确范围内,边界矩形可能就足够了。
模块宽度的示例值(符合300 dpi):
- 线性类型:0.254 mm,0.339 mm
- PDF417:0.254mm(行高设置为0.762 mm)
- 2D矩阵码:0.508 mm
对于位图输出,模块宽度可以适应于位图分辨率(例如,96dpi)。在这里,您可以使用GetOptimalBitmapSize API调用。
三. 将图像保存到缓冲区/strong>
下面是如何使用BCSaveImageToBuffer的代码示例:
#include "windowsx.h"// after BCCreate(..) // generate image in bufferLPBYTE lpBuffer;eCode = BCSaveImageToBuffer (pBarCode, &lpBuffer, eIMBmp, nWidth, nHeight, 96.0, 96.0);if (eCode == ErrOk){ // lock buffer before reading if (GlobalLockPtr(lpBuffer)) { // retrieve size of buffer int nSize = GlobalSize((HGLOBAL)GlobalHandle(lpBuffer)); // process buffer for (int i=0; i< nSize; i++) { BYTE byte = (static_cast<LPBYTE> (lpBuffer))[i]; // ... } // unlock after reading (required) GlobalUnlockPtr(lpBuffer); // free memory (required) GlobalFreePtr(lpBuffer);}
四. 如何在VB中使用自定义绘图(回调)/strong>
TBarCode V6
'callback functionPublic Function MyDrawRow ( ByRef pMyData As Long, ByVal pBarCode As UIntPtr, ByVal hDC As IntPtr, ByVal hTargetDC As IntPtr, ByRef pRect As Rectangle) As UInt32 Dim strTempo As String strTempo = TBC6.BCGetMetaData(pBarCode) Return Convert.ToUInt32(0)End Function'setting the address of the callback functionPublic cb As TBC6.Callback cb = AddressOf MyDrawRow eCode = TBC6.BCSetFuncDrawRow(hBarcode, cb, pData)'required declarationsDeclare Ansi Function BCDrawCB Lib "TBarCode6.ocx" ( _ ByRef hBarcode As UIntPtr, _ ByVal hDC As IntPtr, _ ByRef pRect As Rectangle, _ ByVal func As Callback, _ ByVal func As Callback, _ ByVal pData As IntPtr ) As Int32Declare Ansi Function BCGetMetaData Lib "TBarCode6.ocx" ( _ ByVal hBarcode As UIntPtr ) As String
五. 绘制到Bitmap-DC并缩放到Screen-DC/strong>
DrawBarcode (HDC hDC, long xpos, long ypos){ RECT hRect; long lHRHeight = 0; ypos += GetFullHeight(); hRect.left = xpos; hRect.top = ypos; hRect.right = hRect.left + width; hRect.bottom = hRect.top + height; HDC memDC; HBITMAP memBMP, oldBMP; int width, height; RECT draw; width = abs(hRect.right - hRect.left); height = abs (hRect.bottom - hRect.top); draw.left = 0; draw.bottom = height; draw.top = 0; draw.right = width; memDC = CreateCompatibleDC(hDC); memBMP = CreateCompatibleBitmap(hDC, width, height); oldBMP = (HBITMAP) SelectObject(memDC, memBMP); if (S_OK = BCDraw(pBarcode, memDC, &draw)) { // scale / copy to target DC BitBlt(hDC, hRect.left, hRect.top, width, height, memDC, 0, 0, SRCCOPY); SelectObject(memDC, oldBMP); } DeleteObject(memBMP); DeleteDC(memDC);}
六. 如何使用抗锯齿字体渲染生成位图输出/strong>
调整适当的DC
使用以下屏幕DC创建具有消除锯齿选项的字体渲染(根据屏幕调整):
HDC dc = CreateDC(TEXT(“DISPLAY”),NULL,NULL,NULL);eCode = BCSaveImageEx(m_pbarCode,dc,file,imageType,100,widthPx,heightPx,dpi,dpi); if(dc) DeleteDC(dc);
调整正确的字体(LOGFONT)
使用Windows标准字体对话框或以下函数从Windows GDI检索预初始化的LOGFONT结构:
HFONT hSystemVariableFont = (HFONT ) GetStockObject( ANSI_VAR_FONT );LOGFONT lfSystemVariableFont;GetObject ( hSystemVariableFont, sizeof(LOGFONT), &lfSystemVariableFont );// Make sure that the following flags are set in the LOGFONT structure passed to BCSetLogFont():lfOutPrecision (e.g. set to 3)lfClipPrecision (e.g. set to 2)lfQuality (e.g. set to 1)lfWeight (e.g. set to 400)lfFaceName should be set to a True Type font (e.g. “Arial”)// Calculate the font height as follows:lfHeight = = -MulDiv( 10 /*PointSize*/, GetDeviceCaps(hDC, LOGPIXELSY), 72);
未完待续……
标签:条形码条形码生成工业4.0工业物联
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!