Preguntas frecuentes sobre TBarCode DLL / TBarCode Library
¿Cómo puedo crear un mapa de bits con un ancho de módulo específico?
¿Cómo puedo crear un mapa de bits con un ancho de módulo específico?
En el siguiente código simplemente establece el ancho de módulo en [mm] y la altura total del código de barras en [mm]. Mediante el indicador optimizeToDPI también puede añadir una optimización al ancho de módulo (de hecho, obtiene el mismo resultado que con BCGetOptimalBitmapSize). El código de ejemplo es para códigos de barras lineales, pero también se puede adaptar a códigos 2D.
Llame a esto después de 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);
Debido al indicador de optimización habilitado, con un ancho de módulo dado de 0,26 mm, el ancho de módulo resultante será de 0,254 mm (6 píxeles a 600 ppp).