ricmoo/QRCode

QR to Bitmap

jjjrmy opened this issue · 17 comments

I'm trying to create a BMP of the QR code to display on a 128x64 OLED screen like you mentioned in the readme.
Is there an example for this?

I have tried using your for loop to generate the bits, but that did not work.

some days ago,i had try it.
core code is:
for (uint8_t y = 0; y < qrcode.size; y++) {
for (uint8_t x = 0; x < qrcode.size; x++) {
if (qrcode_getModule(&qrcode, x, y)) {
u8g.setColorIndex(1);
}else{
u8g.setColorIndex(0);
}
u8g.drawPixel(x0+x,y0+y);
}
}
but it work slowly...T_T

what is variable u8g?

where is x0 and y0 defined?
without it the QR code is half as wide as it is tall.

x0,and y0 is the beginning coordinates of QRcode picture...
i use a 12864LCD, and i want to show the QRcode in the center,code is:

#define Lcd_X  128
#define Lcd_Y  64
void EncodeDataInLCDAtCenter(uint8_t version,uint8_t ecc,const char *lpsSource){
    _ToBeUpdatedFlag = 0;
    
    uint8_t BufferSize = qrcode_getBufferSize(version);
    uint8_t qrcodeData[BufferSize];
    qrcode_initText(&qrcode, qrcodeData, version, ecc, lpsSource);
    //qrcode_initText(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8_t ecc, const char *data)

    uint8_t x0 = (Lcd_X-qrcode.size)/2;
    uint8_t y0 = (Lcd_Y-qrcode.size)/2;

    for (uint8_t y = 0; y < qrcode.size; y++) {
        for (uint8_t x = 0; x < qrcode.size; x++) {
            if (qrcode_getModule(&qrcode, x, y)) {
              u8g.setColorIndex(1);
            }else{
              u8g.setColorIndex(0);
            }
            u8g.drawPixel(x0+x,y0+y);
        }
    }
}

It depends on the LCD or OLED display you use, but @yifanshu02 code looks good.

I usually use the Adafruit Library, but it is similar.

I will be posting a more in depth example soon.

This library is what I used in my Firefly Hardware Wallet. Once I post the source code for that, I'll link to it from the README as an example.

One other note on using the 128x64 monochrome OLED; "on" modules in the QR code are black and "off" modules are white, so you will need to take this into account when using the display (since "on" pixels are white and "off" pixels are black).

Has anyone got this to display on a OLED which is 128 x 64. I am trying to do the something without any luck?

I used it with a different lcd and it did work fine for me. Can you show what you already have done?

` HI_GFX_Clear(1);

static uint8_t buffer[QR_VERSION_BUFFER_SIZE(7)] = { 0 };

QRCode qrcode = { 0 };
const int8_t error = qrcode_initText(&qrcode, buffer, 7, 0, "1245");
ASSERT_DEBUG_MODE(0 == error);

const uint8_t y0 = 10;
const uint8_t x0 = 10;

for (uint8_t y = 0; y < qrcode.size; y++)
{
    for (uint8_t x = 0; x < qrcode.size; x++)
    {
        uint8_t pixel = 0;
        if(qrcode_getModule(&qrcode, x, y) )
        {
            /*Put black pixel*/
            pixel = 1;
        }
        else
        {
            /*White pixel*/
            pixel = 0;
        }

        HI_GFX_Draw_Pixel(x0 + x, y0 + y, pixel);

    }

}`

mvimg_20180613_150802

Can you try:

QRCode qrcode;

Rather than initializing it to { 0 }?

That won't do anything. I am just clearing the memory because qrcode is initialised on the stack.
Is a single module equivalent to 1 pixel?

Yes, a single module is 1 pixel.

I don't think that is clearing the stack correctly. You shouldn't need to do either = { 0 }. I always use the QR codes on the stack.

But here is a working use of it:
https://github.com/firefly/wallet/blob/master/source/firefly/firefly_display.cpp#L411

And here is the initialization:
https://github.com/firefly/wallet/blob/master/source/firefly/firefly.ino#L325

The only other thing I can think of is that the HI_GFX_Draw_Pixel isn't working, because other than that it looks fine to me.

The pixel draw works because I used it for many other things.
Also If I remove qrcode_getModule() the code should just draw a black square on a white surface which is does.

It seems that the buffer is encoded incorrectly. I will start digging through the library.

Thanks for the help.

The function grid is used to determine is a given module is a function module, which is not subject to masking.

Can you try running the test cases? You should be able to modify them to use your "12345", which will compare this library against the known-correct CPP library.

some days ago,i had try it.
core code is:
for (uint8_t y = 0; y < qrcode.size; y++) {
for (uint8_t x = 0; x < qrcode.size; x++) {
if (qrcode_getModule(&qrcode, x, y)) {
u8g.setColorIndex(1);
}else{
u8g.setColorIndex(0);
}
u8g.drawPixel(x0+x,y0+y);
}
}
but it work slowly...T_T

Yup, I am experiencing the same. Printing point by point make it slow printing (it takes around 3-4 second to print the QR code of version 8, while version 3 will be printed within a sec).