zoukankan      html  css  js  c++  java
  • 使用java解析和制作二维码

    项目结构

    文件源码 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());
        }
    }
  • 相关阅读:
    在ASP.Net和IIS中删除不必要的HTTP响应头
    java合并多个word 2007 文档 基于docx4j
    [转]怎样与 CORS 和 cookie 打交道
    css 设置div半透明 悬浮在页面底部 不随滚动条滚动
    [转]理解Vuex的辅助函数mapState, mapActions, mapMutations用法
    [转]Vue中用props给data赋初始值遇到的问题解决
    [转]import xxx from 和 import {xxx} from的区别
    [转]详解vue父组件传递props异步数据到子组件的问题
    [转]js判断数据类型的四种方法
    [转]iview的render函数用法
  • 原文地址:https://www.cnblogs.com/tfgzs/p/3833269.html
Copyright © 2011-2022 走看看