当前位置 : 主页 > 编程语言 > java >

二维码生成工具

来源:互联网 收集:自由互联 发布时间:2021-06-28
gistfile1.txt import com.google.zxing.*;import com.google.zxing.common.BitMatrix;import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;import org.apache.commons.codec.binary.Base64;import org.apache.commons.codec.binary.Base64OutputSt
gistfile1.txt
import com.google.zxing.*;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Base64OutputStream;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class QRCodeUtils {

    public static final String QRCODE_DEFAULT_CHARSET = "UTF-8";

    public static final int QRCODE_DEFAULT_HEIGHT = 220;

    public static final int QRCODE_DEFAULT_WIDTH = 220;

    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;

    public static void main(String[] args) throws IOException, NotFoundException {
        String data = "http://www.baidu.com";
        File logoFile = new File("E:\\1.jpg");
        BufferedImage image = QRCodeUtils.createQRCodeWithLogo(data, logoFile);
        ImageIO.write(image, "png", new File("E:\\test.png"));
        System.out.println("done");
    }

    /**
     * Create qrcode with default settings
     *
     * @param data
     * @return
     * @author stefli
     */
    public static BufferedImage createQRCode(String data) {
        return createQRCode(data, QRCODE_DEFAULT_WIDTH, QRCODE_DEFAULT_HEIGHT);
    }

    /**
     * Create qrcode with default charset
     *
     * @param data
     * @param width
     * @param height
     * @return
     * @author stefli
     */
    public static BufferedImage createQRCode(String data, int width, int height) {
        return createQRCode(data, QRCODE_DEFAULT_CHARSET, width, height);
    }

    /**
     * Create qrcode with specified charset
     *
     * @param data
     * @param charset
     * @param width
     * @param height
     * @return
     * @author stefli
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    public static BufferedImage createQRCode(String data, String charset, int width, int height) {
        Map hint = new HashMap();
        hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hint.put(EncodeHintType.CHARACTER_SET, charset);

        return createQRCode(data, charset, hint, width, height);
    }

    /**
     * Create qrcode with specified hint
     *
     * @param data
     * @param charset
     * @param hint
     * @param width
     * @param height
     * @return
     * @author stefli
     */
    public static BufferedImage createQRCode(String data, String charset, Map
 
   hint, int width,
                                             int height) {
        BitMatrix matrix;
        try {
            matrix = new MultiFormatWriter().encode(new String(data.getBytes(charset), charset), BarcodeFormat.QR_CODE,
                    width, height, hint);
            return toBufferedImage(matrix);
        } catch (WriterException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    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;
    }

    /**
     * Create qrcode with default settings and logo
     *
     * @param data
     * @param logoFile
     * @return
     * @author stefli
     */
    public static BufferedImage createQRCodeWithLogo(String data, File logoFile) {
        return createQRCodeWithLogo(data, QRCODE_DEFAULT_WIDTH, QRCODE_DEFAULT_HEIGHT, logoFile);
    }

    /**
     * Create qrcode with default charset and logo
     *
     * @param data
     * @param width
     * @param height
     * @param logoFile
     * @return
     * @author stefli
     */
    public static BufferedImage createQRCodeWithLogo(String data, int width, int height, File logoFile) {
        return createQRCodeWithLogo(data, QRCODE_DEFAULT_CHARSET, width, height, logoFile);
    }

    /**
     * Create qrcode with specified charset and logo
     *
     * @param data
     * @param charset
     * @param width
     * @param height
     * @param logoFile
     * @return
     * @author stefli
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    public static BufferedImage createQRCodeWithLogo(String data, String charset, int width, int height, File logoFile) {
        Map hint = new HashMap();
        hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hint.put(EncodeHintType.CHARACTER_SET, charset);

        return createQRCodeWithLogo(data, charset, hint, width, height, logoFile);
    }

    /**
     * Create qrcode with specified hint and logo
     *
     * @param data
     * @param charset
     * @param hint
     * @param width
     * @param height
     * @param logoFile
     * @return
     * @author stefli
     */
    public static BufferedImage createQRCodeWithLogo(String data, String charset, Map
  
    hint, int width, int height, File logoFile) { try { BufferedImage qrcode = createQRCode(data, charset, hint, width, height); BufferedImage logo = ImageIO.read(logoFile); /** * 设置logo的大小,设置为二维码图片的20%,因为过大会盖掉二维码 */ int widthLogo = logo.getWidth(null) > qrcode.getWidth() * 1 / 5 ? (qrcode.getWidth() * 1 / 5) : logo.getWidth(null); int heightLogo = logo.getHeight(null) > qrcode.getHeight() * 1 / 5 ? (qrcode.getHeight() * 1 / 5) : logo.getWidth(null); /** * logo放在中心 */ int x = (qrcode.getWidth() - widthLogo) / 2; int y = (qrcode.getHeight() - heightLogo) / 2; //开始绘制图片 Graphics2D g = qrcode.createGraphics(); g.drawImage(logo, x, y, widthLogo, heightLogo, null); g.dispose(); return qrcode; } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } } /** * Return base64 for image * * @param image * @return * @author stefli */ public static String getImageBase64String(BufferedImage image) { String result = null; try { ByteArrayOutputStream os = new ByteArrayOutputStream(); OutputStream b64 = new Base64OutputStream(os); ImageIO.write(image, "png", b64); result = os.toString("UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e.getMessage(), e); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } return result; } /** * Decode the base64Image data to image * * @param base64ImageString * @param file * @author stefli */ public static void convertBase64StringToImage(String base64ImageString, File file) { FileOutputStream os; try { Base64 d = new Base64(); byte[] bs = d.decode(base64ImageString); os = new FileOutputStream(file.getAbsolutePath()); os.write(bs); os.close(); } catch (FileNotFoundException e) { throw new RuntimeException(e.getMessage(), e); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } } }
  
 
网友评论