java生成二维码内部放入中文介绍

2021-06-05 12:02

阅读:552

标签:false   getwidth   添加   exists   common   hand   isp   ade   barcode   

//工具类

package test;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
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.BinaryBitmap;
import com.google.zxing.EncodeHintType;
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;
/*
 * 语言:java 工具jar:zxing 版本:3.2.1
 * 使用前需要导入jar包或者依赖
 * 生成和解码QR工具类
 * 参考自:https://www.cnblogs.com/mr-wuxiansheng/p/7256507.html
 */
public class QR {
	 private static final int BLACK = 0xFF000000;
     private static final int WHITE = 0xFFFFFFFF;
     // LOGO宽度  
     private static final int LOGO_WIDTH = 60;  
     // LOGO高度  
     private static final int LOGO_HEIGHT = 60;  
     //二维码
     private static final int QCwidth = 600;
     private static final int QCheight = 600;
     //图片格式
     private static final String format="png";
     /**
 	 * 
 	 * 图片高度增加60
 	 * 
 	 */
 	private static final int PIC_HEIGHT=QCheight+60;
	public static String createQR(String outPath,String content) {
		 Hashtable ht = new Hashtable ();
         ht.put (EncodeHintType.CHARACTER_SET, "UTF-8");
		try {
			BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE, QCwidth, QCheight, ht);
			 int b_width = bitMatrix.getWidth ();
	         int b_height = bitMatrix.getHeight ();
	         // 建立图像缓冲器
	         BufferedImage image = new BufferedImage (b_width, b_height, BufferedImage.TYPE_3BYTE_BGR);
	         for ( int x = 0; x  LOGO_WIDTH) {   width = LOGO_WIDTH;       }  
		      if (height > LOGO_HEIGHT) {   height = LOGO_HEIGHT;              }  
		      Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);  
		      BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
		      Graphics g = tag.getGraphics();  
		      g.drawImage(image, 0, 0, null); // 绘制缩小后的图  
		      g.dispose();  
		      src = image;  
		  }  
		  // 插入LOGO  
		  Graphics2D graph = source.createGraphics();  
		  int x = (QCwidth - width) / 2;  
		  int y = (QCheight - height) / 2;  
		  graph.drawImage(src, x, y, width, height, null);  
		  Shape shape = new RoundRectangle2D.Float(x, y, width, height, 6, 6);  
		  graph.setStroke(new BasicStroke(3f));  
		  graph.draw(shape);  
		  graph.dispose();   
		
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
	}
	
	

}

  //启动控制类

package test;

import java.util.Map;

public class CreateQR {
	public static void main(String[] args) {
		//生成QR
		String cqr = QR.createQR("E:test.png", "老银铺百年传承承接各种金银首饰加工,地址苏州市吴江区友谊大厦105号,高级金银加工大师:裴忠云  电话:18362712202");
		System.out.println("生成状态:" + cqr);
		//解析QR
		Map m = QR.analysisQR("E:test.png");
		System.out.println("解析状态:" + m.get("STATE"));
		System.out.println("二维码内容:" + m.get("TEXT"));
	}
	
}

  //pom.xml文件

javax.servlet 
		javax.servlet-api 
		3.1.0org.springframework
    	spring-webmvc
    	4.0.6.RELEASEcom.google.zxing
            core
            3.3.0com.google.zxing
			javase
			3.2.1

  

java生成二维码内部放入中文介绍

标签:false   getwidth   添加   exists   common   hand   isp   ade   barcode   

原文地址:https://www.cnblogs.com/xianz666/p/14629034.html


评论


亲,登录后才可以留言!