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包
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包
相关文章推荐
- 使用PHP QR Code生成二维码
- php生成二维码的类库:PHP QR Code下载及使用方法
- 使用PHP二维码生成类库PHP QR Code生成二维码
- QR code二维码简介及Qrencode库的移植与使用
- 使用PHP QR Code生成二维码
- java实现二维码-使用QR Code方式生成和解析二维码
- 使用PHP QR Code生成二维码
- 使用PHP QR Code生成二维码
- 使用PHP QR Code生成二维码
- 一个非常不错的 php QR Code 类库 使用类
- 使用PHP QR Code生成二维码
- 使用PHP QR Code生成二维码
- 阐述二维码的原理以及使用google api和PHP QR Code来生成二维码
- 使用PHP QR Code生成二维码事例应用
- 阐述二维码的原理以及使用google api和PHP QR Code来生成二维码
- 阐述二维码的原理以及使用google api和PHP QR Code来生成二维码
- 使用PHP QR Code生成二维码
- 使用PHP QR Code生成二维码
- 我写的一个Qt 显示二维码( QR Code)的控件(可以去掉对 libpthread 的依赖,而且编译出的库文件可以在 vc2010 的release 模式下使用)
- 使用ZXing.Net生成与识别二维码(QR Code)