Android:使用Zxing生成的二维码有边距(不适合该区域)

Android:使用Zxing生成的二维码有边距(不适合该区域),第1张

概述我在我的应用程序ZXing库中使用它来生成QR码.我想生成适合屏幕宽度的QR码(可能是一些小填充). 如果我将屏幕宽度设置为QR码的宽度尺寸,我会得到更小的QR码.看截图(它是320×240分辨率).我想要QR码适合黑色区域.为什么QR码红色这么小? 如何将其拉伸到黑色区域? 我的代码: Display display = getWindowManager().getDefaultDisplay( 我在我的应用程序ZXing库中使用它来生成QR码.我想生成适合屏幕宽度的QR码(可能是一些小填充).

如果我将屏幕宽度设置为QR码的宽度尺寸,我会得到更小的QR码.看截图(它是320×240分辨率).我想要QR码适合黑色区域.为什么QR码红色这么小?

如何将其拉伸到黑色区域?

我的代码:

display display = getwindowManager().getDefaultdisplay();Point size = new Point();display.getSize(size);int wIDth = size.x; Bitmap bm = encodeAsBitmap(mGeneratedURL,barcodeFormat.QR_CODE,wIDth,wIDth);qrcodeImage.setimageBitmap(bm);

生成QR码:

private Bitmap encodeAsBitmap(String contents,barcodeFormat format,int img_wIDth,int img_height) throws WriterException {    String contentsToEncode = contents;    if (contentsToEncode == null) {        return null;    }    Map<EncodeHintType,Object> hints = null;    String enCoding = guessAppropriateEnCoding(contentsToEncode);    if (enCoding != null) {        hints = new EnumMap<EncodeHintType,Object>(EncodeHintType.class);        //hints.put(EncodeHintType.CHaraCTER_SET,enCoding);        hints.put(EncodeHintType.margin,0); /* default = 4 */    }    MultiFormatWriter writer = new MultiFormatWriter();    BitMatrix result;    try {        result = writer.encode(contentsToEncode,format,img_wIDth,img_height,hints);    } catch (IllegalArgumentException iae) {        // Unsupported format        return null;    }    int wIDth = result.getWIDth();    int height = result.getHeight();    int[] pixels = new int[wIDth * height];    for (int y = 0; y < height; y++) {        int offset = y * wIDth;        for (int x = 0; x < wIDth; x++) {            pixels[offset + x] = result.get(x,y) ? RED : color.BLACK;        }    }    Bitmap bitmap = Bitmap.createBitmap(wIDth,height,Bitmap.Config.ARGB_8888);    bitmap.setPixels(pixels,height);    return bitmap;}
解决方法 我发现了一个问题!

这一行:

String enCoding = guessAppropriateEnCoding(contentsToEncode);

返回null

所以它没有设定

EncodeHintType.margin.

删除该文件,它应该没问题.

//if (enCoding != null) {    hints = new EnumMap<EncodeHintType,Object>(EncodeHintType.class);    //hints.put(EncodeHintType.CHaraCTER_SET,enCoding);    hints.put(EncodeHintType.margin,0); /* default = 4 *///}
总结

以上是内存溢出为你收集整理的Android:使用Zxing生成的二维码有边距(不适合该区域)全部内容,希望文章能够帮你解决Android:使用Zxing生成的二维码有边距(不适合该区域)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1138628.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存