二维码生成工具类


import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Base64;
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;

	/**
	 * <span style="font-size:18px;font-weight:blod;">ZXing 方式生成二维码</span>
	 * @param text    <a href="javascript:void();">二维码内容</a>
	 * @param width    二维码宽
	 * @param height    二维码高
	 * @param outPutPath    二维码生成保存路径
	 * @param imageType        二维码生成格式
	 */
	public static void zxingCodeCreate(String text, int width, int height, String outPutPath,String fileName, String imageType){
		Map<EncodeHintType, String> his = new HashMap<EncodeHintType, String>();
	    //设置编码字符集
	    his.put(EncodeHintType.CHARACTER_SET, "utf-8");
	    try {
	        //1、生成二维码
	        BitMatrix encode = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, his);
	        
	        //2、获取二维码宽高
	        int codeWidth = encode.getWidth();
	        int codeHeight = encode.getHeight();
	        
	        //3、将二维码放入缓冲流
	        BufferedImage image = new BufferedImage(codeWidth, codeHeight, BufferedImage.TYPE_INT_RGB);
	        for (int i = 0; i < codeWidth; i++) {
	            for (int j = 0; j < codeHeight; j++) {
	                //4、循环将二维码内容定入图片
	                image.setRGB(i, j, encode.get(i, j) ? BLACK : WHITE);
	            }
	        }
	        File outPut = new File(outPutPath);
	        //如果文件夹不存在创建图片
	        if(!outPut.isDirectory()) {
	        	outPut.mkdirs();
	        }
	        File outPutImage = new File(outPutPath+"/"+fileName);
	        //如果图片不存在创建图片
	    	if(!outPutImage.exists()) {
	    		outPutImage.createNewFile();
	    	}	
	        //5、将二维码写入图片
	        ImageIO.write(image, imageType, outPutImage);
	        System.out.println("生成成功");
	    } catch (WriterException e) {
	        e.printStackTrace();
	        System.out.println("二维码生成失败");
	    } catch (IOException e) {
	        e.printStackTrace();
	        System.out.println("生成二维码图片失败");
	    }
	}


	/**
	 * 
	 * 生成带背景二维码图片
	 * @param text 二维码内容 例:https://www.baidu.com
	 * @param width 二维码宽度
	 * @param height 二维码高度
	 * @param bigPath 背景图路径
	 * @param x   二维码距离左边像素
	 * @param y   二维码距离顶部像素
	 * @return
	 * @throws WriterException 
	 * @throws IOException 
	 */
	public static String zxingCodeCreateImage(String text, int width, int height,String bigPath, String x, String y) throws WriterException, IOException{
		Map<EncodeHintType, String> his = new HashMap<EncodeHintType, String>();
	    //设置编码字符集
	    his.put(EncodeHintType.CHARACTER_SET, "utf-8");
        //1、生成二维码
        BitMatrix encode = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, his);
        
        //2、获取二维码宽高
        int codeWidth = encode.getWidth();
        int codeHeight = encode.getHeight();
        
        //3、将二维码放入缓冲流
        BufferedImage image = new BufferedImage(codeWidth, codeHeight, BufferedImage.TYPE_INT_RGB);
        for (int i = 0; i < codeWidth; i++) {
            for (int j = 0; j < codeHeight; j++) {
                //4、循环将二维码内容定入图片
                image.setRGB(i, j, encode.get(i, j) ? BLACK : WHITE);
            }
        }
        //设置圆角
        image=setClip(image, 10);
        return mergeImage(bigPath, image, x, y);
	}

	/**
	 * 图片切圆角
	 * @param srcImage
	 * @param radius
	 * @return
	 */
	public static BufferedImage setClip(BufferedImage srcImage, int radius){
	    int width = srcImage.getWidth();
	    int height = srcImage.getHeight();
	    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
	    Graphics2D gs = image.createGraphics();

	    gs.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	    gs.setClip(new RoundRectangle2D.Double(0, 0, width, height, radius, radius));
	    gs.drawImage(srcImage, 0, 0, null);
	    gs.dispose();
	    return image;
	}

	/**
	 * 将二维码绘图到图片中
	 * @param bigPath
	 * @param small
	 * @param x
	 * @param y
	 * @throws IOException
	 */
	public static String mergeImage(String bigPath,  BufferedImage small, String x, String y) throws IOException {
		 
        BufferedImage big = ImageIO.read(new File(bigPath));
        Graphics2D g = big.createGraphics();
        float fx = Float.parseFloat(x);
        float fy = Float.parseFloat(y);
        int x_i = (int) fx;
        int y_i = (int) fy;
        g.drawImage(small, x_i, y_i, small.getWidth(), small.getHeight(), null);
        g.dispose();
//	        ImageIO.write(big, "png", new File(bigPath));

        
        ByteArrayOutputStream baos = new ByteArrayOutputStream();// io流
        ImageIO.write(big, "png", baos);
        byte[] bytes = baos.toByteArray();// 转换成字节
        Base64.Encoder encoder = Base64.getEncoder();
        String jpgBase64 = encoder.encodeToString(bytes).trim();// 转换成base64串
        jpgBase64 = jpgBase64.replaceAll("\n", "").replaceAll("\r", "");// 删除 \r\n
        return "data:image/jpeg;base64," + jpgBase64;
	}
	
	public static void main(String[] args) {
		String path = "F:\\project\\image";
		try {
			System.out.println(zxingCodeCreateImage("https://www.baidu.com", 200, 200,path+"/a.png", "200", "500"));
		} catch (WriterException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值