java

来源:互联网 发布:千元电视机推荐 知乎 编辑:程序博客网 时间:2024/06/03 21:26

1.QRCode

package com.qr.code;

import com.swetake.util.Qrcode;import jp.sourceforge.qrcode.QRCodeDecoder;import jp.sourceforge.qrcode.data.QRCodeImage;import jp.sourceforge.qrcode.exception.DecodingFailedException;import javax.imageio.ImageIO;import java.awt.*;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;/** * Created by static on 2017/4/24. * 二维码生成器 */public class QRCode {    private static final String  CODE_FORMAT = "utf-8";    /**     * 生成二维码     * @param contents  描述文字     * @param imgPath 生成图片路径     * @param logoImg 二维码图片logo     * @throws Exception     */    public static void generateQRCodeImg(String contents,String imgPath,String logoImg) throws Exception {        int width = 140,height = 140;        try {            Qrcode qrcode = new Qrcode();            qrcode.setQrcodeErrorCorrect(FaultTolerant.M.getCode());            qrcode.setQrcodeEncodeMode('B');            qrcode.setQrcodeVersion(7);            BufferedImage bufImg = new BufferedImage(width, height,BufferedImage.TYPE_INT_BGR);            Graphics2D gs = bufImg.createGraphics();            gs.setBackground(Color.WHITE);            gs.clearRect(0, 0, width, height);            gs.setColor(Color.BLACK);            int pixOff = 2;//偏移量            byte[] contentBytes = contents.getBytes(CODE_FORMAT);            if (contentBytes.length != 0 && contentBytes.length < 120) {                boolean[][] codeOut = qrcode.calQrcode(contentBytes);                for (int i = 0; i < codeOut.length; i++) {                    for (int j = 0; j < codeOut.length; j++) {                        if (codeOut[j][i]) {                            gs.fillRect(j * 3 + pixOff, i * 3 + pixOff, 3, 3);                        }                    }                }            } else {                throw new Exception("文字内容大小超出限制;(限制小于120)");            }            Image img = null;            if(logoImg!=null&&logoImg!=""&&logoImg.trim().length()>0){                img = ImageIO.read(new File(logoImg));            }            gs.drawImage(img, 45, 45, 50,50, null);            gs.dispose();            bufImg.flush();            File imgFile = new File(imgPath);            String[] postfix = imgPath.split("\\.");            ImageIO.write(bufImg,postfix[1], imgFile);        }catch (Exception ex){            throw new Exception("生成异常:",ex);        }    }    /**     * 二维码读取     * @param imgPath 图片路径     */    public static void readQRCodeImg(String imgPath){        File imageFile = new File(imgPath);        BufferedImage bufImg;        String decodedData;        try {            bufImg = ImageIO.read(imageFile);            QRCodeDecoder decoder = new QRCodeDecoder();            decodedData = new String(decoder.decode(new QRImage(bufImg)), CODE_FORMAT);            System.out.println(decodedData);        } catch (IOException e) {            System.out.println("读取异常: " + e.getMessage());            e.printStackTrace();        } catch (DecodingFailedException dfe) {            System.out.println("解析异常: " + dfe.getMessage());            dfe.printStackTrace();        }    }    public static class QRImage implements QRCodeImage {        BufferedImage bufImg;        public QRImage(BufferedImage bufImg) {            this.bufImg = bufImg;        }        public int getWidth() {            return bufImg.getWidth();        }        public int getHeight() {            return bufImg.getHeight();        }        public int getPixel(int x, int y) {            return bufImg.getRGB(x, y);        }    }    public static void main(String [] args) throws Exception {        String contents = "这是一个二维码生成器";        String imgPath = "2.png";        String logoImg = "logo.png";        QRCode.generateQRCodeImg(contents,imgPath,logoImg);        QRCode.readQRCodeImg(imgPath);    }}

2.FaultTolerant

package com.qr.code;/** * Created by static on 2017/4/24. * 排错率 * 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%), * 排错率越高可存储的信息越少,但对二维码清晰度的要求越小 */public enum FaultTolerant {    L('L',"7%"),    M('M',"15%"),    Q('Q',"25%"),    H('H',"30%");    private FaultTolerant(char code, String describe) {        this.code = code;        this.describe = describe;    }    private char code;    private String describe;    public char getCode() {        return code;    }    public void setCode(char code) {        this.code = code;    }    public String getDescribe() {        return describe;    }    public void setDescribe(String describe) {        this.describe = describe;    }}



3.运行截图











0 0
原创粉丝点击