您的位置:首页 > 其它

qr code的使用

2015-01-26 12:03 190 查看
使用google的zxing包

Help:

package com.qrcode.test;

import com.google.zxing.common.BitMatrix; 

    

 import javax.imageio.ImageIO; 

 import java.io.File; 

 import java.io.OutputStream; 

 import java.io.IOException; 

 import java.awt.image.BufferedImage; 

    

    

 public final class Help { 

    

   private static final int BLACK = 0xFF000000; 

   private static final int WHITE = 0xFFFFFFFF; 

    

   private Help() {} 

    

      

   public static BufferedImage toBufferedImage(BitMatrix matrix) { 

     int width = matrix.getWidth(); 

     int height = matrix.getHeight(); 

     BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 

     for (int x = 0; x < width; x++) { 

       for (int y = 0; y < height; y++) { 

         image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE); 

       } 

     } 

     return image; 

   } 

    

      

   public static void writeToFile(BitMatrix matrix, String format, File file) 

       throws IOException { 

     BufferedImage image = toBufferedImage(matrix); 

     if (!ImageIO.write(image, format, file)) { 

       throw new IOException("Could not write an image of format " + format + " to " + file); 

     } 

   } 

    

      

   public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) 

       throws IOException { 

     BufferedImage image = toBufferedImage(matrix); 

     if (!ImageIO.write(image, format, stream)) { 

       throw new IOException("Could not write an image of format " + format); 

     } 

   } 

    

 }

QRcode:

package com.qrcode.test;

import com.google.zxing.common.BitMatrix;

import com.google.zxing.qrcode.QRCodeWriter;

import com.google.zxing.BarcodeFormat;

import com.qrcode.test.Help;

import java.io.*;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import javax.imageio.*;

public class QRTest extends JFrame{
String content="http://www.baidu.com?中国";
JLabel label1=new JLabel("QR coding");
JTextField text1=new JTextField();
public void init(){
this.setSize(400,380);
this.setLayout(null);

JButton button1=new JButton("encode");
text1.setBounds(10,10,370,30);
button1.setBounds(300,50,80,30);

this.getContentPane().add(text1);
this.getContentPane().add(button1);

label1.setBounds(80,100,200,200);
this.getContentPane().add(label1);
this.setVisible(true);
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
content=text1.getText();
if(content=="" || content.equals("")){
text1.setText("Please enter your string...");
text1.requestFocus();
}
encode();
ImageIcon image1=new ImageIcon("c:/qr.png");
image1.getImage().flush();
label1.setIcon(image1);
System.out.println(content);
}
catch(Exception e1){}
}
});

}
public void encode() throws Exception{
content=new String(content.getBytes("GBK"),"ISO-8859-1");
String imagePath="c:\\qr.png";
File f1=new File(imagePath);
QRCodeWriter write=new QRCodeWriter();
BitMatrix matrix=write.encode(content,BarcodeFormat.QR_CODE,200,200);
Help.writeToFile(matrix,"png",f1);
}
public static void main(String args[]) throws Exception{
new QRTest().init();
}

}

然后用myeclipse生成可执行的jar包
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  qrcode qr