常见问题 TBarCode DLL / TBarCode Library
如何创建具有特定模块宽度的位图?
如何创建具有特定模块宽度的位图?
在下面的代码中,您只需设置以毫米为单位的模块宽度和条码总高度。通过 optimizeToDPI 标志,您还可以为模块宽度添加优化(实际上会得到与 BCGetOptimalBitmapSize 相同的结果)。示例代码适用于线性条码,但也可适配二维码。
在 BCCreate (..) 之后调用此代码。
double moduleWidthUserMM = 0.26; double heightUserMM = 22.0; double dpi = 600; int widthUserPx = 1; // dummy value, since the width of the bar code is determined by the module width int heightUserPx = dpi * heightUserMM / 25.4; // for bitmaps we need [Pixel] double width, height; bool optimizeToDPI = TRUE; // adapt module width to next lower full Pixels => avoid quantization errors int fontSizePt = (int)(0.51 + (10 * moduleWidthUserMM / 0.339)); // auto font size - works well with EAN / Code-128 / Code 39 // bounding rectangle of barcode -> desired drawing coordinates RECT rect; rect.left = 0; rect.right = widthUserPx; rect.top = 0; rect.bottom = heightUserPx; eCode = BCSetDPI(pBarCode, dpi, dpi); eCode = BCSetIgnoreDpiFromDC(pBarCode, TRUE); // Ensure that the width of a bar is a multiple of a single pixel (and at minimum one pixel) eCode = BCSetOptResolution(pBarCode, optimizeToDPI); // bar code should adapt to module width eCode = BCSetSizeMode(pBarCode, e_SizeMode::eSizeMode_CustomModuleWidth); // set module width in [0.001mm] // we need a rounding delta of 0.001mm in case of optimize flag eCode = BCSetModWidthN(pBarCode, moduleWidthUserMM * 1000 + (optimizeToDPI?1:0)); eCode = BCSetFontHeight(pBarCode, fontSizePt); // Calculate bar code size // for retrieving the size in [Pixel] we need a screen hDC HDC hDC = GetDC(NULL); eCode = BCGetBarcodeSize(pBarCode, &rect, hDC, eMUPixel, &width, &height); if (hDC) DeleteDC(hDC); // Save bitmap with the given module width and size eCode = BCSaveImage(pBarCode, "C:\\Temp\\Barcode-600dpi.jpg", eIMJpg, (int) (width + 0.5), (int) (height + 0.5), dpi, dpi);
由于启用了优化标志,在给定模块宽度为 0.26 mm 时,最终的模块宽度将为 0.254 mm(600 dpi 下的 6 个像素)。