关于Zxing生成DM二维码变形问题总结

使用Zxing生成DM码不是正方形的问题

在hints中增加
hints.put(EncodeHintType.DATA_MATRIX_SHAPE, SymbolShapeHint.FORCE_SQUARE);//设置样式:不设置,正方形,矩形

SymbolShapeHint.FORCE_NONE 默认
SymbolShapeHint.FORCE_SQUARE 正方形
SymbolShapeHint.FORCE_RECTANGLE 矩形

 /**
     * @param content              二维码内容
     * @param width                二维码宽度
     * @param height               二维码高度
     * @param charset              字符编码
     * @param barcodeFormat        二维码类型QR、DM、条形码
     * @param errorCorrectionLevel 容错级别
     * @param margin               边距
     * @param color                二维码颜色 16进制 例如:#ff000000
     * @param colorBg              背景色   16进制 例如:#ffffffff
     * @return
     */
    public static Bitmap createImage(String content, int width, int height, String charset,
                                     BarcodeFormat barcodeFormat,
                                     ErrorCorrectionLevel errorCorrectionLevel,
                                     int margin, int color, int colorBg) {
        try {
            //判断URL合法性
            if (content == null || "".equals(content) || content.length() < 1) {
                return null;
            }
            Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
            hints.put(EncodeHintType.CHARACTER_SET, charset);//编码格式
            hints.put(EncodeHintType.ERROR_CORRECTION, errorCorrectionLevel);//容错类型
            hints.put(EncodeHintType.MARGIN, margin);//边距
            hints.put(EncodeHintType.DATA_MATRIX_SHAPE, SymbolShapeHint.FORCE_SQUARE);//设置样式:不设置,正方形,矩形
            //图像数据转换,使用了矩阵转换
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, barcodeFormat, width, height, hints);
            int[] pixels = new int[width * height];
            //下面这里按照二维码的算法,逐个生成二维码的图片,
            //两个for循环是图片横列扫描的结果
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * width + x] = color;
                    } else {
                        pixels[y * height + x] = colorBg;
                    }
                }
            }
            //生成二维码图片的格式,使用ARGB_8888
            Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
            return bitmap;
        } catch (WriterException e) {
            e.printStackTrace();
            return null;
        }
    }

调用方法

createImage(content, width, height, "utf-8",
                BarcodeFormat.DATA_MATRIX, errorCorrectionLevel, margin, 0XFF000000, 0XFFFFFFFF);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安静的码字猴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值