public class QrConfig extends Object
Modifier and Type | Field and Description |
---|---|
protected Integer |
backColor
背景色,默认白色,null表示透明
|
protected Charset |
charset
编码
|
protected com.google.zxing.qrcode.decoder.ErrorCorrectionLevel |
errorCorrection
纠错级别
|
protected Integer |
foreColor
前景色(二维码颜色)
|
protected com.google.zxing.BarcodeFormat |
format
生成码的格式,默认为二维码
|
protected int |
height
高度
|
protected Image |
img
二维码中的Logo
|
protected double |
imgRound
二维码logo圆角弧度,0~1,为长宽占比
|
protected Integer |
margin
边距0~4
|
protected Integer |
qrVersion
设置二维码中的信息量,可设置0-40的整数
|
protected int |
ratio
二维码中的Logo缩放的比例系数,如5表示长宽最小值的1/5
|
protected com.google.zxing.datamatrix.encoder.SymbolShapeHint |
shapeHint
DATA_MATRIX的符号形状
|
protected int |
width
宽度
|
Constructor and Description |
---|
QrConfig()
构造,默认长宽为300
|
QrConfig(int width,
int height)
构造
|
Modifier and Type | Method and Description |
---|---|
int |
getBackColor()
获取背景色
|
Charset |
getCharset()
获取编码
|
com.google.zxing.qrcode.decoder.ErrorCorrectionLevel |
getErrorCorrection()
获取纠错级别
|
int |
getForeColor()
获取前景色
|
com.google.zxing.BarcodeFormat |
getFormat()
获取码格式
|
int |
getHeight()
获取高度
|
Image |
getImg()
获取二维码中的Logo
|
double |
getImgRound()
获取二维码logo圆角弧度,0~1,为长宽占比
|
Integer |
getMargin()
获取边距
|
Integer |
getQrVersion()
设置二维码中的信息量,可设置0-40的整数,二维码图片也会根据qrVersion而变化,0表示根据传入信息自动变化
|
int |
getRatio()
获取二维码中的Logo缩放的比例系数,如5表示长宽最小值的1/5
|
int |
getWidth()
获取宽度
|
static QrConfig |
of()
创建QrConfig
|
static QrConfig |
of(int width,
int height)
创建QrConfig
|
QrConfig |
setBackColor(Color backColor)
设置背景色,例如:Color.BLUE
|
QrConfig |
setCharset(Charset charset)
设置编码
|
void |
setEnableEci(boolean enableEci)
是否开启ECI编码
如果enableEci=false,则二维码中不包含ECI信息,即: charset 字符编码设置为null , 二维码为英文字符,保持false最佳如果enableEci=true,则二维码中包含ECI信息,即:按照 charset 编码进行设置, 二维码为包含中文,保持true最佳,否则会中文乱码参考1:关于\000026的问题 参考2:ECI(Extended_Channel_Interpretation)模式 参考3:二维码的生成细节和原理 二维码编码有ECI模式和非ECI模式的情况之分,在ECI模式下第一个字节是用作编码标识,而非ECI模式下直接就是数据流。 |
QrConfig |
setErrorCorrection(com.google.zxing.qrcode.decoder.ErrorCorrectionLevel errorCorrection)
设置纠错级别
|
QrConfig |
setForeColor(Color foreColor)
设置前景色,例如:Color.BLUE.getRGB()
|
QrConfig |
setFormat(com.google.zxing.BarcodeFormat format)
设置码格式,默认二维码
|
QrConfig |
setHeight(int height)
设置高度
|
QrConfig |
setImg(byte[] imageBytes)
设置二维码中的Logo文件
|
QrConfig |
setImg(File imgFile)
设置二维码中的Logo文件
|
QrConfig |
setImg(Image img)
设置二维码中的Logo
|
QrConfig |
setImg(String imgPath)
设置二维码中的Logo文件
|
QrConfig |
setImgRound(double imgRound)
设置二维码logo圆角弧度,0~1,为长宽占比
|
QrConfig |
setMargin(Integer margin)
设置边距
|
QrConfig |
setQrVersion(Integer qrVersion)
设置二维码中的信息量,可设置0-40的整数,二维码图片也会根据qrVersion而变化,0表示根据传入信息自动变化
|
QrConfig |
setRatio(int ratio)
设置二维码中的Logo缩放的比例系数,如5表示长宽最小值的1/5
|
QrConfig |
setShapeHint(com.google.zxing.datamatrix.encoder.SymbolShapeHint shapeHint)
设置DATA_MATRIX的符号形状
|
QrConfig |
setWidth(int width)
设置宽度
|
HashMap<com.google.zxing.EncodeHintType,Object> |
toHints()
转换为Zxing的二维码配置
|
protected int width
protected int height
protected Integer foreColor
protected Integer backColor
protected Integer margin
protected Integer qrVersion
protected com.google.zxing.qrcode.decoder.ErrorCorrectionLevel errorCorrection
protected Charset charset
protected Image img
protected double imgRound
protected int ratio
protected com.google.zxing.datamatrix.encoder.SymbolShapeHint shapeHint
protected com.google.zxing.BarcodeFormat format
public QrConfig()
public QrConfig(int width, int height)
width
- 宽height
- 高public static QrConfig of()
public static QrConfig of(int width, int height)
width
- 宽height
- 高public int getWidth()
public QrConfig setWidth(int width)
width
- 宽度public int getHeight()
public QrConfig setHeight(int height)
height
- 高度public int getForeColor()
public QrConfig setForeColor(Color foreColor)
foreColor
- 前景色public int getBackColor()
public QrConfig setBackColor(Color backColor)
backColor
- 背景色,null表示透明背景public Integer getMargin()
public Integer getQrVersion()
public QrConfig setQrVersion(Integer qrVersion)
qrVersion
- 二维码中的信息量public com.google.zxing.qrcode.decoder.ErrorCorrectionLevel getErrorCorrection()
public void setEnableEci(boolean enableEci)
charset
字符编码设置为null
, 二维码为英文字符,保持false最佳charset
编码进行设置, 二维码为包含中文,保持true最佳,否则会中文乱码二维码编码有ECI模式和非ECI模式的情况之分,在ECI模式下第一个字节是用作编码标识,而非ECI模式下直接就是数据流。 ECI模式其实是更好的方案,这样子解码的时候可以根据标识采用不同的编码方式。而非ECI模式只能按照一种统一的方式处理了。 但是由于部分设备不支持ECI模式,所以就出现了无法识别的情况。 使用扫码桩/扫码枪,可能会出现\000026的字符。使用手机扫描、其他二维码解析软件扫描,则不会出现。
ECI编码表可以看出UTF-8就是对应"\000026"(对应数字22)
总结建议:如果二维码内容全是字符,没有中文,就不用使用UTF-8等格式进行编码,只有使用中文等特殊符号才需要编码
enableEci
- 是否开启ECIEncodeHintType.PDF417_AUTO_ECI
public QrConfig setErrorCorrection(com.google.zxing.qrcode.decoder.ErrorCorrectionLevel errorCorrection)
errorCorrection
- 纠错级别public Charset getCharset()
public Image getImg()
public QrConfig setImg(String imgPath)
imgPath
- 二维码中的Logo路径public QrConfig setImg(byte[] imageBytes)
imageBytes
- 二维码中的Logo图片bytes表示形式public QrConfig setImg(File imgFile)
imgFile
- 二维码中的Logopublic double getImgRound()
public QrConfig setImgRound(double imgRound)
imgRound
- 二维码logo圆角弧度,0~1,为长宽占比public int getRatio()
public QrConfig setRatio(int ratio)
ratio
- 二维码中的Logo缩放的比例系数,如5表示长宽最小值的1/5public QrConfig setShapeHint(com.google.zxing.datamatrix.encoder.SymbolShapeHint shapeHint)
shapeHint
- DATA_MATRIX的符号形状public com.google.zxing.BarcodeFormat getFormat()
public QrConfig setFormat(com.google.zxing.BarcodeFormat format)
format
- 码格式Copyright © 2025. All rights reserved.