標籤:處理 自己 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產生二維碼