小言_互联网的博客

Java生成带logo的二维码

445人阅读  评论(0)

使用纯Java生成二维码,首先需要引入jar包:core-x.x.x(com.google.zxing),下载地址:https://mvnrepository.com/artifact/com.google.zxing/core/3.3.0

代码都写了注释,二维码的工具类:

package QR_Code.zxing;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import jp.sourceforge.qrcode.util.Color;

/** zxing二维码的工具类 */
public class ZXingUtil {
	
	/** 加密生成二维码: 
	 * @throws Exception 
	 * 	content :需要加密的字符串
	 * 	BarcodeFormat.QR_CODE: 需要解析的类型(二维码)
	 * 	hints: 存放加密涉及的一些参数(编码、排错率等)
	 * */
	public static  void encodeImg(String imgPath,String format,String content,int width,int height,String logo) throws Exception {
		Hashtable<EncodeHintType,Object> hints = new Hashtable<>();
		//排错率 :L<M<Q<H
		hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
		//编码格式
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		//外边距
		hints.put(EncodeHintType.MARGIN, 1);
		
		BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hints);
		
		//内存中的图片:此时需要的图片是二维码,而二维码则需要一个 boolean[][] -->com.google.zxing.common.BitMatrix
		BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		for(int x=0; x<width; x++) {
			for(int y=0; y<height; y++) {
				img.setRGB(x, y, (bitMatrix.get(x, y) ? Color.BLACK :Color.WHITE));
			}
		}
		
		//画 logo
		img = LogoUtil.logoMatrix(img, logo);
		
		//String -> file
		File file = new File(imgPath);
		//生成图片
		ImageIO.write(img, format, file);
	}
	
	
	/**
	 * 	解码
	 * @param file
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void decodeImg(File file) throws Exception {
		if(!file.exists()) return ;
		// 读取到内存
		BufferedImage image = ImageIO.read(file);
		
		MultiFormatReader formatReader = new MultiFormatReader();
		
		LuminanceSource source = new BufferedImageLuminanceSource(image);
		Binarizer binarizer = new HybridBinarizer(source);
		BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
		
		Map map = new HashMap<>();
		
		map.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		Result result = formatReader.decode(binaryBitmap,map);
		System.err.println("解析结果:"+result.toString());
	}
	
}

添加 logo的工具类:

package QR_Code.zxing;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

/** logo类 */
public class LogoUtil {
	//传入logo、二维码 ->带logo的二维码
	public static BufferedImage logoMatrix(BufferedImage matrixImage,String logo) throws Exception {
		// 产生一个二维码画板
		Graphics2D g2 = matrixImage.createGraphics();
		
		//String -> BufferedImage(内存缓冲区的图片)
		BufferedImage logoImg = ImageIO.read(new File(logo));
		
		int height = matrixImage.getHeight();
		int width = matrixImage.getWidth();
		//纯logo图片   参数(logo图,2/5x,y轴位置(横轴纵轴的2/5的位置),1/5是宽高(占整个二维码图片对应的1/5),  null)
		g2.drawImage(logoImg, width*2/5, height*2/5, width*1/5, height*1/5, null);
		//产生一个画笔  画白色圆角正方形  参数(5, 笔尖类型 ,连接处的形状 JOIN_ROUND 圆滑)
		BasicStroke stroke = new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
		g2.setStroke(stroke);
		//创建一个正方形(存放logo的正方形)
		RoundRectangle2D.Float  round = new RoundRectangle2D.Float( width*2/5, height*2/5, width*1/5, height*1/5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
		g2.setColor(Color.WHITE);
		g2.draw(round);
		
		//灰色边框
		BasicStroke stroke2 = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
		g2.setStroke(stroke2);
		//再来一个正方形(logo覆盖的正方形),位置参数微调
		RoundRectangle2D.Float  round2 = new RoundRectangle2D.Float( width*2/5+2, height*2/5+2, width*1/5-4, height*1/5-4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
		g2.setColor(Color.GRAY);//灰色
		//也可以这样设置颜色
//		Color color = new Color(128,128,128);
		g2.draw(round2);
		g2.dispose();
		matrixImage.flush();
		return matrixImage;
	}
}

最后是测试类:

package QR_Code.zxing;

import java.io.File;

/**zxing方式的二维码*/
public class Test {
	public static void main(String[] args) throws Exception {
		//图片路径
		String imgPath = "src/zxing二维码.gif";
		//二维码图片中加密的内容
		String content = "helloworld";
		//logo图片
		String logo = "src/logo.png";
		//生成二维码
		ZXingUtil.encodeImg(imgPath, "gif", content, 430, 430,logo);
		
		//解码
		ZXingUtil.decodeImg(new File(imgPath));
	}
}


转载:https://blog.csdn.net/weixin_43274097/article/details/101905424
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场