Skip to content

Commit

Permalink
fix #74 #75
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyueyi committed Jul 4, 2020
1 parent 1445c7f commit d2c87f7
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 29 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<dependency>
<groupId>com.github.liuyueyi.quick-media</groupId>
<artifactId>qrcode-plugin</artifactId>
<version>2.4</version>
<version>2.4.2</version>
</dependency>
```

Expand Down Expand Up @@ -240,7 +240,7 @@ markdown转html,转图片的封装类, 详细内容查看: [markdown-plugin]

[查看更多二维码详情](https://liuyueyi.github.io/quick-media/#/插件/二维码/二维码插件使用手册)

<font color="red">最新版本2.4,详情查看 [迭代日志](https://liuyueyi.github.io/quick-media/#/%E8%BF%AD%E4%BB%A3/%E6%9B%B4%E6%96%B0%E6%97%A5%E5%BF%97?id=_27-qrcode-plugin)</font>
<font color="red">最新版本2.4.2,详情查看 [迭代日志](https://liuyueyi.github.io/quick-media/#/%E8%BF%AD%E4%BB%A3/%E6%9B%B4%E6%96%B0%E6%97%A5%E5%BF%97?id=_27-qrcode-plugin)</font>

给出一个实际生成的case如下:

Expand Down
4 changes: 3 additions & 1 deletion docs/插件/二维码/二维码插件使用手册.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<dependency>
<groupId>com.github.hui.media</groupId>
<artifactId>qrcode-plugin</artifactId>
<version>2.3</version>
<version>2.4.2</version>
</dependency>
```

Expand Down Expand Up @@ -351,6 +351,8 @@ boolean ans = QrCodeGenWrapper.of(msg)
- 圆角带logo背景色
- 圆角带logo背景,边框

**说明:2.4.2版本优化了logo的锯齿严重的问题**

![logo](http://ww1.sinaimg.cn/large/8154e929gy1g8w66xzxx6j20n3062adc.jpg)

**v2.4版本新增圆形logo**
Expand Down
14 changes: 9 additions & 5 deletions docs/迭代/更新日志.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
- 第一个相对稳定的正式版本
- 全线插件,升级版本为 `2.0`

10. [2.2](https://github.com/liuyueyi/quick-media/releases/tag/v2.2)
10. [2.2](https://github.com/liuyueyi/quick-media/releases/tag/2.2)
- 升级springboot版本
- qrcode升级2.2
- 开启 `jitpack` maven仓库依赖
Expand All @@ -58,10 +58,14 @@
- 支持文字二维码
- 重写几何二维码渲染逻辑,与图片渲染逻辑保持一致

12. [2.4.1](https://github.com/liuyueyi/quick-media/releases/tag/v2.4.1)
12. [2.4.1](https://github.com/liuyueyi/quick-media/releases/tag/2.4.1)
- qrcode升级2.4.1
- 文字二维码支持随机+顺序两种渲染模式


13. [2.4.2](https://github.com/liuyueyi/quick-media/releases/tag/2.4.2)
- qrcode升级2.4.2
- [#74](https://github.com/liuyueyi/quick-media/issues/74) 优化logo锯齿严重问题
- fix [#75] 指定二维码0/1渲染图片时,且不指定探测图形时,0点渲染逻辑bug

## 2. 版本说明

Expand Down Expand Up @@ -439,11 +443,11 @@
<dependency>
<groupId>com.github.hui.media</groupId>
<artifactId>qrcode-plugin</artifactId>
<version>2.4.1</version>
<version>2.4.2</version>
</dependency>
</textarea>
</td>
<td>2020.06.29</td>
<td>2020.07.04</td>
<td>二维码生成解码</td>
</tr>
<tr>
Expand Down
2 changes: 1 addition & 1 deletion plugins/qrcode-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>qrcode-plugin</artifactId>
<version>2.4.1</version>
<version>2.4.2</version>

<properties>
<zxing.version>3.3.0</zxing.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ public static BufferedImage drawLogo(BufferedImage qrImg, QrCodeOptions.LogoOpti


// 插入LOGO
Graphics2D qrImgGraphic = qrImg.createGraphics();
Graphics2D qrImgGraphic = GraphicUtil.getG2d(qrImg);

if (logoOptions.getOpacity() != null) {
qrImgGraphic.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, logoOptions.getOpacity()));
}
qrImgGraphic.drawImage(logoImg, logoOffsetX, logoOffsetY, logoWidth, logoHeight, null);
qrImgGraphic
.drawImage(logoImg.getScaledInstance(logoWidth, logoHeight, BufferedImage.SCALE_SMOOTH), logoOffsetX,
logoOffsetY, null);
qrImgGraphic.dispose();
logoImg.flush();
return qrImg;
Expand Down Expand Up @@ -108,20 +110,25 @@ public static BufferedImage drawBackground(BufferedImage qrImg, QrCodeOptions.Bg
bgImg = temp;
}

Graphics2D bgImgGraphic = bgImg.createGraphics();
Graphics2D bgImgGraphic = GraphicUtil.getG2d(bgImg);
if (bgImgOptions.getBgImgStyle() == QrCodeOptions.BgImgStyle.FILL) {
// 选择一块区域进行填充
bgImgGraphic.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));
bgImgGraphic.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
bgImgGraphic.drawImage(qrImg, bgImgOptions.getStartX(), bgImgOptions.getStartY(), qrWidth, qrHeight, null);
bgImgGraphic
.drawImage(qrImg.getScaledInstance(qrWidth, qrHeight, Image.SCALE_SMOOTH), bgImgOptions.getStartX(),
bgImgOptions.getStartY(),

null);
} else {
// 全覆盖方式
int bgOffsetX = (bgW - qrWidth) >> 1;
int bgOffsetY = (bgH - qrHeight) >> 1;
// 设置透明度, 避免看不到背景
bgImgGraphic.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, bgImgOptions.getOpacity()));
bgImgGraphic.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
bgImgGraphic.drawImage(qrImg, bgOffsetX, bgOffsetY, qrWidth, qrHeight, null);
bgImgGraphic.drawImage(qrImg.getScaledInstance(qrWidth, qrHeight, Image.SCALE_SMOOTH), bgOffsetX, bgOffsetY,
null);
bgImgGraphic.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));
}
bgImgGraphic.dispose();
Expand Down Expand Up @@ -163,17 +170,21 @@ public static List<ImmutablePair<BufferedImage, Integer>> drawGifBackground(Buff
temp.getGraphics().drawImage(bgImg.getScaledInstance(bgW, bgH, Image.SCALE_SMOOTH), 0, 0, null);
bgImg = temp;

Graphics2D bgGraphic = bgImg.createGraphics();
Graphics2D bgGraphic = GraphicUtil.getG2d(bgImg);
if (bgImgOptions.getBgImgStyle() == QrCodeOptions.BgImgStyle.FILL) {
// 选择一块区域进行填充
bgGraphic.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));
bgGraphic.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
bgGraphic.drawImage(qrImg, bgOffsetX, bgOffsetY, qrWidth, qrHeight, null);
bgGraphic
.drawImage(qrImg.getScaledInstance(qrWidth, qrHeight, Image.SCALE_SMOOTH), bgOffsetX, bgOffsetY,
null);
} else {
// 全覆盖模式, 设置透明度, 避免看不到背景
bgGraphic.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, bgImgOptions.getOpacity()));
bgGraphic.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
bgGraphic.drawImage(qrImg, bgOffsetX, bgOffsetY, qrWidth, qrHeight, null);
bgGraphic
.drawImage(qrImg.getScaledInstance(qrWidth, qrHeight, Image.SCALE_SMOOTH), bgOffsetX, bgOffsetY,
null);
bgGraphic.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));
}
bgGraphic.dispose();
Expand Down Expand Up @@ -252,7 +263,7 @@ public static BufferedImage drawQrInfo(QrCodeOptions qrCodeConfig, BitMatrixEx b
if (bitMatrix.getByteMatrix().get(x, y) == 0) {
// 探测图形内部的元素与二维码的01点图绘制逻辑分开
// 绘制二维码中不在探测图形内部的0点图
if (!detectLocation.detectedArea() && qrCodeConfig.getDetectOptions().getSpecial()) {
if (!detectLocation.detectedArea() || !qrCodeConfig.getDetectOptions().getSpecial()) {
drawQrDotBgImg(qrCodeConfig, g2, leftPadding, topPadding, infoSize, x, y);
}
continue;
Expand Down Expand Up @@ -369,8 +380,9 @@ private static void drawDetectImg(QrCodeOptions qrCodeConfig, Graphics2D g2, Bit
BufferedImage detectedImg = qrCodeConfig.getDetectOptions().chooseDetectedImg(detectLocation);
if (detectedImg != null) {
// 使用探测图形的图片来渲染
g2.drawImage(detectedImg, leftPadding + x * infoSize, topPadding + y * infoSize,
infoSize * detectCornerSize, infoSize * detectCornerSize, null);
g2.drawImage(detectedImg
.getScaledInstance(infoSize * detectCornerSize, infoSize * detectCornerSize, Image.SCALE_SMOOTH),
leftPadding + x * infoSize, topPadding + y * infoSize, null);

// 图片直接渲染完毕之后,将其他探测图形的点设置为0,表示不需要再次渲染
for (int addX = 0; addX < detectCornerSize; addX++) {
Expand All @@ -394,13 +406,15 @@ private static void drawDetectImg(QrCodeOptions qrCodeConfig, Graphics2D g2, Bit

private static void drawQrDotBgImg(QrCodeOptions qrCodeConfig, Graphics2D g2, int leftPadding, int topPadding,
int infoSize, int x, int y) {
// 如果没有指定二维码中0点对应的背景图,则不做任何处理
if (qrCodeConfig.getDrawOptions().getBgImg() == null) {
return;
}

// 绘制二维码背景图
g2.drawImage(qrCodeConfig.getDrawOptions().getBgImg(), leftPadding + x * infoSize, topPadding + y * infoSize,
infoSize, infoSize, null);
QrCodeOptions.DrawStyle.IMAGE
.draw(g2, leftPadding + x * infoSize, topPadding + y * infoSize, infoSize, infoSize,
qrCodeConfig.getDrawOptions().getBgImg(), null);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,12 @@ public static DrawOptionsBuilder builder() {

public static class DrawOptionsBuilder {
/**
* 着色颜色
* 二维码居中 1对应的着色颜色
*/
private Color preColor;

/**
* 背景颜色
* 二维码矩阵中 0对应的背景颜色
*/
private Color bgColor;

Expand All @@ -365,14 +365,17 @@ public static class DrawOptionsBuilder {
private boolean diaphaneityFill;

/**
* 绘制的二维码文字
* 文字二维码中,用于渲染的文字库,支持按字符顺序or随机两种展现方式(说明:英文不友好)
*/
private String text;

/**
* 文字二维码,渲染模式
*/
private TxtMode txtMode;

/**
* 生成文字二维码时的字体
* 文字二维码,字体名
*/
private String fontName;

Expand All @@ -386,12 +389,12 @@ public static class DrawOptionsBuilder {
private Integer fontStyle;

/**
* 绘制的背景图片
* 二维码矩阵中,0点对应绘制的背景图片, 1点对应绘制的图片在 imgMapper 中
*/
private BufferedImage bgImg;

/**
* 绘制样式
* 二维码绘制样式
*/
private DrawStyle drawStyle;

Expand Down Expand Up @@ -612,7 +615,7 @@ public boolean expand(DotSize dotSize) {

@Override
public void draw(Graphics2D g2d, int x, int y, int w, int h, BufferedImage img, String txt) {
g2d.drawImage(img, x, y, w, h, null);
g2d.drawImage(img.getScaledInstance(w, h, Image.SCALE_SMOOTH), x, y, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,27 @@ public void imgQr3() {
}
}

@Test
public void imgQr4() {
try {
String msg = "http://weixin.qq.com/r/FS9waAPEg178rUcL93oH";
int size = 500;
boolean ans = QrCodeGenWrapper.of(msg)
.setW(size)
.setH(size)
.setErrorCorrection(ErrorCorrectionLevel.M)
.setDrawBgColor(ColorUtil.OPACITY)
.setDrawBgImg("overbg/b.png")
.setDrawStyle(QrCodeOptions.DrawStyle.IMAGE)
.setDrawImg("overbg/a.png")
// .setDetectSpecial()
.asFile("/tmp/imgQr4.png");
} catch (Exception e) {
e.printStackTrace();
}
}


/**
* 动态二维码,和背景图的使用姿势一样,唯一的区别就是背景图是动态的
*/
Expand Down

0 comments on commit d2c87f7

Please sign in to comment.