zoukankan      html  css  js  c++  java
  • java-二维码编写zxing

    zxing 这个是google的 
    下载地址 
    http://code.google.com/p/zxing/downloads/list

    二维码源码案例

    package com.utils;
    
    import java.awt.image.BufferedImage;
    import java.awt.image.DataBufferByte;
    import java.io.File;
    import java.io.IOException;
    import java.util.Date;
    import java.util.Hashtable;
    
    import javax.imageio.ImageIO;
    
    import com.google.zxing.BarcodeFormat;
    import com.google.zxing.EncodeHintType;
    import com.google.zxing.MultiFormatWriter;
    import com.google.zxing.common.BitMatrix;
    
    public class UtilsQrCode {
        
        private static final int BLACK = 0xff000000;
        private static final int WHITE = 0xFFFFFFFF;
    
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            File file = new File("C:\Users\huage\Desktop\图片\"+new Date().getTime()+".png");
            encode("http://www.baidu.com", file, BarcodeFormat.QR_CODE, 200, 200);
        }
        
        /**
         * @param contents
         * @param file
         * @param format
         * @param width
         * @param height
         */
        public static  void encode(String contents, File file, BarcodeFormat format, int width, int height) {
            try {
                contents = new String(contents.getBytes("UTF-8"), "ISO-8859-1"); 
                Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
                hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
                hints.put(EncodeHintType.MARGIN, 1);
                BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, format, width, height,hints);
                BufferedImage image = toBufferedImage(bitMatrix);
                writeToFile(image, "png", file);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        
        public static byte[] imgToBytes(BufferedImage image){
            if( image == null ) return new byte[0];
            byte[] data = ((DataBufferByte) image.getData().getDataBuffer()).getData();
            return data;
        }
        
    
        public static void writeToFile(BufferedImage image, String format, File file) throws IOException {
            if( image == null || file == null ) return;
            ImageIO.write(image, format, file);
        }
    
        
        public static BufferedImage toBufferedImage(BitMatrix matrix) {
            int width = matrix.getWidth();
            int height = matrix.getHeight();
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    image.setRGB(x, y, matrix.get(x, y) == true ? BLACK : WHITE);
                }
            }
            return image;
        }
    
    
    
    
    }
  • 相关阅读:
    WebStorm2019
    微信公众号互阅平台-真实提高阅读量-「作者加鸡腿」
    macos 致命错误: 在类路径或引导类路径中找不到程序包 java.lang
    IDEA2020激活码 / IDEA 2020.1.2激活破解教程
    Linux命令大全
    2019年终总结-2020展望「定版」
    SpringBoot如何切换Redis默认库
    uniapp增加百度统计代码(h5)
    修改MyEclipse/Eclipse左侧文字大小(MacOS/Windows)
    Invalid connection string format, a valid format is: "host:port:sid"
  • 原文地址:https://www.cnblogs.com/hwaggLee/p/5207250.html
Copyright © 2011-2022 走看看