使用zxing生成二维码的问题

soulhunters 2017-09-26 12:16:09
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 CreatQRCode {
public static void main(String[] args){
int width=300;
int height=300;
String format="png";
String content="www.imooc.com";

HashMap hints= new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hints.put(EncodeHintType.MARGIN,2);

try {
BitMatrix bitMatrix =new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);
Path file=new File("D:/MyEclipse/code/img.png").toPath();
MatrixToImageWriter.writeToPath(bitMatrix, format, file);

} catch (Exception e) {

e.printStackTrace();
}
}
}
错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Type mismatch: cannot convert from ArrayList<?> to Collection<BlockPair>
Cannot instantiate the type ArrayList<?>
Syntax error on token "<", ? expected after this token

at com.google.zxing.qrcode.encoder.Encoder.interleaveWithECBytes(Encoder.java:406)
at com.google.zxing.qrcode.encoder.Encoder.encode(Encoder.java:136)
at com.google.zxing.qrcode.QRCodeWriter.encode(QRCodeWriter.java:78)
at com.google.zxing.MultiFormatWriter.encode(MultiFormatWriter.java:102)
at com.google.zxing.MultiFormatWriter.encode(MultiFormatWriter.java:49)
at com.zxing.CreatQRCode.main(CreatQRCode.java:28)
不知道怎么解决。jdk是1.7的。引入的zxing包也是用jdk1.7打包的。
MyEclipse编译。
...全文
575 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
soulhunters 2017-09-26
  • 打赏
  • 举报
回复
是在慕课网上照着打得。本人小白。求各位大神指教啊,基本方法都用过都没解决掉这个,估计是类型,泛型的问题
soulhunters 2017-09-26
  • 打赏
  • 举报
回复
BitMatrix bitMatrix =new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hints); 原来我是漏了一个hints参数 但现在代码已经没有报错了。但是编译还是出错 Exception in thread "main" java.lang.Error: Unresolved compilation problem: at com.google.zxing.common.CharacterSetECI.getCharacterSetECIByName(CharacterSetECI.java:114) at com.google.zxing.qrcode.encoder.Encoder.encode(Encoder.java:95) at com.google.zxing.qrcode.QRCodeWriter.encode(QRCodeWriter.java:78) at com.google.zxing.MultiFormatWriter.encode(MultiFormatWriter.java:102) at com.zxing.CreatQRCode.main(CreatQRCode.java:30) 求解决啊。谢谢
qq_32579657 2017-09-26
  • 打赏
  • 举报
回复
import com.google.zxing.client.j2se.MatrixToImageWriter的方法writeToPath 在将ArrayList<?>转换为Collection<BlockPair>失败

51,410

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧