Commit 1303f885 authored by gaoyingwei's avatar gaoyingwei

修改二维码生成

parent 1dbb9775
......@@ -35,7 +35,9 @@ public enum ApplicationExceptionEnum implements ServiceExceptionEnum{
REQUEST_TOO_MANY(500, "请求频繁,请稍后重试"),
TASK_CONFIG_ERROR(500, "定时任务配置错误"),
DELETE_MODEL_ERROR(500, "已分配商圈,不能删除");
DELETE_MODEL_ERROR(500, "已分配商圈,不能删除"),
QRCODE_ERROR(500, "二维码生成失败");
ApplicationExceptionEnum(int code, String message) {
this.code = code;
......
package com.emall.flash.utils;
import com.emall.flash.bean.exception.ApplicationException;
import com.emall.flash.bean.exception.ApplicationExceptionEnum;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.UnsupportedEncodingException;
import java.io.*;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
public class CyQrCodeUtil {
private static final Logger log = LoggerFactory.getLogger(CyQrCodeUtil.class);
......@@ -44,4 +56,62 @@ public class CyQrCodeUtil {
throw var8;
}
}
public static InputStream getCode2(String urlCode, int qrWidth, int qrHeight){
ByteArrayInputStream stream = null;
//需要编码的内容
String png_base64 = null;
//返回二维码 并且构造支付包web支付对象
ByteArrayOutputStream baOp = new ByteArrayOutputStream();
try {
BufferedImage bufImg = createImage(urlCode,qrWidth,qrHeight);
// 生成二维码QRCode图片
ImageIO.write(bufImg, "png", baOp);
byte[] bytes = baOp.toByteArray();//转换成字节
stream = new ByteArrayInputStream(bytes);
File file = new File("D:/3.png");
// if (!file.getParentFile().exists()){
// //文件夹不存在 生成
// file.getParentFile().mkdirs();
// }
FileOutputStream fos = null;
BufferedOutputStream bos = null;
fos = new FileOutputStream(file);
fos.write(bytes);
bos = new BufferedOutputStream(fos);
bos.write(bytes);
} catch (Exception e) {
log.error(e.getMessage());
throw new ApplicationException(ApplicationExceptionEnum.QRCODE_ERROR);
}
return stream;
}
private static final String CHARSET = "utf-8";
public static BufferedImage createImage(String content, int qrWidth, int qrHeight) throws Exception {
Map<EncodeHintType, Object> hints = new Hashtable<>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, qrWidth, qrHeight,hints);
qrWidth = bitMatrix.getWidth();
qrHeight = bitMatrix.getHeight();
//创建二维码
BufferedImage image = new BufferedImage(qrWidth, qrHeight, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < qrWidth; x++) {
for (int y = 0; y < qrHeight; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
}
}
//在二维码下方增加文字显示
BufferedImage bi = new BufferedImage(qrWidth, qrHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.setBackground(new Color(0xFF,0xFF,0xFF));
//通过使用当前绘图表面的背景色进行填充来清除指定的矩形。
g2.clearRect(0, 0, qrWidth, qrHeight);
g2.drawImage(image, 0, 0, qrWidth, qrHeight, null); //x,y为image图片的位置
image=bi;
return image;
}
}
......@@ -347,8 +347,8 @@ public class Qrcode {
for(int a = 0; a < 15; ++a) {
byte content = Byte.parseByte(formatInformationArray[formatInformationValue].substring(a, a + 1));
matrixContent[formatInformationX1[i] & 255][formatInformationY1[a] & 255] = (byte)(content * 255);
matrixContent[formatInformationX2[i] & 255][formatInformationY2[a] & 255] = (byte)(content * 255);
matrixContent[formatInformationX1[a] & 255][formatInformationY1[a] & 255] = (byte)(content * 255);
matrixContent[formatInformationX2[a] & 255][formatInformationY2[a] & 255] = (byte)(content * 255);
}
boolean[][] out = new boolean[modules1Side][modules1Side];
......@@ -628,4 +628,4 @@ public class Qrcode {
return res;
}
}
\ No newline at end of file
}
......@@ -63,6 +63,17 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
......
......@@ -157,7 +157,7 @@ public class UserController extends BaseController {
//返回二维码 并且构造支付包web支付对象
ByteArrayOutputStream baOp = new ByteArrayOutputStream();
try {
BufferedImage bufImg = CyQrCodeUtil.qRCodeCommon(id);
BufferedImage bufImg = CyQrCodeUtil.createImage(id,235,235);
// 生成二维码QRCode图片
ImageIO.write(bufImg, "png", baOp);
byte[] bytes = baOp.toByteArray();//转换成字节
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment