条形码是通过线条的粗细和线条的间隔所设计出来的识别码,
二维码在条形码的基础上设计的两个位面的识别码,能储存更多的信息,
二维码的种类很多,但是通常用的堆叠式的qrcode
------------
生成二维码的方式:
1.通过封装的js代码完成,
下载jquery.qrcode.min.js,使用 .qrcode("......................")方法生成二维码
2 使用Zxing jar 包,调用 MatrixToImageWriter.writeToPath()的方法
public class Zxing {
public static void main(String[] args) {
int width =300;
int height=300;
String format="png";
String content ="http://www.baidu.com";
Map 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, hints);
Path file=new File("C:/baidu.png").toPath();
MatrixToImageWriter.writeToPath(bitMatrix, format, file);
} catch (Exception e) {
System.out.println("二维码生成异常");
e.printStackTrace();
}
}
}
Zxingjar包附带解码工具,
public class ZxingDecode {
public static void main(String[] args) {
MultiFormatReader multiFormatReader=new MultiFormatReader();
try {
BinaryBitmap binaryBitma=new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(ImageIO.read(new File("C://baidu.png")))));
HashMap hints= new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
Result result=multiFormatReader.decode(binaryBitma, hints);
System.out.println(result.getText());
System.out.println(result.getBarcodeFormat());
} catch (Exception e) {
e.printStackTrace();
}
}
}
3使用qrcode的jar 包
生成二维码:
public class QrcodeTest {
public static void main(String[] args) throws IOException {
Qrcode x=new Qrcode();
x.setQrcodeErrorCorrect('M');
x.setQrcodeEncodeMode('B');
x.setQrcodeVersion(7);
int width=67+12*(7-1);
int height=67+12*(7-1);
String qrDate="http://www.baidu.com";
BufferedImage bufferedImage =new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D gs=bufferedImage.createGraphics();
gs.setBackground(Color.WHITE);
gs.setColor(Color.BLUE);
gs.clearRect(0, 0, width, height);
int pixoff =2;
byte[] d=qrDate.getBytes("gb2312");
if(d.length>0&&d.length<120){
boolean[][] s=x.calQrcode(d);
for(int i=0;i<s.length;i++){
for(int j=0;j<s.length;j++){
if(s[j][i]){
gs.fillRect(j*3+pixoff, i*3+pixoff, 3, 3);
}
}
}
}
gs.dispose();
bufferedImage.flush();
ImageIO.write(bufferedImage, "png", new File("C://baidu.png"));
}
}
解码方式
public class QrcodeDecode {
public static void main(String[] args) throws IOException {
File file =new File("C:/imocc.png");
BufferedImage bufferedImage =ImageIO.read(file);
QRCodeDecoder codeDecoder=new QRCodeDecoder();
String st=new String(codeDecoder.decode
(new MyQrCodeImageHelper(bufferedImage)), "gb2312");
System.out.println(st);
}
}
解码的代码里面有一个MyQrCodeImageHelper类的对象,这个需要自己去定义的
它需要实现QRCodeImage接口,重写颜色,宽,高,三种方法
public class MyQrCodeImageHelper implements QRCodeImage{
BufferedImage bufferedImage;
public MyQrCodeImageHelper(BufferedImage bufferedImage) {
super();
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();
}
}