Zxing 產生二維碼嵌套logo,並輸出到瀏覽器,zxinglogo

來源:互聯網
上載者:User

Zxing 產生二維碼嵌套logo,並輸出到瀏覽器,zxinglogo

 哎呀,看了那麼多部落格,搬運了那麼多代碼,第一次自己寫部落格,我發現即使會使用某技術了,但是要表達、轉述出來還是不簡單的。希望我能堅持下去哈。

為什麼使用Zxing?Google 名氣大啊,其他我也不瞭解啊。

上手還是很容易的,引入jar 包後十幾代碼的事。

 

1、在pom.xml 引入依賴

        <!-- 條碼、二維碼產生  -->        <dependency>            <groupId>com.google.zxing</groupId>            <artifactId>core</artifactId>            <version>3.0.0</version>        </dependency>        <dependency>            <groupId>com.google.zxing</groupId>            <artifactId>javase</artifactId>            <version>3.0.0</version>        </dependency>

 

2、後台controler層 繪製輸出二維碼代碼@RequestMapping("createQRCode")    public String createQRCode(HttpServletRequest request, HttpServletResponse response){

//如果目前使用者有渠道,則產生二維碼        String id = UserUtils.getUser().getId();        LoanChannel channelByUserId = loanChannelService.getChannelByUserId(id);        if (channelByUserId!=null){            JSONObject json = new JSONObject();            json.put(                    "channelName",                    channelByUserId.getName());            json.put("channelNo", channelByUserId.getChannelNo());            String content = json.toJSONString();// 內容            int width = 210; // 映像寬度            int height = 200; // 映像高度            String format = "png";// 映像類型            Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");            //設定錯誤修正層級            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);            BitMatrix bitMatrix = null;// 產生矩陣            try {                bitMatrix = new MultiFormatWriter().encode(content,                        BarcodeFormat.QR_CODE, width, height, hints);                /** 定義一張空白緩衝流圖片 */                BufferedImage image = new BufferedImage(bitMatrix.getWidth(), bitMatrix.getHeight(),                        BufferedImage.TYPE_INT_RGB);                /** 迴圈二維碼位元組轉化對象中所有點  矩陣轉image*/                for (int x = 0; x < bitMatrix.getWidth(); x++){                    for (int y = 0; y < bitMatrix.getHeight(); y++){                        /** 擷取其中一點,判斷它是黑色還是白色   true:黑色   false:白色 */                        int rgb = bitMatrix.get(x, y) ? 0x000000 : 0xffffff;                        /** 在空白圖片繪製一點 */                        image.setRGB(x, y, rgb);                    }                }
          //載入logo InputStream stream = this.getClass().getResourceAsStream("/images/logo.png"); BufferedImage logo = ImageIO.read(stream);
          //嵌套logo BufferedImage logoMatrix = LogoConfig.LogoMatrix(image, logo); ImageIO.write(logoMatrix,format,response.getOutputStream()); } catch (WriterException e) { logger.info("輸出二維碼錯誤",e); e.printStackTrace(); } catch (IOException e) { logger.info("輸出二維碼錯誤",e); e.printStackTrace(); } String data=channelByUserId.getName()+"-"+channelByUserId.getChannelNo(); return data; } return ""; }

3、二維碼嵌套logo處理類

public class LogoConfig{    /**     * 設定 logo     * @param matrixImage 源二維碼圖片     * @return 返回帶有logo的二維碼圖片     * @throws IOException     * @author Administrator sangwenhao     */    public static BufferedImage LogoMatrix(BufferedImage matrixImage, BufferedImage logo) throws IOException{        /**         * 讀取二維碼圖片,並構建繪圖對象         */        Graphics2D g2 = matrixImage.createGraphics();        int matrixWidth = matrixImage.getWidth();        int matrixHeigh = matrixImage.getHeight();        //開始繪製圖片        g2.drawImage(logo,matrixWidth/5*2,matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5, null);//繪製        BasicStroke stroke = new BasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);        g2.setStroke(stroke);// 設定筆畫對象        //指定弧度的圓角矩形        RoundRectangle2D.Float round = new RoundRectangle2D.Float(matrixWidth/5*2, matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5,20,20);        g2.setColor(Color.white);        g2.draw(round);// 繪製圓弧矩形        //設定logo 有一道灰色邊框        BasicStroke stroke2 = new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);        g2.setStroke(stroke2);// 設定筆畫對象        RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(matrixWidth/5*2+2, matrixHeigh/5*2+2, matrixWidth/5-4, matrixHeigh/5-4,20,20);        g2.setColor(new Color(128,128,128));        g2.draw(round2);// 繪製圓弧矩形*/        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);        matrixImage.flush() ;        g2.dispose();        return matrixImage ;    }}

4、前端使用 a標籤href 使用圖片地址可以直接完成圖片的點擊下載,download 屬性用於指定儲存的檔案名稱

  img src 由後台輸出

<a id="fileName" href="${ctx}/loan/loanChannel/createQRCode" download="渠道.png">  <img src="${ctx}/loan/loanChannel/createQRCode"></a>

 5、效果

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.