最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java生成二維碼并且給二維碼添加logo

 更新時間:2019年12月11日 11:10:18   作者:風(fēng)起塵落  
這篇文章主要介紹了java生成二維碼并且給二維碼添加logo的實例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下

java生成二維碼,具體代碼如下所示:

package com.bus.wx.action.code;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import com.bus.plugin.wx.action.WxAction;
import com.bus.wx.util.Limits;
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 com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.lys.sys.log.Log;
/**
 * 條形碼、二維碼
 * @author hwj
 *
 */
@Scope(value = "prototype")
@Controller("Bar_Qr_Code_Action")
@RequestMapping(value="plug/wx/wwz/{bcflag}/barqrcode")
public class Bar_Qr_Code_Action extends WxAction{
 private static final int LogoPart = 4;
 private static final int BLACK = 0xFF000000;//用于設(shè)置圖案的顏色 
  private static final int WHITE = 0xFFFFFFFF; //用于背景色 
  String format = "png";
public static BufferedImage toBufferedImage(BitMatrix matrix) { 
  int width = matrix.getWidth(); 
  int height = matrix.getHeight(); 
  BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
  for (int x = 0; x < width; x++) { 
    for (int y = 0; y < height; y++) { 
      image.setRGB(x, y, (matrix.get(x, y) ? BLACK : WHITE)); 
//     image.setRGB(x, y, (matrix.get(x, y) ? Color.YELLOW.getRGB() : Color.CYAN.getRGB())); 
    } 
  } 
  return image; 
}
  public BufferedImage LogoMatrix(BufferedImage matrixImage) throws IOException{ 
     /** 
      * 讀取二維碼圖片,并構(gòu)建繪圖對象 
      */ 
     Graphics2D g2 = matrixImage.createGraphics(); 
      
     int matrixWidth = matrixImage.getWidth(); 
     int matrixHeigh = matrixImage.getHeight(); 
      
     /** 
      * 讀取Logo圖片 
      */ 
     String path=request.getSession().getServletContext().getRealPath("/images/logo/dzjkklog.png");
     BufferedImage logo = ImageIO.read(new File(path)); 
  
     //開始繪制圖片 
     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);// 設(shè)置筆畫對象 
     //指定弧度的圓角矩形 
     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);// 繪制圓弧矩形 
      
     //設(shè)置logo 有一道灰色邊框 
     BasicStroke stroke2 = new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);  
     g2.setStroke(stroke2);// 設(shè)置筆畫對象 
     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.dispose(); 
     matrixImage.flush() ; 
     return matrixImage ; 
   } 
    
 
 /**
 * 生成二維碼:直接將生成的二維碼傳輸?shù)角芭_頁面
 * @param bcflag
 */
 @RequestMapping(value="createQrCodes",method = RequestMethod.GET)
 public void createQrCodes(@PathVariable String bcflag,String dastid){
 
 String url="掃二維碼出現(xiàn)的內(nèi)容";
 if(url!=null&&!"".equals(url)){
  ServletOutputStream stream=null;
  try {
  int width=430;
  int height=430;
  stream=response.getOutputStream();
  QRCodeWriter writer=new QRCodeWriter();
   Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); 
     
      hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); 
      hints.put(EncodeHintType.CHARACTER_SET, "utf-8");   
      hints.put(EncodeHintType.MARGIN, 1);//設(shè)置二維碼邊的空度,非負數(shù) 
      BitMatrix bitMatrix = new MultiFormatWriter().encode(url,//要編碼的內(nèi)容 
          BarcodeFormat.QR_CODE, 
          width, //條形碼的寬度 
          height, //條形碼的高度 
          hints);
  //BitMatrix m=writer.encode(url, BarcodeFormat.QR_CODE, height,width);
   BufferedImage image = toBufferedImage(bitMatrix); 
    image = LogoMatrix(image); 
     if (!ImageIO.write(image, format, stream)) { 
       throw new IOException("Could not write an image of format " + format); 
     } 
  } catch (Exception e) {
   Log.in.info(e.getMessage());
  }finally{
   if(stream!=null){
   try {
    stream.flush();
    stream.close();
    } catch (IOException e) {
    Log.in.info(e.getMessage());
    }
   }
  }
  }
 }
}

總結(jié)

以上所述是小編給大家介紹的java生成二維碼并且給二維碼添加logo,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

  • Java?SSM框架講解

    Java?SSM框架講解

    這篇文章主要介紹了什么是SSM框架,SSM框架是spring、spring?MVC?、和mybatis框架的整合,是標(biāo)準(zhǔn)的MVC模式。想進一步了解的同學(xué)可以詳細參考本文
    2023-03-03
  • Java實現(xiàn)獲取客戶端真實IP方法小結(jié)

    Java實現(xiàn)獲取客戶端真實IP方法小結(jié)

    本文給大家匯總介紹了2種使用java實現(xiàn)獲取客戶端真實IP的方法,主要用于獲取使用了代理訪問的來訪者的IP,有需要的小伙伴可以參考下。
    2016-03-03
  • Maven發(fā)布Jar包中文亂碼解決方法

    Maven發(fā)布Jar包中文亂碼解決方法

    這篇文章主要介紹了Maven發(fā)布Jar包中文亂碼解決方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • SpringBoot整合Prometheus如何實現(xiàn)資源監(jiān)控

    SpringBoot整合Prometheus如何實現(xiàn)資源監(jiān)控

    本文介紹了如何使用Prometheus監(jiān)控SpringBoot應(yīng)用,Prometheus是一個開源的監(jiān)控和告警工具,SpringBootActuator提供了監(jiān)控和管理SpringBoot應(yīng)用的工具,通過添加依賴、配置Actuator和Prometheus,可以實現(xiàn)對SpringBoot應(yīng)用的實時監(jiān)控
    2024-12-12
  • 簡單談?wù)凧ava中String類型的參數(shù)傳遞問題

    簡單談?wù)凧ava中String類型的參數(shù)傳遞問題

    這篇文章主要介紹了簡單談?wù)凧ava中String類型的參數(shù)傳遞問題的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • 深入java事件注冊的應(yīng)用分析

    深入java事件注冊的應(yīng)用分析

    本篇文章是對java事件注冊進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • Spring Cloud 優(yōu)雅下線以及灰度發(fā)布實現(xiàn)

    Spring Cloud 優(yōu)雅下線以及灰度發(fā)布實現(xiàn)

    這篇文章主要介紹了Spring Cloud 優(yōu)雅下線以及灰度發(fā)布實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • 詳解SpringBoot讀取resource目錄下properties文件的常見方式

    詳解SpringBoot讀取resource目錄下properties文件的常見方式

    這篇文章主要介紹了SpringBoot讀取resource目錄下properties文件的常見方式,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • 一文詳解SpringBoot中CommandLineRunner接口

    一文詳解SpringBoot中CommandLineRunner接口

    Spring Boot的CommandLineRunner接口是一個函數(shù)式接口,用于在Spring Boot應(yīng)用程序啟動后執(zhí)行一些初始化操作,它提供了一個run方法,該方法在應(yīng)用程序啟動后被調(diào)用,本文給大家詳細介紹了SpringBoot中CommandLineRunner接口,需要的朋友可以參考下
    2023-10-10
  • SpringBoot 普通類調(diào)用Bean對象的一種方式推薦

    SpringBoot 普通類調(diào)用Bean對象的一種方式推薦

    這篇文章主要介紹了SpringBoot 普通類調(diào)用Bean對象的一種方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11

最新評論

刚察县| 吉木乃县| 沾益县| 天祝| 凤凰县| 辽阳市| 琼结县| 竹山县| 锡林郭勒盟| 长岭县| 钟山县| 城市| 忻州市| 巴楚县| 达拉特旗| 靖边县| 乌拉特中旗| 密云县| 苏尼特右旗| 侯马市| 德令哈市| 专栏| 开阳县| 青河县| 赤峰市| 镇康县| 盐山县| 玛纳斯县| 中卫市| 蕉岭县| 泗水县| 南江县| 顺昌县| 璧山县| 新密市| 平谷区| 汨罗市| 大理市| 驻马店市| 长子县| 凌海市|