Java產生二維碼

來源:互聯網
上載者:User

標籤:nbsp   buffered   ogg   AC   sse   correct   failed   第三方   背景色   

前言: 這周有個項目需要產生二維碼,研究了一下使用Google的zxing產生二維碼,發現效果還可以,在這裡記錄下。如果需要更加定製化的二維碼,也可接通第三方API服務產生二維碼。

 

二維碼的產生 : 

@Component("qrcodeHandler")public class QrcodeHandlerImpl implements QrcodeHandler {        private static final Logger logger = LoggerFactory.getLogger(QrcodeHandlerImpl.class);    /** 二維碼顏色 */    private static final int FRONT_COLOR = 0xFF000000; // 前景色彩黑色    private static final int BACK_COLOR = 0xFFFFFFFF;  // 背景色白色        /** 編碼格式 */    private static final String CHARACTER_SET = "UTF-8";        /** 圖片類型 */    private static final String FORMAT = "png";        /** 二維碼尺寸 */    private static final int QRCODE_SIZE = 300;        /** logo資訊 */    private static final int LOGO_SIZE = 60;        private static final HashMap<EncodeHintType, Object> hints;         static {        hints = new HashMap<EncodeHintType, Object>();        hints.put(EncodeHintType.CHARACTER_SET, CHARACTER_SET);        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);        hints.put(EncodeHintType.MARGIN, 1);    }        /**     * 產生簡單的二維碼     *      * @param contents  二維碼實際內容,包括網址、文本、檔案等, 如http://www.cnblogs.com/ark-blog/     * @param destFile  產生的二維碼檔案     */    public void generateSimpleQrcode(String contents, File destFile) {                try {            // 1. 產生位矩陣            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);                        // 2. 儲存圖片            MatrixToImageWriter.writeToPath(bitMatrix, FORMAT, destFile.toPath());                    } catch (Exception e) {            logger.error("#generateSimpleQrcode() - create qrcode failed, exception=[{}]", e);            throw BusinessException.newInstance("500", "create qrcode failed");        }            }    /**     * 產生帶logo的二維碼     *      * @param contents  二維碼實際內容,包括網址、文本、檔案等, 如http://www.cnblogs.com/ark-blog/     * @param logoFile  logo     * @param destFile  產生的二維碼檔案     */    public void generateLogoQrcode(String contents, File logoFile, File destFile) {                try {            // 1. 建立位矩陣圖            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();            BitMatrix bitMatrix = multiFormatWriter.encode(contents, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);            BufferedImage qrcodeImage = new BufferedImage(QRCODE_SIZE, QRCODE_SIZE, BufferedImage.TYPE_INT_RGB);            for (int x = 0; x < QRCODE_SIZE; x++) {                for (int y = 0; y < QRCODE_SIZE; y++) {                    qrcodeImage.setRGB(x, y, bitMatrix.get(x, y) ? FRONT_COLOR : BACK_COLOR);                }            }                        // 2. 在矩陣圖上繪製logo            Image logoImage = ImageIO.read(logoFile);                BufferedImage logoBufferedImage = new BufferedImage(LOGO_SIZE, LOGO_SIZE, BufferedImage.TYPE_INT_RGB);            logoBufferedImage.getGraphics().drawImage(logoImage, 0, 0, null);                        int position = (QRCODE_SIZE - LOGO_SIZE) / 2;            Graphics2D graphics2d = qrcodeImage.createGraphics();            graphics2d.drawImage(logoBufferedImage, position, position, null);            graphics2d.dispose();                        // 3. 儲存圖片            qrcodeImage.flush();            ImageIO.write(qrcodeImage, FORMAT, destFile);                    } catch (Exception e) {            logger.error("#generateLogoQrcode() - create qrcode failed, exception=[{}]", e);            throw BusinessException.newInstance("500", "create qrcode failed");        }            }}

Java產生二維碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.