I want to print QR image (BITMAP) and text using ESCPOS. I am using VISUAL STUDIO 2005. This is the only version that is available at work. The LOGIC below is what I have modified so far.
pirnt receipt result → enter image description here
I WANT RECIPT → RECEIPT
QR IAMAGE HERE →QRIMAGE
This QrImage is bitmap (128x128)
I have successfully implemented printing a bitmap image using GS v 0 in ESC/POS.
However, I am unable to print a bitmap image and text on the same line using GS v 0.
I have found that many people have asked the same question as me.
An answer was provided in the given link, but I don't fully understand it. → link
I created it to prioritize printing bitmap images instead of text.
When I ran this, it only printed empty text.
↓↓↓ THIS IS MY SOURCE ↓↓↓↓ I have revised my writing based on the feedback from those who helped me.
HBITMAP hBmp;
LONG x, y, x1, h;
CString temp = "";
CString csFileName = "C:\QRCODE.bmp";
hBmp = (HBITMAP)::LoadImage(NULL, fileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_MONOCHROME);
if (hBmp == NULL) {
return FALSE
}
CBitmap* m_bmpImage = CBitmap::FromHandle(hBmp);
BITMAP m_bmpInfo;
m_bmpImage->GetBitmap(&m_bmpInfo);
if (m_bmpInfo.bmBitsPixel != 1) {
return ERR_PRT_WRITE;
}
int width = m_bmpInfo.bmWidth;
int height = m_bmpInfo.bmHeight;
int bytesPerLine = (width + 7) / 8;
int totalSize = m_bmpInfo.bmWidthBytes * height;
BYTE* m_buf = (BYTE*)malloc(totalSize);
if (m_buf == NULL) {
return FALSE;
}
memset(m_buf, 0x00, totalSize);
BYTE bmiBuffer[sizeof(BITMAPINFOHEADER) + 2 * sizeof(RGBQUAD)];
BITMAPINFO* pBMI = (BITMAPINFO*)bmiBuffer;
ZeroMemory(bmiBuffer, sizeof(bmiBuffer));
pBMI->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pBMI->bmiHeader.biWidth = m_bmpInfo.bmWidth;
pBMI->bmiHeader.biHeight = m_bmpInfo.bmHeight;
pBMI->bmiHeader.biPlanes = 1;
pBMI->bmiHeader.biBitCount = 1;
pBMI->bmiHeader.biCompression = BI_RGB;
pBMI->bmiColors[0].rgbBlue = 0;
pBMI->bmiColors[0].rgbGreen = 0;
pBMI->bmiColors[0].rgbRed = 0;
pBMI->bmiColors[0].rgbReserved = 0;
pBMI->bmiColors[1].rgbBlue = 255;
pBMI->bmiColors[1].rgbGreen = 255;
pBMI->bmiColors[1].rgbRed = 255;
pBMI->bmiColors[1].rgbReserved = 0;
HDC hdc = ::GetDC(NULL);
if (!GetDIBits(hdc, hBmp, 0, m_bmpInfo.bmHeight, m_buf, pBMI, DIB_RGB_COLORS)) {
::ReleaseDC(NULL, hdc);
free(m_buf);
return ERR_PRT_WRITE;
}
::ReleaseDC(NULL, hdc);
for (int i = 0; i < 10; i++) {
printf("m_buf[%d]: 0x%02X\n", i, m_buf[i]);
}
BYTE sendData[1024];
int pos = 0;
for (int row = 0; row < height; row += 8) {
pos = 0;
sendData[pos++] = 0x1B; // ESC
sendData[pos++] = '*'; // '*'
sendData[pos++] = 0; // mode:0
sendData[pos++] = (BYTE)(width & 0xFF); // nL
sendData[pos++] = (BYTE)((width >> 8) & 0xFF); // nH
for (int col = 0; col < width; col++) {
BYTE c = 0;
for (int i = 0; i < 8; i++) {
int currentRow = row + i;
if (currentRow < height) {
int byteIndex = currentRow * m_bmpInfo.bmWidthBytes + (col / 8);
int bitIndex = col % 8;
BYTE pixelByte = m_buf[byteIndex];
if (pixelByte & (0x80 >> bitIndex)) {
c |= (1 << i);
}
}
}
sendData[pos++] = c;
}
write((LPSTR)sendData, pos);
}
free(m_buf);
return TRUE;
so, print... here.... enter image description here i don't understand my receipt... The questions and answers here are quite immature. I’m sorry.
I've had a look at your code and I have done this task before but don't have access to that code right now.
In essence I see two problems with your code - the lesser one is that you don't really know the byte format of the bitmap (what if it is a color bitmap) so you need a way to pull out the value of a specific pixel - this SO question should help you with that
More importantly the epson printer protocol requires you to write 8 rows at a time. You need to construct a byte array made up of width bytes each of which represent a 8 pixel vertical line. So your row loop really needs to be something more like this (not actual C++ but shows the ideas):
for (int row = 0; row < height; row+=8) {
for (int col=0;col<width; col++) {
//make a array to store the row information here
BYTE c=0;
for (int i=0;i<8;i++)
if (IsBitmapPixelBlack(col,row+i)) c|=(1<<i);
//Append c to the array
}
//Print out this row (i.e. these 8 pixels vertically for the the entire width)
}
I suggest you try getting just the QR code first and then fiddle the plan to include the text which goes to the right of the QR code (which you need to print after the first row and before the second).
Hope this gives you some direction - this stuff is very old school and the escpos protocols you are using have existed since late 1980's.