Java二维码实现

 

      借助第三方Jar包实现二维码:

      http://central.maven.org/maven2/com/google/zxing/core/3.2.0/core-3.2.0.jar
 

    pom.xml 文件依赖

	<!-- 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.0.0</version>  
     </dependency> 

 

   

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;
//java  实现二维码
public class Pro_QRcode {

	public static void main(String[] args) {
		int width = 300;
		int height = 300;
		String format = "png";
		String contents = "http://985571285.iteye.com/blog/2330432";

		//定义参数
		HashMap hints = new HashMap();
		//字符集
		hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
		//纠正等级分为:L(纠错等级%7)M(纠错等级%15)Q(纠错等级%25)H(纠错等级%30);
		hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);
		//外边距
		hints.put(EncodeHintType.MARGIN, 0);
		
		try { 
			BitMatrix matrix = 
			new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height,hints);
			//写出路径
			Path file = new File("D:/PIC.png").toPath();
			
			try {
				
				MatrixToImageWriter.writeToPath(matrix, format, file);
			} catch (IOException e) {
				
				e.printStackTrace();
			}
		} catch (WriterException e) {
			
			e.printStackTrace();
			
		}
	}
}

 二 、流输出

   

https://www.cnblogs.com/jston/archive/2013/01/30/2883460.html

 

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值