java代码生成二维码

java两种方式生成二维码
一种通过zxing生成与解析只引入zxing3.2.1.jar
一种引入qrcode.jar

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.swetake.util.Qrcode;

import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.data.QRCodeImage;

class QTest implements QRCodeImage {

    BufferedImage bufferedImage;

    public QTest(BufferedImage bufferedImage) {
        this.bufferedImage = bufferedImage;
    }

    @Override
    public int getHeight() {
        return bufferedImage.getHeight();
    }

    @Override
    public int getPixel(int arg0, int arg1) {
        return bufferedImage.getRGB(arg0, arg1);
    }

    @Override
    public int getWidth() {
        return bufferedImage.getWidth();
    }
}

public class GenerateQR_code {
    /**
     * 设置纠错能力,L级7%,M级15%,Q级25%,H级30%
     */
    private final static ErrorCorrectionLevel L = ErrorCorrectionLevel.L;
    private final static ErrorCorrectionLevel M = ErrorCorrectionLevel.M;
    private final static ErrorCorrectionLevel Q = ErrorCorrectionLevel.Q;
    private final static ErrorCorrectionLevel H = ErrorCorrectionLevel.H;

    public static void main(String[] args) {
        String content=GenerateQR_code.createMP("zjf", "boss10086", "facebook", "132465", "458979", "321565", "http://www.baidu.com", "1107@qq.com", "1107");
            try {
                GenerateQR_code.createByZxing("C:\\Users\\Administrator\\Desktop\\test.png", content);
            } catch (WriterException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

    /**
     * 传参文件全路径与内容
     * 
     * @param filepath
     * @param content
     * @throws WriterException
     * @throws IOException
     */
    public static void createByZxing(String filepath, String content) throws WriterException, IOException {
        createByZxing(filepath, content, "UTF-8", "png", 300, 300, 1, M);
    }

    /**
     * 传参文件全路径与内容
     * 
     * @param filepath
     * @param content
     * @throws IOException
     */
    public static void createByQRcode(String filepath, String content) throws IOException {
        // 纠错能力,L级7%,M级15%,Q级25%,H级30%
        // 内容,N代表数字,A代表a-z,B代表其他字符
        createByQRcode(filepath, content, 7, 'M', 'B', Color.WHITE, Color.BLACK, "png");
    }

    /**
     * 生成二维码名片格式的字符串
     * 
     * @param name 名字
     * @param nickname 昵称
     * @param orgname 组织
     * @param homephone 家庭电话
     * @param workphone 工作电话
     * @param phone 电话
     * @param url url
     * @param email 邮件
     * @param qq QQ
     * @return
     */
    public static String createMP(String name, String nickname, String orgname, String homephone, String workphone,
            String phone, String url, String email, String qq) {
        String str = "BEGIN:VCARD\n" + "VERSION:2.1\n" + "N:" + name + "\n" + "FN:" + "\n" + "NICKNAME:" + nickname
                + "\n" + "ORG:" + orgname + "\n" + "TITLE:" + "\n" + "TEL;WORK;VOICE:PHONE1\n"
                + "TEL;WORK;VOICE:PHONE2\n" + "TEL;HOME;VOICE:" + homephone + "\n" + "TEL;WORK;VOICE:PHONE4\n"
                + "TEL;WORK;VOICE:" + workphone + "\n" + "TEL;PAGER;VOICE:" + phone + "\n" + "TEL;WORK;FAX:PHONE7\n"
                + "TEL;HOME;FAX:PHONE8\n" + "ADR;WORK:;;\n" + "ADR;HOME;POSTAL;PARCEL:;;\n" + "URL:" + url + "\n"
                + "URL:\n" + "EMAIL;PREF;INTERNET:" + email + "\n" + "X-QQ:" + qq + "\n" + "END:VCARD";
        return str;
    }

    /**
     * 传参文件全路径与字符集 解析二维码
     * 
     * @throws IOException
     */
    public static String parseByQRCode(String filepath, String charset) throws IOException {
        File file = new File(filepath);
        BufferedImage bufferedImage = ImageIO.read(file);
        QRCodeDecoder codeDecoder = new QRCodeDecoder();
        byte[] s = codeDecoder.decode(new QTest(bufferedImage));
        String str = new String(s, charset);
        return str;
    }

    /**
     * 传参文件全路径与字符集 解析二维码
     * 
     * @throws NotFoundException
     * @throws IOException
     */
    public static void parseByZxing(String filepath, String charset) throws NotFoundException, IOException {
        MultiFormatReader formatReader = new MultiFormatReader();
        File file = new File(filepath);
        BufferedImage image = ImageIO.read(file);
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
        // 参数
        Map map = new HashMap();
        // 设置字符集
        map.put(EncodeHintType.CHARACTER_SET, charset);
        Result result = formatReader.decode(binaryBitmap, map);
        System.out.println(result.getText());
        System.out.println(result.getText());
        System.out.println(result.getBarcodeFormat());

    }

    /**
     * 传参
     * 
     * @param filepath
     *            文件全路径
     * @param content
     *            内容
     * @param charset
     *            字符集
     * @param suffix
     *            文件后缀
     * @param width
     *            宽
     * @param height
     *            高
     * @param padding
     *            编剧
     * @param e
     *            纠错等级
     * @throws WriterException
     * @throws IOException
     */
    public static void createByZxing(String filepath, String content, String charset, String suffix, int width, int height,
            int padding, ErrorCorrectionLevel e) throws WriterException, IOException {
        // 参数
        HashMap<EncodeHintType, Object> map = new HashMap<EncodeHintType, Object>();
        // 设置字符集
        map.put(EncodeHintType.CHARACTER_SET, charset);
        // 设置纠错能力,L级7%,M级15%,Q级25%,H级30%
        // 纠错能力越强存储内容越少,清晰度要求越低
        map.put(EncodeHintType.ERROR_CORRECTION, e);
        // 边距默认5
        map.put(EncodeHintType.MARGIN, padding);

        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, map);
        Path file = new File(filepath).toPath();
        MatrixToImageWriter.writeToPath(bitMatrix, suffix, file);
    }

    /**
     * 传参
     * 
     * @param filepath
     *            文件全路径
     * @param content
     *            内容
     * @param version
     *            版本
     * @param e
     *            纠错等级
     * @param c
     *            字符
     * @param backgroundColor
     *            背景颜色
     * @param strColor
     *            内容颜色
     * @param suffix
     *            后缀
     * @throws IOException
     */
    public static void createByQRcode(String filepath, String content, int version, char e, char c, Color backgroundColor,
            Color strColor, String suffix) throws IOException {
        Qrcode x = new Qrcode();
        // 纠错等级
        x.setQrcodeErrorCorrect(e);
        // 内容,N代表数字,A代表a-z,B代表其他字符
        x.setQrcodeEncodeMode(c);
        // 版本
        x.setQrcodeVersion(version);
        int width = 67 + 12 * (version - 1);
        int height = 67 + 12 * (version - 1);
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 创建画图工具
        Graphics2D gs = bufferedImage.createGraphics();
        // 设置背景颜色
        gs.setBackground(backgroundColor);
        // 设置内容颜色
        gs.setColor(strColor);
        // 清除画板内容
        gs.clearRect(0, 0, width, height);

        // 偏移量不加解析可能出错
        int pixoff = 2;
        // 向画板内添加数据
        byte[] d = content.getBytes();
        if (d.length > 0 && d.length < 120) {
            boolean[][] s = x.calQrcode(d);
            for (int i = 0; i < s.length; i++) {
                for (int j = 0; j < s.length; j++) {
                    if (s[j][i]) {
                        gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
                    }
                }
            }
        }
        gs.dispose();
        bufferedImage.flush();
        ImageIO.write(bufferedImage, suffix, new File(filepath));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值