java產生二維碼

來源:互聯網
上載者:User

標籤:處理   自己   mode   hit   cimage   path   bsp   new   釋放   

其實在寫這個東西的時候,有些細節問題的配置,我就不再詳細的寫了,唯寫了相關的主要原始碼:

package com.tz.util;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.imageio.ImageIO;

import com.swetake.util.Qrcode;//引進的包,我自己

/**
* 產生二維碼
* @author yanlong
* content 二維碼的內容
* imgPath二維碼的路徑
* return void 返回的類型
*
*/
public class QrcodeImg {
//產生一個二維碼的方法
public static void getQrcodeImg(String content,String imgPath){
//執行個體化Qrcode 對象
Qrcode qcQrcode=new Qrcode();
//編碼
qcQrcode.setQrcodeEncodeMode(‘B‘);
//排錯率15%的大小
qcQrcode.setQrcodeErrorCorrect(‘M‘);
//版本
qcQrcode.setQrcodeVersion(15);

int width=235;
int height=235;
//花板
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
//繪製工具
Graphics2D gs=image.createGraphics();
//開始繪製
//背景色
gs.setBackground(Color.white);
//繪製矩形
gs.clearRect(0, 0, width, height);
//設定內容的顏色
gs.setColor(Color.black);
//開始處理我們的資訊
byte[] codeOut;
try {
codeOut =content.getBytes("utf-8");
//通過byte返回布爾類型的數組。
boolean[][] code=qcQrcode.calQrcode(codeOut);
for(int i=0;i<code.length;i++){
for(int j=0;j<code.length;j++){
if(code[j][i]){
//如果為真則塗成黑色
gs.fillRect(j*3+2, i*3+2, 3, 3);
}
}
}
//載入圖片
File file=new File("C:/Users/yanlong/Desktop/123456.png");
Image srcImage=ImageIO.read(file);
int _width=srcImage.getWidth(null);
int _heigth=srcImage.getHeight(null);
gs.drawImage(srcImage,(width-_width)/2,(height-_heigth)/2,_width,_heigth,null);
//釋放資源
gs.dispose();
image.flush();
//儲存------,寫入指定路徑

ImageIO.write(image, "png", new File(imgPath));
System.out.println("二維碼產生成功");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
//主方法
public static void main(String[] args){

getQrcodeImg("雖然我相信付出和收穫不一定成正比,但是我更相信天道酬勤!---陳燕龍","C:/Users/yanlong/Desktop/3.png");


}

}

————————————————————————————————————————————————————————————

二維碼

 

 

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.