工具类的使用,分享给大家。先看效果:
无logo时:
有logo时:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
/**
* @Date 创建时间: 2020-11-03 09:54
* @Author 作者姓名: Liux
* @Version 1.0
* @Copyright Copyright by
* @Direction 类说明 二维码写入到本地文件、或者二维码写入到流、以及二维码加入logo的操作
*/
public class QRCodeUtil {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private QRCodeUtil() {
}
private static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height, 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) ? BLACK : WHITE);
}
}
return image;
}
/***
* 文件方式生成
* @param matrix
* @param format
* @param file
* @throws IOException
*/
public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
/***
* 文件方式生成
* @param matrix
* @param format
* @param file
* @throws IOException
*/
public static void writeLogoToFile(BitMatrix matrix, String format, File file) throws IOException {
BufferedImage image = toBufferedImage(matrix);
//设置logo图标
image = QRLogoConfig.LogoMatrix( image );
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
/***
* 流方式生成 - 普通版本
* @param matrix
* @param format
* @param stream
* @throws IOException
*/
public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, stream)) {
throw new IOException("Could not write an image of format " + format);
}
}
/***
* 流方式生成 - 待logo版本
* @param matrix
* @param format
* @param stream
* @throws IOException
*/
public static void writeLogoToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {
BufferedImage image = toBufferedImage(matrix);
//设置logo图标
BufferedImage logoImage = QRLogoConfig.LogoMatrix( image );
File outputFile = new File("D:" + File.separator + "new.gif");
ImageIO.write(image, format, outputFile ) ;
if ( !ImageIO.write( logoImage , format, stream ) ) {
throw new IOException("Could not write an image of format " + format);
}
}
public static void main(String[] args) throws Exception {
String text = "http://www.baidu.com";
int width = 300;
int height = 300;
// 二维码的图片格式
String format = "gif";
Hashtable hints = new Hashtable();
// 内容所使用编码
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
// 生成二维码
File outputFile = new File("D:" + File.separator + "new.gif");
QRCodeUtil.writeToFile(bitMatrix, format, outputFile);
QRCodeUtil.writeLogoToFile(bitMatrix, format, outputFile);
}
}
import org.springframework.core.io.ClassPathResource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
/**
* @Date 创建时间: 2020-11-03 10:09
* @Author 作者姓名: Liux
* @Version 1.0
* @Copyright Copyright by
* @Direction 类说明
* 二维码 添加 logo图标 处理的方法,
* 模仿微信自动生成二维码效果,有圆角边框,logo和二维码间有空白区,logo带有灰色边框
*/
public class QRLogoConfig {
/***
* 微信支付时使用的logo最好不要超过 45 * 45,否则有概率扫一扫识别不成功
*/
private static int logo_width = 45 ;
private static int logo_height = 45 ;
/**
* 设置 logo
* @param matrixImage 源二维码图片
* @return 返回带有logo的二维码图片
* @throws IOException
* @author
*/
public static BufferedImage LogoMatrix(BufferedImage matrixImage) throws IOException{
//读取Logo图片 - springboot resource下面的文件夹下读取配置
ClassPathResource classPathResource = new ClassPathResource("static/common/img/logo.png");
InputStream inputStream = classPathResource.getInputStream();
BufferedImage logoImage = ImageIO.read( inputStream );
return mergeImage( matrixImage , logoImage , null ) ;
}
/**
* 待合并的两张图必须满足这样的前提,如果水平方向合并,则高度必须相等;如果是垂直方向合并,宽度必须相等。
* mergeImage方法不做判断,自己判断。
*
* @param logoImage
* 待合并的第一张图
* @param targetImage
* 带合并的第二张图
* @param isHorizontal
* 为true时表示水平方向合并,为false时表示垂直方向合并
* @return 返回合并后的BufferedImage对象
* @throws IOException
*/
public static BufferedImage mergeImage( BufferedImage targetImage, BufferedImage logoImage, String isHorizontal) throws IOException {
int w1 = targetImage.getWidth();
int h1 = targetImage.getHeight();
int w2 = logoImage.getWidth();
int h2 = logoImage.getHeight();
logo_width = w2 ;
logo_height = h2 ;
// 从图片中读取RGB
int[] ImageArrayOne = new int[w1 * h1];
ImageArrayOne = targetImage.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行扫描图像中各个像素的RGB到数组中
int[] ImageArrayTwo = new int[w2 * h2];
ImageArrayTwo = logoImage.getRGB(0, 0, w2, h2, ImageArrayTwo, 0, w2);
// 生成新图片
BufferedImage DestImage = null;
if(isHorizontal==null){
if(true){
DestImage = new BufferedImage(w1, h1, BufferedImage.TYPE_INT_RGB);
//第一步-画出底图的信息
DestImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
//第二步-画出logo图片的信息,开始的坐标,长宽设置
int startX = ( w1 / 2 ) - (logo_width / 2) ;
int starty = ( h1 / 2 ) - (logo_height / 2) ;
DestImage.setRGB( startX,starty, logo_width ,logo_height , ImageArrayTwo, 0, logo_width );
}
}else if (isHorizontal.equals("1")) { // 水平方向合并
DestImage = new BufferedImage(w1+w2, h1, BufferedImage.TYPE_INT_RGB);
DestImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
DestImage.setRGB(w1, 20, w2, h2, ImageArrayTwo, 0, w2);
} else if(isHorizontal.equals("2")) { // 垂直方向合并
DestImage = new BufferedImage(w1, h1 + h2,
BufferedImage.TYPE_INT_RGB);
DestImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
DestImage.setRGB(0, h1, w2, h2, ImageArrayTwo, 0, w2); // 设置下半部分的RGB
}
return DestImage;
}
}
依赖的Maven包:
<!-- google二维码 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.1.0</version>
</dependency>
微信二维码支付时调用:
//二维码宽高
int width=300,height=300 ;
//微信统一下单后,返回的二维码内容字符
String text = "" ;
//二维码的图片格式
String format = "gif";
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
//内容所使用编码
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
//生成简单二维码
//QRCodeUtil.writeToStream(bitMatrix, format, response.getOutputStream());
//生成二维码带logo
QRCodeUtil.writeLogoToStream(bitMatrix, format, response.getOutputStream());
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/160819.html