我用qrcode生成二维码,字节20个,但是打印出来的二维码只有9mm长宽,那么参数该如何设置?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我用qrcode生成二维码,字节20个,但是打印出来的二维码只有9mm长宽,那么参数该如何设置?相关的知识,希望对你有一定的参考价值。

如上,用的java,或者生成二维码用zxing会不会更好些?

生成二维码,你可以用二维码生成软件,二维码生成软件支持多种二维码类型,生成之后,双击二维码,可以在图形属性中,自定义设置各项参数。如图所示:

参考技术A zxing 生成二维码

/*生成二维码*/
public static Bitmap createQRCode(String content, int w, int h, Bitmap logo)

if (TextUtils.isEmpty(content))
return null;

/*偏移量*/
int offsetX = w / 2;
int offsetY = h / 2;

/*生成logo*/
Bitmap logoBitmap = null;

if (logo != null)
Matrix matrix = new Matrix();
float scaleFactor = Math.min(w * 1.0f / 5 / logo.getWidth(), h * 1.0f / 5 / logo.getHeight());
matrix.postScale(scaleFactor, scaleFactor);
logoBitmap = Bitmap.createBitmap(logo, 0, 0, logo.getWidth(), logo.getHeight(), matrix, true);


/*如果log不为null,重新计算偏移量*/
int logoW = 0;
int logoH = 0;
if (logoBitmap != null)
logoW = logoBitmap.getWidth();
logoH = logoBitmap.getHeight();
offsetX = (w - logoW) / 2;
offsetY = (h - logoH) / 2;


/*指定为UTF-8*/
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//容错级别
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//设置空白边距的宽度
hints.put(EncodeHintType.MARGIN, 0);
// 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
BitMatrix matrix = null;
try
matrix = new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, w, h, hints);

// 二维矩阵转为一维像素数组,也就是一直横着排了
int[] pixels = new int[w * h];
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++)
if (x >= offsetX && x < offsetX + logoW && y >= offsetY && y < offsetY + logoH)
int pixel = logoBitmap.getPixel(x - offsetX, y - offsetY);
if (pixel == 0)
if (matrix.get(x, y))
pixel = 0xff000000;
else
pixel = 0xffffffff;


pixels[y * w + x] = pixel;
else
if (matrix.get(x, y))
pixels[y * w + x] = 0xff000000;
else
pixels[y * w + x] = 0xffffffff;





Bitmap bitmap = Bitmap.createBitmap(w, h,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, w, 0, 0, w, h);
return bitmap;

catch (WriterException e)

System.out.print(e);
return null;


参考技术B 二维码的大小需要在生成的时候设置,具体百度一下

为啥我用phprqcode 生成二维码带logo的时候,就无法读取信息

参考技术A 使用 jQuery.qrcode.js实现。
jQuery.qrcode.js 是一个能够在客户端生成矩阵二维码QRCode 的jquery插件,使用它可以很方便的在页面上生成二维条码。此插件是能够独立使用的,体积也比较 小,使用gzip压缩后才不到4kb。因为它是直接在客户端生成的条码, 所以不会有图片下载的过程,能够实现快速生成。它是基于一个多语言的类库封装的,也不依赖于其他额外的服务。
好处:使用jquery-qrcode的好处,不需要在服务器端生成多余的二维码图片,二维码直接通过JavaScript直接在客户端生成,有效减少带宽,以及维护成本。

以上是关于我用qrcode生成二维码,字节20个,但是打印出来的二维码只有9mm长宽,那么参数该如何设置?的主要内容,如果未能解决你的问题,请参考以下文章

arduino通过热敏打印机打印二维码

为啥我用phprqcode 生成二维码带logo的时候,就无法读取信息

php利用Qrcode生成带logo二维码

c#怎么生成DataMatrix二维码,并打印

python_使用qrcode生成二维码

使用jquery.qrcode生成二维码实现微信分享功能