码迷,mamicode.com
首页 > 编程语言 > 详细

使用java解析和制作二维码

时间:2014-07-11 21:23:42      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   java   color   使用   

 

项目结构

bubuko.com,布布扣

文件源码 QR.zip

第一步:导入zxing的两个架包 core.jarjavase.jar

第二步:使用工具类 MatrixToImageWriter.java

 1 package util;
 2 import com.google.zxing.common.BitMatrix;
 3 import javax.imageio.ImageIO;
 4 import java.io.File;
 5 import java.io.OutputStream;
 6 import java.io.IOException;
 7 import java.awt.image.BufferedImage;
 8 public final class MatrixToImageWriter {
 9     private static final int BLACK = 0xFF000000;
10     private static final int WHITE = 0xFFFFFFFF;
11     private MatrixToImageWriter() {
12     }
13     public static BufferedImage toBufferedImage(BitMatrix matrix) {
14         int width = matrix.getWidth();
15         int height = matrix.getHeight();
16         BufferedImage image = new BufferedImage(width, height,
17                 BufferedImage.TYPE_INT_RGB);
18         for (int x = 0; x < width; x++) {
19             for (int y = 0; y < height; y++) {
20                 image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
21             }
22         }
23         return image;
24     }
25     public static void writeToFile(BitMatrix matrix, String format, File file)
26             throws IOException {
27         BufferedImage image = toBufferedImage(matrix);
28         if (!ImageIO.write(image, format, file)) {
29             throw new IOException("Could not write an image of format "
30                     + format + " to " + file);
31         }
32     }
33     public static void writeToStream(BitMatrix matrix, String format,
34             OutputStream stream) throws IOException {
35         BufferedImage image = toBufferedImage(matrix);
36         if (!ImageIO.write(image, format, stream)) {
37             throw new IOException("Could not write an image of format "
38                     + format);
39         }
40     }
41 }

第三步:创建二维码

package demo;
import java.io.File;
import java.util.Hashtable;
import util.MatrixToImageWriter;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
/**
 *将文字转换为二维码
 */
public class Encoder {
    public static void main(String[] args) throws Exception {
        String text = "http://www.tfgzs.com";
        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
        // 内容所使用编码
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(text,BarcodeFormat.QR_CODE, 300, 300, hints);
        // 生成二维码
        MatrixToImageWriter.writeToFile(bitMatrix, "gif", new File("E:/123/new.gif"));
        System.out.println("二维码生成完成");
    }
}

第四步:解析二维码

package demo;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
/**
 * 将二维码解析为文字
 */
public class Decoder {
    public static void main(String[] args) {
        BufferedImage bufferedImage = null;
        try {
            bufferedImage = ImageIO.read(new File("E://123/qrcodeImage.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
        hints.put(DecodeHintType.CHARACTER_SET, "GBK");
        Result result = null;
        try {
            result = new MultiFormatReader().decode(bitmap, hints);
        } catch (NotFoundException e) {
            e.printStackTrace();
        }
        System.out.println(result.toString());
    }
}

使用java解析和制作二维码,布布扣,bubuko.com

使用java解析和制作二维码

标签:style   blog   http   java   color   使用   

原文地址:http://www.cnblogs.com/tfgzs/p/3833269.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!