qrcode support argb

This commit is contained in:
Looly 2020-01-11 10:02:10 +08:00
parent a99c0e65d4
commit 067c0578d6
4 changed files with 64 additions and 24 deletions

View File

@ -15,6 +15,7 @@
* 【core 】 提供一个自带默认值的Mappr#87@Gitee
* 【core 】 修改Dict在非大小写敏感状态下get也不区分大小写issue#722@Github
* 【core 】 StrUtil增加contains方法issue#716@Github
* 【core 】 QrCodeUtil增加背景透明支持pr#89@Gitee
### Bug修复
* 【core 】 修复NumberUtil.mul中null的结果错误问题issue#I17Y4J@Gitee

View File

@ -1,14 +1,8 @@
package cn.hutool.extra.qrcode;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import cn.hutool.core.img.Img;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.util.CharsetUtil;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
@ -22,9 +16,14 @@ import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import cn.hutool.core.img.Img;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.util.CharsetUtil;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
/**
* 基于Zxing的二维码工具类
@ -310,9 +309,9 @@ public class QrCodeUtil {
final HashMap<DecodeHintType, Object> hints = new HashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, CharsetUtil.UTF_8);
// 优化精度
hints.put(DecodeHintType.TRY_HARDER, Boolean.valueOf(isTryHarder));
hints.put(DecodeHintType.TRY_HARDER, isTryHarder);
// 复杂模式开启PURE_BARCODE模式
hints.put(DecodeHintType.PURE_BARCODE, Boolean.valueOf(isPureBarcode));
hints.put(DecodeHintType.PURE_BARCODE, isPureBarcode);
Result result;
try {
result = formatReader.decode(binaryBitmap, hints);
@ -334,17 +333,21 @@ public class QrCodeUtil {
*
* @param matrix BitMatrix
* @param foreColor 前景色
* @param backColor 背景色
* @param backColor 背景色(null表示透明背景)
* @return BufferedImage
* @since 4.1.2
*/
public static BufferedImage toImage(BitMatrix matrix, int foreColor, int backColor) {
public static BufferedImage toImage(BitMatrix matrix, int foreColor, Integer backColor) {
final int width = matrix.getWidth();
final int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
BufferedImage image = new BufferedImage(width, height, null == backColor ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? foreColor : backColor);
if(matrix.get(x, y)) {
image.setRGB(x, y, foreColor);
} else if(null != backColor){
image.setRGB(x, y, backColor);
}
}
}
return image;

View File

@ -1,5 +1,6 @@
package cn.hutool.extra.qrcode;
import java.awt.Color;
import java.awt.Image;
import java.io.File;
import java.nio.charset.Charset;
@ -29,8 +30,8 @@ public class QrConfig {
protected int height;
/** 前景色(二维码颜色) */
protected int foreColor = BLACK;
/** 背景色 */
protected int backColor = WHITE;
/** 背景色默认白色null表示透明 */
protected Integer backColor = WHITE;
/** 边距1~4 */
protected Integer margin = 2;
/** 纠错级别 */
@ -123,12 +124,28 @@ public class QrConfig {
*
* @param foreColor 前景色
* @return this
* @deprecated 请使用 {@link #setForeColor(Color)}
*/
@Deprecated
public QrConfig setForeColor(int foreColor) {
this.foreColor = foreColor;
return this;
}
/**
* 设置前景色例如Color.BLUE.getRGB()
*
* @param foreColor 前景色
* @return this
* @since 5.1.1
*/
public QrConfig setForeColor(Color foreColor) {
if(null != foreColor){
this.foreColor = foreColor.getRGB();
}
return this;
}
/**
* 获取背景色
*
@ -143,12 +160,30 @@ public class QrConfig {
*
* @param backColor 背景色
* @return this
* @deprecated 请使用 {@link #setBackColor(Color)}
*/
@Deprecated
public QrConfig setBackColor(int backColor) {
this.backColor = backColor;
return this;
}
/**
* 设置背景色例如Color.BLUE
*
* @param backColor 背景色,null表示透明背景
* @return this
* @since 5.1.1
*/
public QrConfig setBackColor(Color backColor) {
if(null == backColor){
this.backColor = null;
} else {
this.backColor = backColor.getRGB();
}
return this;
}
/**
* 获取边距
*

View File

@ -28,11 +28,12 @@ public class QrCodeUtilTest {
@Ignore
public void generateCustomTest() {
QrConfig config = new QrConfig();
config.setMargin(3);
config.setForeColor(Color.CYAN.getRGB());
config.setBackColor(Color.GRAY.getRGB());
config.setMargin(0);
config.setForeColor(Color.CYAN);
// 背景色透明
config.setBackColor(null);
config.setErrorCorrection(ErrorCorrectionLevel.H);
QrCodeUtil.generate("https://hutool.cn/", config, FileUtil.file("e:/qrcodeCustom.jpg"));
QrCodeUtil.generate("https://hutool.cn/", config, FileUtil.file("d:/qrcodeCustom.png"));
}
@Test