Zxin包生成二维码白边问题解决方法

本文介绍如何使用Java的Zxing库生成二维码,并解决生成过程中出现的白边问题。通过设置`quietZone`参数为4,可以调整二维码周围的空白区域。代码示例展示了创建带有指定内容的二维码并保存到文件的过程。
摘要由CSDN通过智能技术生成
package com;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.Writer;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.google.zxing.qrcode.encoder.ByteMatrix;
import com.google.zxing.qrcode.encoder.Encoder;
import com.google.zxing.qrcode.encoder.QRCode;
import java.util.Map;

/***/
public final class QRCodeWriter implements Writer {
private static final int QUIET_ZONE_SIZE = 4;


public BitMatrix encode(String contents, BarcodeFormat format, int width, int height) throws WriterException {
return encode(contents, format, width, height, null);
}
/**
* 需要白边请调用该方法
* @param contents
* @param format
* @param width 宽度
* @param height 高度
* @param quietZone 白边大小
* @param hints
* @return
* @throws WriterException
*/
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height,int quietZone, Map<EncodeHintType, ?> hints)
throws WriterException {
if (contents.isEmpty()) {
throw new IllegalArgumentException("Found empty contents");
}


if (format != BarcodeFormat.QR_CODE) {
throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
}


if ((width < 0) || (height < 0)) {
throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' + height);
}


ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值