最近看了一些有关生成二维码的帖子,觉得挺好玩了,自己弄了一下,但是不知道是怎么回事。到我这里总是出错,可能是我的软件包有问题吧,于是自己修改了一下,看看有没有和我一样的朋友也遇到这样的问题: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
希望对 和我遇到一样问题的朋友有帮助。