Java生成二維碼


單元測試版:

導入jar包:

鏈接:https://pan.baidu.com/s/1qR4Mh0pwBAhpzx5gqCXDHg
提取碼:a1qa
導入里面的zxing3.2.1.jar包

Java代碼:

 

package ewm.test;

import java.io.File;
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.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class Demo {

	public static void main(String[] args) {
		if (orCode("https://www.ak1ak1.com/catalog.html", "F:\\1.jpg")) {
            System.out.println("ok,成功");
        } else {
            System.out.println("no,失敗");
        }
	}
	
	private static boolean orCode(String content, String path) {
        /*
         * 圖片的寬度和高度
         */
        int width = 300;
        int height = 300;
        // 圖片的格式
        String format = "png";
        // 二維碼內容
        // String content = "hello,word";
        // 定義二維碼的參數
        HashMap hints = new HashMap();
        // 定義字符集編碼格式
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // 糾錯的等級 L > M > Q > H 糾錯的能力越高可存儲的越少,一般使用M
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        // 設置圖片邊距
        hints.put(EncodeHintType.MARGIN, 2);
        try {
            // 最終生成 參數列表 (1.內容 2.格式 3.寬度 4.高度 5.二維碼參數)
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            // 寫入到本地
            Path file = new File(path).toPath();
            MatrixToImageWriter.writeToPath(bitMatrix, format, file);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }
	
}

web版本:

導入maven依賴

<dependency>
		   <groupId>com.google.zxing</groupId>
		   <artifactId>core</artifactId>
		   <version>3.3.3</version>
		   <scope>compile</scope>
		</dependency>
		<dependency>
		   <groupId>com.google.zxing</groupId>
		   <artifactId>javase</artifactId>
		   <version>3.3.3</version>
		   <scope>compile</scope>
		</dependency>

Java代碼:

public void dowanload(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {
		Project project = JSON.parseObject(JSONObject.toJSONString(getProject(id).getData(), true),Project.class);
	    //二維碼中包含的信息
	    String content = project.getPageUrl() + "&" + project.getProjectID();
	    Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
	    // 指定編碼格式
	    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
	    // 指定糾錯級別(L--7%,M--15%,Q--25%,H--30%)
	    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
	    // 編碼內容,編碼類型(這里指定為二維碼),生成圖片寬度,生成圖片高度,設置參數
	    BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 200, 200, hints);
	    //設置請求頭
	    response.setHeader("Content-Type","application/octet-stream");
	    response.setHeader("Content-Disposition", "attachment;filename=" + project.getProjectID() + ".png");
	    OutputStream outputStream = response.getOutputStream();
	    MatrixToImageWriter.writeToStream(bitMatrix, "png", outputStream);
	    outputStream.flush();
	    outputStream.close();
	}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM