package com.wk.qrcode;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import jp.sourceforge.qrcode.QRCodeDecoder;
public class ReadQRCode {
public static void main(String[] args) throws IOException {
File file = new File("D://img.png");
BufferedImage bufferedImage = ImageIO.read(file);
QRCodeDecoder codeDecoder = new QRCodeDecoder();
String result = new String(codeDecoder.decode(new MyQRCodeImage(bufferedImage)));
System.out.println(result);
}
}
package com.wk.qrcode;
import java.awt.image.BufferedImage;
import jp.sourceforge.qrcode.data.QRCodeImage;
public class MyQRCodeImage implements QRCodeImage {
BufferedImage bufferedImage;
public MyQRCodeImage(BufferedImage bufferedImage) {
this.bufferedImage = bufferedImage;
}
@Override
public int getHeight() {
return bufferedImage.getHeight();
}
@Override
public int getPixel(int arg0, int arg1) {
return bufferedImage.getRGB(arg0, arg1);
}
@Override
public int getWidth() {
return bufferedImage.getWidth();
}
}
结果打印出来是:
19628671028299113646982254461625629964540660043172309990720326327708969541999728475429321813549061834182907580805898539388735496775103106854592411992889663010198072995495752784186816116372008403787635790855390000000000000000000000000000000000000000000000000000000000000000000000
不是编码的问题,还是生成的图片有问题,大家看那个双重for循环gs.fillRect方法,大家如果i在外层循环,j在内层循环(个人习惯),应该gs.fillRect(i*3+pixoff,j*3+pixoff, 3, 3);,而不是gs.fillRect(j*3+pixoff,i*3+pixoff, 3, 3);j和i的位置问题,我生成的图片扫描出来www.imooc.com,但是解析确实一串数字,发现i和j写反了
同问,我也没解决
String result = new String(codeDecoder.decode(new MyQRCodeImage(bufferedImage)));
将这句代码修改为
String result = new String(codeDecoder.decode(new MyQRCodeImage(bufferedImage)),"gb2312");
解析出来的内容没有设置编码格式