生成二维码

titledatetagscategories
生成二维码
2019-08-29 03:10:22 -0700
JAVA
二维码
JAVA

所需依赖

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.3.0</version>
</dependency>

代码实现

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

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 class QrCodeUtil{


	private static final int BLACK = 0xFF000000;  
	private static final int WHITE = 0xFFFFFFFF;  

	/**
	 * 生成二维码图片
	 * @param content  内容
	 * @param width    二维码图片宽度  如果有logo图片,不应低于250
	 * @param height   二维码图片高度  如果有logo图片,不应低于250
	 * @param logo     二维码上面的logo  可选
	 * @return  二维码图片
	 */
	public static BufferedImage toBufferedImage(String content,int width,int height,Image logo) {
		MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
		Map<EncodeHintType, String> hints = new HashMap<>();
		//内容所使用编码  
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); 
		BitMatrix matrix =null;
		try {
			matrix = multiFormatWriter.encode(content,BarcodeFormat.QR_CODE, width, height, hints);
		} catch (WriterException e) {
			e.printStackTrace();
		}
		if(matrix == null){return null;}

		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);  
			}  
		}

		//二维码上面添加logo
		if(logo!=null){
			//缩小logo大小 构建图片流
			BufferedImage tag = new BufferedImage(33, 33, BufferedImage.TYPE_INT_RGB);
			//绘制改变尺寸后的图
			tag.getGraphics().drawImage(logo, 0, 0, 33, 33, null);
			Graphics2D g = image.createGraphics(); //画笔对象
			// 设置对线段的锯齿状边缘处理   
			g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
			g.drawImage(logo, (width-tag.getWidth(null))/2, (height-tag.getHeight(null))/2, tag.getWidth(null), tag.getHeight(null), null);
			g.dispose();
		}

		return image;  
	}  

	/**
	 * 生成二维码,并保存为文件
	 * @param content  二维码包含的内容
	 * @param width    二维码图片宽度  如果有logo图片,不应低于250
	 * @param height   二维码图片高度  如果有logo图片,不应低于250
	 * @param format   生成二维码图片的扩展名  如jpg/png等
	 * @param file     二维码保存文件
	 * @param logo     logo图片  可选
	 */
	public static void writeToFile(String content,int width,int height,String format,File file,Image logo){  

		BufferedImage image = toBufferedImage(content,width,height,logo);  
		try {
			ImageIO.write(image, format, file);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}  

    /**
     * 生成二维码,并写到流中
     * @param content   二维码包含的内容
     * @param width     二维码图片宽度  如果有logo图片,不应低于250
     * @param height    二维码图片高度  如果有logo图片,不应低于250
     * @param format    生成二维码图片的扩展名  如jpg/png等
     * @param stream    输出流
     * @param logo      logo图片  可选
     */
	public static void writeToStream(String content,int width,int height, String format,OutputStream outPutstream,Image logo)  
			 {  
		BufferedImage image = toBufferedImage(content,width,height,logo);  
		try {
			ImageIO.write(image, format, outPutstream);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
     
	
}

调用实例

	public static void main(String[] args) {
		try {
			String content = "http://gys.zjtcn.com/project/detail/SZLHXQ22_gcxx_t_current_p1.html"; 
			//二维码保存位置  
			File outputFile = new File("d:/14.jpg"); 
            
			writeToFile(content, 250,250,"png",outputFile,ImageIO.read(new File("D:/qrcode_logo.png")));  
			writeToStream(content, 250, 250, "png", System.out, ImageIO.read(new File("D:/qrcode_logo.png")));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值