Java二維碼生成 2018-07-16

生成二維碼

package com.ywsoftware.oa.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

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

public class QrCodeUtil {

    /**
     * 創(chuàng)建base64二維碼圖片
     */
    public static String createQrCodeBase64Pic(String content) throws Exception {
        // 圖像寬度
        int width = 139;
        // 圖像高度
        int height = 139;
        // 圖像類型
        String format = "png";
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        // 生成矩陣
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
                BarcodeFormat.QR_CODE, width, height, hints);
        BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
        //新建流
        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
        //利用ImageIO類提供的write方法窄刘,將bi以png圖片的數(shù)據(jù)模式寫入流痴腌。
        ImageIO.write(image, format, arrayOutputStream);
        //從流中獲取數(shù)據(jù)數(shù)組。
        byte[] bytes = arrayOutputStream.toByteArray();
        return "data:image/png;base64," + Base64.getEncoder().encodeToString(bytes);
    }
}

源碼里偷來(lái)的

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Map;
import javax.imageio.ImageIO;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ZxingUtils {
    private static Log log = LogFactory.getLog(ZxingUtils.class);
    private static final int BLACK = -16777216;
    private static final int WHITE = -1;

    public ZxingUtils() {
    }

    private static BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, 1);

        for(int x = 0; x < width; ++x) {
            for(int y = 0; y < height; ++y) {
                image.setRGB(x, y, matrix.get(x, y) ? -16777216 : -1);
            }
        }

        return image;
    }

    private static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
        BufferedImage image = toBufferedImage(matrix);
        if (!ImageIO.write(image, format, file)) {
            throw new IOException("Could not write an image of format " + format + " to " + file);
        }
    }

    public static File getQRCodeImge(String contents, int width, String imgPath) {
        return getQRCodeImge(contents, width, width, imgPath);
    }

    public static File getQRCodeImge(String contents, int width, int height, String imgPath) {
        try {
            Map<EncodeHintType, Object> hints = new Hashtable();
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
            hints.put(EncodeHintType.CHARACTER_SET, "UTF8");
            BitMatrix bitMatrix = (new MultiFormatWriter()).encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
            File imageFile = new File(imgPath);
            writeToFile(bitMatrix, "png", imageFile);
            return imageFile;
        } catch (Exception var7) {
            log.error("create QR code error!", var7);
            return null;
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市青团,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌脓杉,老刑警劉巖馋袜,帶你破解...
    沈念sama閱讀 212,029評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異趁啸,居然都是意外死亡强缘,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,395評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門不傅,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)旅掂,“玉大人,你說(shuō)我怎么就攤上這事访娶∩膛埃” “怎么了?”我有些...
    開封第一講書人閱讀 157,570評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵崖疤,是天一觀的道長(zhǎng)秘车。 經(jīng)常有香客問我,道長(zhǎng)劫哼,這世上最難降的妖魔是什么叮趴? 我笑而不...
    開封第一講書人閱讀 56,535評(píng)論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮权烧,結(jié)果婚禮上眯亦,老公的妹妹穿的比我還像新娘咳蔚。我一直安慰自己,他們只是感情好搔驼,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,650評(píng)論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著侈询,像睡著了一般舌涨。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上扔字,一...
    開封第一講書人閱讀 49,850評(píng)論 1 290
  • 那天囊嘉,我揣著相機(jī)與錄音,去河邊找鬼革为。 笑死扭粱,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的震檩。 我是一名探鬼主播琢蛤,決...
    沈念sama閱讀 39,006評(píng)論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼抛虏!你這毒婦竟也來(lái)了博其?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,747評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤迂猴,失蹤者是張志新(化名)和其女友劉穎慕淡,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體沸毁,經(jīng)...
    沈念sama閱讀 44,207評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡峰髓,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,536評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了息尺。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片携兵。...
    茶點(diǎn)故事閱讀 38,683評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖掷倔,靈堂內(nèi)的尸體忽然破棺而出眉孩,到底是詐尸還是另有隱情,我是刑警寧澤勒葱,帶...
    沈念sama閱讀 34,342評(píng)論 4 330
  • 正文 年R本政府宣布浪汪,位于F島的核電站,受9級(jí)特大地震影響凛虽,放射性物質(zhì)發(fā)生泄漏死遭。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,964評(píng)論 3 315
  • 文/蒙蒙 一凯旋、第九天 我趴在偏房一處隱蔽的房頂上張望呀潭。 院中可真熱鬧钉迷,春花似錦、人聲如沸钠署。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,772評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)谐鼎。三九已至舰蟆,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間狸棍,已是汗流浹背身害。 一陣腳步聲響...
    開封第一講書人閱讀 32,004評(píng)論 1 266
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留草戈,地道東北人塌鸯。 一個(gè)月前我還...
    沈念sama閱讀 46,401評(píng)論 2 360
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像唐片,于是被迫代替她去往敵國(guó)和親丙猬。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,566評(píng)論 2 349

推薦閱讀更多精彩內(nèi)容