【发布时间】:2017-08-16 04:49:25
【问题描述】:
我在QLabel 上成功绘制并显示QrCode,但扫描时无法识别。这是我使用的代码 - 我用静态函数制作了一个小类:
void QrCodeDrawer::paintQR(QPainter &painter, const QSize sz, const QString &data, QColor fg)
{
char *str=data.toUtf8().data();
// NOTE: At this point you will use the API to get the encoding and format you want, instead of my hardcoded stuff:
QrCode qr = QrCode::encodeText(str, QrCode::Ecc::HIGH);
const int s=qr.size>0?qr.size:1;
const double w=sz.width();
const double h=sz.height();
const double aspect=w/h;
const double size=((aspect>1.0)?h:w);
const double scale=size/(s+2);
// NOTE: For performance reasons my implementation only draws the foreground parts in supplied color.
// It expects background to be prepared already (in white or whatever is preferred).
painter.setPen(Qt::NoPen);
painter.setBrush(fg);
for(int y=0; y<s; y++) {
for(int x=0; x<s; x++) {
const int color=qr.getModule(x, y); // 0 for white, 1 for black
if(0x0!=color) {
const double rx1=(x+1)*scale, ry1=(y+1)*scale;
QRectF r(rx1, ry1, scale, scale);
painter.drawRects(&r,1);
}
}
}
}
并在这里调用它:
QPixmap map(400,400);
QPainter painter(&map);
QrCodeDrawer::paintQR(painter,QSize(400,400),"Hello World", QColor("white"));
ui.qrCode->setPixmap(map);
我将“Hello World”作为输入字符串,这是我得到的代码:
我从here得到源代码。
【问题讨论】:
-
好像倒过来了
-
是的!在我反转颜色后它被识别
-
@kim366 哦,哦!你是对的!,我也尝试过反转,它起作用了!,我起初想知道为什么边缘的 3 个正方形内部是白色的(这很不寻常),但我更紧张的是它被反转了!
-
是的,它对我有用,可能是应用程序开发人员负责此案。事实上,代码可以是任何颜色,而不仅仅是黑白。
-
QR 码规范确实允许使用倒色的符号。但是,除非您切换选项,否则流行的程序(例如 Android 条码扫描仪)无法识别反转,这很麻烦。因此,如果您想要最好的兼容性,请不要绘制反码。
标签: c++ qt cross-platform render qr-code