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

View File

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

View File

@ -1,5 +1,6 @@
package cn.hutool.extra.qrcode; package cn.hutool.extra.qrcode;
import java.awt.Color;
import java.awt.Image; import java.awt.Image;
import java.io.File; import java.io.File;
import java.nio.charset.Charset; import java.nio.charset.Charset;
@ -29,8 +30,8 @@ public class QrConfig {
protected int height; protected int height;
/** 前景色(二维码颜色) */ /** 前景色(二维码颜色) */
protected int foreColor = BLACK; protected int foreColor = BLACK;
/** 背景色 */ /** 背景色默认白色null表示透明 */
protected int backColor = WHITE; protected Integer backColor = WHITE;
/** 边距1~4 */ /** 边距1~4 */
protected Integer margin = 2; protected Integer margin = 2;
/** 纠错级别 */ /** 纠错级别 */
@ -123,12 +124,28 @@ public class QrConfig {
* *
* @param foreColor 前景色 * @param foreColor 前景色
* @return this * @return this
* @deprecated 请使用 {@link #setForeColor(Color)}
*/ */
@Deprecated
public QrConfig setForeColor(int foreColor) { public QrConfig setForeColor(int foreColor) {
this.foreColor = foreColor; this.foreColor = foreColor;
return this; 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 背景色 * @param backColor 背景色
* @return this * @return this
* @deprecated 请使用 {@link #setBackColor(Color)}
*/ */
@Deprecated
public QrConfig setBackColor(int backColor) { public QrConfig setBackColor(int backColor) {
this.backColor = backColor; this.backColor = backColor;
return this; 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 @Ignore
public void generateCustomTest() { public void generateCustomTest() {
QrConfig config = new QrConfig(); QrConfig config = new QrConfig();
config.setMargin(3); config.setMargin(0);
config.setForeColor(Color.CYAN.getRGB()); config.setForeColor(Color.CYAN);
config.setBackColor(Color.GRAY.getRGB()); // 背景色透明
config.setBackColor(null);
config.setErrorCorrection(ErrorCorrectionLevel.H); 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 @Test