1. 程式人生 > >簡單二維碼的制作

簡單二維碼的制作

arr static style fill rate ext dmi supported google

1.這裏介紹三種二維碼的制作方法

現在有很多二維碼的軟件所有這篇博客是給大家了解一下它的一些原理:

第一種的制作需要在官網上下載zxing的一個源碼文件,這個可以直接在百度可以下載的

下載好了

第一步需要的是把zxing-zxing-3.3.3//core/src//main//java//com和zxing-zxing-3.3.3//javase//src//main//java//com這兩個文件復制到新建的項目中,放入到項目中這時系統會報錯,這個錯誤我們先不管它。直接把這個項目給導出,選擇jar file一直點下一步就可以了,在Finish的

時候在Options的選項框中勾選第一個就可以了。這個時候導出已經完成了

現在就進入了

第二步了;新建一個項目把剛才的導出的jar包導入到新建的項目中

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;

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;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

/**
 * 生成二維碼
 * @author Administrator
 *
 */
public class CreateQRCode {

	public static void main(String[] args) {
		
		int width=300;
		int height=300;
		String format="png";
		String content="http://www.baidu.com";
		//定義二維碼的參數
		HashMap map=new HashMap();
		map.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
		map.put(EncodeHintType.MARGIN,2);
		
		try {
			
			BitMatrix bit=new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);
			Path file=new File("d://text//img.png").toPath();
			MatrixToImageWriter.writeToPath(bit, format, file);
			
		} catch (WriterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

}
第一種就已經實現了
下面介紹第二種的方法:

第二種要導入Qrcode的jar包,這種的源碼不好下載
所以這你就直接寫代碼了:
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.UnsupportedEncodingException;

import javax.imageio.ImageIO;

import com.swetake.util.Qrcode;

public class text {

    public static void
main(String[] args) throws Exception { Qrcode x=new Qrcode(); x.setQrcodeErrorCorrect(‘M‘);// 糾錯等級 x.setQrcodeEncodeMode(‘B‘);//N代表數字 A代表a-z,B代表其他字符 x.setQrcodeVersion(7);//版本號 String str="www.imoo .com"; int width=67+12*(7-1); int height=67+12*(7-1); BufferedImage bu=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); //java的畫圖 Graphics2D gs=bu.createGraphics(); gs.setBackground(Color.WHITE); gs.setColor(Color.BLACK); gs.clearRect(0, 0, width, height); int pi=2;//偏移量 //gb2312轉漢字 byte[] by=str.getBytes("gb2312"); if(by.length>0 && by.length<120) { boolean[][] s=x.calQrcode(by); for (int i = 0; i < s.length; i++) { for (int j = 0; j < s.length; j++) { if(s[j][i]) { gs.fillRect(j*3+pi, i*3+pi, 3, 3); } } } } gs.dispose(); bu.flush(); ImageIO.write(bu, "png", new File("d://JA//er.png")); } }
ImageIO.write(bu, "png", new File("d://JA//er.png"));這是二維碼產生的圖片地址路徑

 第二種用的不多,只供大家參考就行了

下面直接進入到第三種方法:是通過jquery框架來實現的:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
<title>Insert title here</title>

<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/sAjs/jquery.qrcode.min.js"></script>
</head>
<body>

<div id="qrcode"></div>

    <script type="text/javascript">
jQuery("#qrcode").qrcode("http://www.baidu.com");
    </script>
</body>
</html>

以上就是三種的寫法哦

簡單二維碼的制作