二维码简单生成

   最近看了一些有关生成二维码的帖子,觉得挺好玩了,自己弄了一下,但是不知道是怎么回事。到我这里总是出错,可能是我的软件包有问题吧,于是自己修改了一下,看看有没有和我一样的朋友也遇到这样的问题:Type mismatch: cannot convert from ByteMatrix to BitMatrix;
也就是

MultiFormatWriter  不能转化  BitMatrix  于是 我便做出了一些改动,下面是我的代码:

package Test;

import java.io.File;
import java.util.Hashtable;

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.ByteMatrix;

public class TestCode {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String path = "E:/ElmarSun.jpg";
        String str = "枫叶~:http://blog.csdn.net/sunlitao100/article/details/24976295";
        String format = "jpg" ;
        int width = 300 ;
        int height = 300;
        TestCode tc = new TestCode();
        tc.Generate(str, width, height, path, format) ;

	}
	// str :二维码中的内容
	// width/height : 尺寸
	// path : 生成路径
	// format : 生成图片格式
	public void Generate(String str,int width,int height,String path,String format){
		Hashtable<EncodeHintType, Object> hashTable = new Hashtable<EncodeHintType, Object>();
		hashTable.put(EncodeHintType.CHARACTER_SET, "utf-8"); //设置编码格式为UTF-8
		try {
			//要设置的参数
			ByteMatrix bm = new MultiFormatWriter().encode(str,BarcodeFormat.QR_CODE,width,height,hashTable);
			File outputFile = new File(path); //文件输出
			MatrixToImageWriter.writeToFile(bm, format, outputFile); //生成二维码图片
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
jar包下载地址: http://jarfiles.pandaidea.com/zxing.html
希望对  和我遇到一样问题的朋友有帮助。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值