生成二维码图片 MatrixToImageWriter .java

package com.ws.smpweb.common;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
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.WriterException;
import com.google.zxing.common.BitMatrix;

/**
 * 生成二维码图片
 */
public final class MatrixToImageWriter {
 
 /**
  * 生成二维码图片文件
  * @param content 二维码图片中所嵌入的内容
  * @param format 图片文件格式(后缀名)
  * @param imgPath 二维码图片生成的位置
  * @param logoPath logo图片的路径
  * @param width 二维码图片的宽
  * @param height 二维码图片的高
  * @return
  * @return boolean 生成二维码成功返回true 
  * @throws WriterException
  * @throws IOException
  */
 public static  BufferedImage makeMatrixImg(String content, String logoPath,int width,int height) throws WriterException, IOException{
  /*
   * 1.将内容写入矩阵
   */
  // 设置矩阵所使用的编码格式
  Hashtable<EncodeHintType,String> hints = new Hashtable<EncodeHintType, String>() ;
  hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
  // 初始化矩阵
  BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
  // 将矩阵图形化
  BufferedImage matrixImage = toBufferredImage(bitMatrix);
 
  /*
   * 2.往即将生成的‘二维码图+logo’图片中载入logo
   */
  // 2.1 读取logo图片
  BufferedImage logoImage = ImageIO.read(new File(logoPath));
 
  /*
   * 3。使用二维码图形创建图形工具
   */
  Graphics2D g2d = matrixImage.createGraphics() ;
  /*
   * 4. 画出中间含有logo的二维码图形对象
   */
  int posW = (matrixImage.getWidth()-logoImage.getWidth())/2 ;
  int posH = (matrixImage.getHeight()-logoImage.getHeight())/2 ;
  g2d.drawImage(logoImage, posW, posH, null);
  g2d.dispose();
  logoImage.flush();
 
  /*
   * 5. 将图片读出到文件中
   */
 
  //boolean writed = ImageIO.write(matrixImage, format, os) ;
  return matrixImage;
 }
 
 private static BufferedImage toBufferredImage(BitMatrix bitMatrix){
  int width = bitMatrix.getWidth();
  int height = bitMatrix.getHeight();
 
  BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  for (int i=0; i<width; i++) {
   for(int j=0; j<height; j++){
    image.setRGB(i, j, bitMatrix.get(i, j)?0XFF000000:0XFFFFFF);
   }
  }
  return image;
 }
}
Java生成二维码图片,可以使用第三方库ZXing。下面是一个简单的示例代码: ```java import java.io.File; import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Path; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; public class QRCodeGenerator { public static void main(String[] args) throws WriterException, IOException { String data = "https://www.example.com"; // 要生成二维码的内容 int size = 300; // 二维码图片的大小 // 设置二维码的参数 QRCodeWriter writer = new QRCodeWriter(); BitMatrix bitMatrix = writer.encode(data, BarcodeFormat.QR_CODE, size, size); Path path = FileSystems.getDefault().getPath("qrcode.png"); // 保存图片的路径 ImageIO.write(MatrixToImageWriter.toBufferedImage(bitMatrix), "png", new File(path.toString())); } } ``` 其中,`MatrixToImageWriter.toBufferedImage()`是一个辅助方法,用于将`BitMatrix`对象转换为`BufferedImage`对象,代码如下: ```java import java.awt.Graphics2D; import java.awt.image.BufferedImage; import com.google.zxing.common.BitMatrix; public class MatrixToImageWriter { private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; private MatrixToImageWriter() {} public 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; } } ``` 这样就可以生成一个二维码图片,并保存在本地。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值