欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

Java

當前位置:主頁 > 軟件編程 > Java >

JAVA實現(xiàn)二維碼生成加背景圖代碼實例

來源:本站原創(chuàng)|時間:2020-01-10|欄目:Java|點擊: 次

這篇文章主要介紹了JAVA實現(xiàn)二維碼生成加背景圖代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

pom.xml依賴

<!-- 二維碼生成 -->
    <!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
    <dependency>
      <groupId>com.google.zxing</groupId>
      <artifactId>core</artifactId>
      <version>3.0.1</version>
    </dependency>
/**
 * 類名稱:QRCodeMax 
 * 類描述:生成二維碼圖片+背景+文字描述工具類
 * 創(chuàng)建人:一個除了帥氣,一無是處的男人
 * 創(chuàng)建時間:2018年12月x日x點x分x秒
 * 修改時間:2019年2月x日x點x分x秒
 * 修改備注:更新有參數(shù)構(gòu)造
 * @version: 2.0
 *
 */
public class QRCodeMax {
  
 
  //文字顯示
  private static final int QRCOLOR = 0x201f1f; // 二維碼顏色:黑色
  private static final int BGWHITE = 0xFFFFFF; //二維碼背景顏色:白色
 
  // 設置QR二維碼參數(shù)信息
  private static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() {
    private static final long serialVersionUID = 1L;
    {
      put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);// 設置QR二維碼的糾錯級別(H為最高級別)
      put(EncodeHintType.CHARACTER_SET, "utf-8");// 設置編碼方式
      put(EncodeHintType.MARGIN, 0);// 白邊
    }
  };
 
    /**
    * 生成二維碼圖片+背景+文字描述
    * @param codeFile 生成圖地址
    * @param bgImgFile 背景圖地址
    * @param WIDTH 二維碼寬度
    * @param HEIGHT 二維碼高度
    * @param qrUrl 二維碼識別地址
    * @param note 文字描述1
    * @param tui  文字描述2
    * @param size 文字大小
    * @param imagesX 二維碼x軸方向
    * @param imagesY 二維碼y軸方向
    * @param text1X 文字描述1x軸方向
    * @param text1Y 文字描述1y軸方向
    * @param text2X 文字描述2x軸方向
    * @param text2Y 文字描述2y軸方向
    */
    public static void CreatQRCode( File codeFile, File bgImgFile,Integer WIDTH,Integer HEIGHT,String qrUrl,
        String note,String tui,Integer size,Integer imagesX,Integer imagesY,Integer text1X,Integer text1Y
        ,Integer text2X,Integer text2Y) {
      try {
        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
        // 參數(shù)順序分別為: 編碼內(nèi)容,編碼類型,生成圖片寬度,生成圖片高度,設置參數(shù)
        BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
 
        // 開始利用二維碼數(shù)據(jù)創(chuàng)建Bitmap圖片,分別設為黑(0xFFFFFFFF) 白(0xFF000000)兩色
        for (int x = 0; x < WIDTH; x++) {
          for (int y = 0; y < HEIGHT; y++) {
            image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
          }
        }
        
        /*
         *   添加背景圖片
         */
        BufferedImage backgroundImage = ImageIO.read(bgImgFile);
        int bgWidth=backgroundImage.getWidth();
        int qrWidth=image.getWidth();
        //距離背景圖片x邊的距離,居中顯示
        int disx=(bgWidth-qrWidth)-imagesX;
        //距離y邊距離 * * * *
        int disy=imagesY;
        Graphics2D rng=backgroundImage.createGraphics();
        rng.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP));
        rng.drawImage(image,disx,disy,WIDTH,HEIGHT,null);
 
        /*
         *   文字描述參數(shù)設置
         */
        
        Color textColor=Color.white;
        rng.setColor(textColor);
        rng.drawImage(backgroundImage,0,0,null);
        //設置字體類型和大小(BOLD加粗/ PLAIN平常)
        rng.setFont(new Font("微軟雅黑,Arial",Font.BOLD,size));
        //設置字體顏色
        rng.setColor(Color.black);
        int strWidth=rng.getFontMetrics().stringWidth(note);
        
        //文字1顯示位置
        int disx1=(bgWidth-strWidth)-text1X;//左右
        rng.drawString(note,disx1,text1Y);//上下
        
        //文字2顯示位置
        int disx2=(bgWidth-strWidth)-text2X;//左右
        rng.drawString(tui,disx2,text2Y);//上下
        
        rng.dispose();
        image=backgroundImage;
        image.flush();
        ImageIO.write(image, "png", codeFile);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    
    /**
     * 測試
     * @param args
     */
    public static void main(String[] args) {
      File bgImgFile=new File("D://tu/bg.png");//背景圖片
      File QrCodeFile = new File("D://tu/myqrcode.png");//生成圖片位置
      String url = "https://blog.csdn.net/weixin_38407595";//二維碼鏈接
      String note = "" ;//文字描述
      String tui = "" ;//文字描述
      
      
      //宣傳二維碼生成
      //生成圖地址,背景圖地址,二維碼寬度,二維碼高度,二維碼識別地址,文字描述1,文字描述2,文字大小,圖片x軸方向,圖片y軸方向,文字1||2xy軸方向
      CreatQRCode(QrCodeFile,bgImgFile, 148, 148, url, note,tui, 38, 408, 123, 0, 0, 410, 210);
 
    }
}

效果

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持我們。

上一篇:詳解IDEA JUnit5測試套件運行錯誤的問題

欄    目:Java

下一篇:mybatis if標簽使用總結(jié)

本文標題:JAVA實現(xiàn)二維碼生成加背景圖代碼實例

本文地址:http://mengdiqiu.com.cn/a1/Java/8882.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有