码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2021-04-08 13:33:23      阅读:0      评论:0      收藏:0      [点我收藏+]

标签: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<EncodeHintType, Object> ht = new Hashtable<EncodeHintType, Object> ();
         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 < b_width; x++ ){
	             for ( int y = 0; y < b_height; y++ ){
	                 image.setRGB (x, y, bitMatrix.get (x, y) ? BLACK : WHITE);
	             }
	         }
	         //插入log
	         //insertImage(image,"D:/logo.png" , false);
	         //二维码下方添加文字
	        
	         // 生成二维码
	         ImageIO.write (image, format, new File (outPath)); //二维码的名称 是 erweima.sgif
			
		} catch (Exception e) {
			e.printStackTrace();
			return "ERROR";
		}
		return "SUCCESS";
	}
	/*
	 * 解析QR码
	 * IN:被解析图片路径
	 * OUT:Map集合:
	 * 		key:RESULT  》》  总结果集,未被解析
	 * 			FORMAT  》》  被解析的二维码格式
	 * 			TEXT  》》  二维码中含有的文本内容
	 * 			STATE 》》 解析状态,成功或失败
	 */
	public static Map analysisQR(String path) {
		Map resMap = new HashMap();		//返回出去的map集合
		try {
            MultiFormatReader formatReader = new MultiFormatReader();
            File file = new File(path);
            BufferedImage image = ImageIO.read(file);	//读取文件
            BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
            HashMap hints = new HashMap();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 字符集
            Result result = formatReader.decode(binaryBitmap, hints);
            resMap.put("RESULT", result);	//总结果集
            resMap.put("FORMAT", result.getBarcodeFormat());	//被解析的二维码格式
            resMap.put("TEXT", result.getText());	//被解析的二维码含有的文本内容
            resMap.put("STATE", "SUCCESS");		//解析状态
        } catch (Exception e) {
            e.printStackTrace();
            resMap.put("STATE", "ERROR");
        }
		return resMap;
	}
	
	/** 
     * 插入LOGO  
* @param source   二维码图片 
* @param logoPath   LOGO图片地址 
* @param needCompress  是否压缩 
* @throws Exception 
*/  
	private static void insertImage(BufferedImage source, String logoPath, boolean needCompress)  {
		System.out.println("logoPath============"+logoPath);
		try {
			 File file = new File(logoPath);  
		  if (!file.exists()) {  
		      throw new Exception("logo file not found.");  
		  }  
		  Image src = ImageIO.read(new File(logoPath));  
		  int width = src.getWidth(null);  
		  int height = src.getHeight(null); 
		  if (needCompress) { // 压缩LOGO  
		      if (width > 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文件

<dependencies>
     <!-- servlet -->
     <dependency> 
		<groupId>javax.servlet</groupId> 
		<artifactId>javax.servlet-api</artifactId> 
		<version>3.1.0</version> 
	</dependency>
	<!-- web -->
	 <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-webmvc</artifactId>
    	<version>4.0.6.RELEASE</version>
    </dependency>
	<!--  二维码-->
	<!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.0</version>
        </dependency>
       
		<dependency>
			<groupId>com.google.zxing</groupId>
			<artifactId>javase</artifactId>
			<version>3.2.1</version>
		</dependency>
     </dependencies>
      

  

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!