二维码总结地址(俩种代码演示,够实际开发使用)

二维码总结地址(大佬谈理论):
https://blog.csdn.net/liulina603/article/details/42099121

代码演示:有不足之处(菜鸟上代码)

package tbase.bean;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import jp.sourceforge.qrcode.data.QRCodeImage;

import com.alibaba.fastjson.JSONObject;
import com.swetake.util.Qrcode;

public class QRCodeUtils {

/**
 * @param args
 *@param
 */

	/**
	 * 
	 * import jp.sourceforge.qrcode.data.QRCodeImage;

import com.swetake.util.Qrcode; 在Qrcode 包中
* 编码字符串内容到目标File对象中
*
* @param encodeddata 编码的内容
* @param destFile 生成file文件
* @throws IOException
*/
public static void qrCodeEncode(String encodeddata, File destFile) throws IOException {
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeErrorCorrect(‘M’); // 纠错级别(L 7%、M 15%、Q 25%、H 30%)和版本有关
qrcode.setQrcodeEncodeMode(‘B’);
qrcode.setQrcodeVersion(7); // 设置Qrcode包的版本7 (取值0-40)

	/*	byte[] d = encodeddata.getBytes("UTF-8");*/	// 字符长度小于-127~127
		byte[] d = encodeddata.getBytes("UTF-8");	
		BufferedImage bi = new BufferedImage(139, 139, BufferedImage.TYPE_INT_RGB);
		// createGraphics	// 创建图层
		Graphics2D g = bi.createGraphics();
		
		g.setBackground(Color.WHITE);	// 设置背景颜色(白色)
		g.clearRect(0, 0, 139, 139);	// 矩形 X、Y、width、height
		g.setColor(Color.BLACK);	// 设置图像颜色(黑色)
         //(不能中文)能处理的字符串长度是122
		/*if (d.length > 0 && d.length < 123) {
			boolean[][] b = qrcode.calQrcode(d);
			for (int i = 0; i < b.length; i++) {
				for (int j = 0; j < b.length; j++) {
					if (b[j][i]) {
						g.fillRect(j * 3 + 2, i * 3 + 2, 3, 3);
					}
				}
			}
		}*/
		if (d.length > 0 && d.length < 123) {
			boolean[][] b = qrcode.calQrcode(d);
			for (int i = 0; i < b.length; i++) {
				for (int j = 0; j < b.length; j++) {
					if (b[j][i]) {
						g.fillRect(j * 3 + 2, i * 3 + 2, 3, 3);
					}
				}
			}
		}
		Image img = ImageIO.read(new File("D:/erha.jpg"));  //logo
		//logo宽高
        int width = 139 / 5;
        int height = 139 / 5;
        //logo起始位置,此目的是为logo居中显示
        int x = (139 - width) / 2;
        int y = (139 - height) / 2;
		g.drawImage(img, x, y,width,height, null);
				
		g.dispose(); // 释放此图形的上下文以及它使用的所有系统资源。调用 dispose 之后,就不能再使用 Graphics 对象
		bi.flush(); // 刷新此 Image 对象正在使用的所有可重构的资源

// ImageIO.write(bi, “png”, response.getOutputStream());
ImageIO.write(bi, “png”, destFile);
System.out.println(“Input Encoded data is:” + encodeddata);
}

	public static void main(String[] args) {
		String FilePath = "d:/qrcode.png";
		File qrFile = new File(FilePath);
 
		// 二维码内容(不能处理中文)
		
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("country", "China");
		jsonObject.put("Province","JiangSu");
        jsonObject.put("city","LiangXi");
        JSONObject addressJson = new JSONObject();
        addressJson.put("SheQu","XingFu");
        addressJson.put("jiedao","jiefang");
        addressJson.put("menhao","88");
        jsonObject.put("address",addressJson);
                // jsonObject.get(addressJson);
        //将json对象转换为json格式的字符串
         String encodeddata = jsonObject.toJSONString();
        
      //  String encodeddata = "23rwegdfkbsvjfdjkddddddddedddddddddsbvjfdslkbvbd333333fjksdfbwwwwwwwwwwwwwwvjdkfsbvfdjsbfvwrrrrrrrrrrrrrtgrvvvvvvvvvvvsn111";
        System.out.println("(不能中文)能处理的字符串长度是"+encodeddata.length());
		try {
			QRCodeUtils.qrCodeEncode(encodeddata, qrFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
 
class J2SEImage implements QRCodeImage {
	BufferedImage image;
 
	public J2SEImage(BufferedImage image) {
		this.image = image;
	}
 
	public int getWidth() {
		return image.getWidth();
	}
 
	public int getHeight() {
		return image.getHeight();
	}
 
	public int getPixel(int x, int y) {
		return image.getRGB(x, y);
	}

}
下面这个jar与JDK版本无关
在这里插入图片描述在这里插入图片描述以上俩个jar是谷歌提供的,下载时注意版本要与JDK对应,否则不能使用。

如果你的JDK版本在7以上,提供下面代码供参考使用
package com.wkcto.crm.utils;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
public class QRCodeTest {
public static void main(String[] args) throws WriterException {
generateQRcode();
}
private static void generateQRcode() throws WriterException {
//{“country”:“China”,“Province”:“江苏省”,“city”:“梁溪区”,“address”:{“社区”:“幸福社区”,“街道”:“解放路”,“门号”:“888号”}}
//创建一个json对象
JSONObject jsonObject=new JSONObject();
jsonObject.put(“country”, “China”);
jsonObject.put(“Province”,“江苏省”);
jsonObject.put(“city”,“梁溪区”);
JSONObject addressJson = new JSONObject();
addressJson.put(“社区”,“幸福社区”);
addressJson.put(“街道”,“解放路”);
addressJson.put(“门号”,“888号”);
jsonObject.put(“address”,addressJson);
// jsonObject.get(addressJson);
//将json对象转换为json格式的字符串
String jsonString = jsonObject.toJSONString();
// 创建一个矩阵对象
Map<EncodeHintType ,Object> hintMap=new HashMap<EncodeHintType, Object>();
hintMap.put(EncodeHintType.CHARACTER_SET, “UTF-8”);
//二维码携带信息
String contents = “robinli”;
BitMatrix bitMatrix=new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, 200, 200,hintMap);
//BitMatrix bitMatrix=new MultiFormatWriter().encode(“robinli”, BarcodeFormat.QR_CODE, 200, 200,hintMap);
String filePath=“D://”;
String fileName=“qrCode.jpg”;

//以下没有这个 path类
 Path path = FileSystems.getDefault().getPath(filePath,fileName);
	//将矩阵对象转化为图片
	 MatrixToImageWriter.writeToPath(bitMatrix, "jpg", path); 
}

}

猜你喜欢

转载自blog.csdn.net/qq_30347133/article/details/88719907