package com.fh.util;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.exception.DecodingFailedException;
import com.swetake.util.Qrcode;
/**
* 二維碼 創建人:FH 313596790QQ 創建時間:2015年4月10日
*
* @version
*/
public class TwoDimensionCode {
/**
* 生成二維碼(QRCode)圖片
*
* @param content
* 存儲內容
* @param imgPath
* 圖片路徑
*/
public static void encoderQRCode(String content, String imgPath) {
encoderQRCode(content, imgPath, "png", 2);
}
/**
* 生成二維碼(QRCode)圖片
*
* @param content
* 存儲內容
* @param output
* 輸出流
*/
public static void encoderQRCode(String content, OutputStream output) {
encoderQRCode(content, output, "png", 2);
}
/**
* 生成二維碼(QRCode)圖片
*
* @param content
* 存儲內容
* @param imgPath
* 圖片路徑
* @param imgType
* 圖片類型
*/
public static void encoderQRCode(String content, String imgPath,
String imgType) {
encoderQRCode(content, imgPath, imgType, 2);
}
/**
* 生成二維碼(QRCode)圖片
*
* @param content
* 存儲內容
* @param output
* 輸出流
* @param imgType
* 圖片類型
*/
public static void encoderQRCode(String content, OutputStream output,
String imgType) {
encoderQRCode(content, output, imgType, 2);
}
/**
* 生成二維碼(QRCode)圖片
*
* @param content
* 存儲內容
* @param imgPath
* 圖片路徑
* @param imgType
* 圖片類型
* @param size
* 二維碼尺寸
*/
public static void encoderQRCode(String content, String imgPath,
String imgType, int size) {
try {
BufferedImage bufImg = qRCodeCommon(content, imgType, size);
File imgFile = new File(imgPath);
// 生成二維碼QRCode圖片
ImageIO.write(bufImg, imgType, imgFile);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 生成二維碼(QRCode)圖片
*
* @param content
* 存儲內容
* @param output
* 輸出流
* @param imgType
* 圖片類型
* @param size
* 二維碼尺寸
*/
public static void encoderQRCode(String content, OutputStream output,
String imgType, int size) {
try {
BufferedImage bufImg = qRCodeCommon(content, imgType, size);
// 生成二維碼QRCode圖片
ImageIO.write(bufImg, imgType, output);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 生成二維碼(QRCode)圖片的公共方法
*
* @param content
* 存儲內容
* @param imgType
* 圖片類型
* @param size
* 二維碼尺寸
* @return
*/
private static BufferedImage qRCodeCommon(String content, String imgType,
int size) {
BufferedImage bufImg = null;
size = 10;
try {
Qrcode qrcodeHandler = new Qrcode();
// 設置二維碼排錯率,可選L(7%)、M(15%)、Q(25%)、H(30%),排錯率越高可存儲的信息越少,但對二維碼清晰度的要求越小
qrcodeHandler.setQrcodeErrorCorrect('M');
qrcodeHandler.setQrcodeEncodeMode('B');
// 設置設置二維碼尺寸,取值范圍1-40,值越大尺寸越大,可存儲的信息越大
qrcodeHandler.setQrcodeVersion(size);
// 獲得內容的字節數組,設置編碼格式
byte[] contentBytes = content.getBytes("utf-8");
// 圖片尺寸
// int imgSize = 67 + 12 * (size - 1);
int imgSize = 67 + 12 * (size - 1);
// System.out.println(imgSize);
bufImg = new BufferedImage(imgSize, imgSize,
BufferedImage.TYPE_INT_RGB);
Graphics2D gs = bufImg.createGraphics();
// 設置背景顏色
gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, imgSize, imgSize);
// 設定圖像顏色> BLACK
gs.setColor(Color.BLACK);
// 設置偏移量,不設置可能導致解析出錯
int pixoff = 2;
// 輸出內容> 二維碼
if (contentBytes.length > 0 && contentBytes.length < 800) {
boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);
for (int i = 0; i < codeOut.length; i++) {
for (int j = 0; j < codeOut.length; j++) {
if (codeOut[j][i]) {
gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
}
}
}
} else {
throw new Exception("QRCode content bytes length = "
+ contentBytes.length + " not in [0, 800].");
}
gs.dispose();
bufImg.flush();
} catch (Exception e) {
e.printStackTrace();
}
return bufImg;
}
/**
* 解析二維碼(QRCode)
*
* @param imgPath
* 圖片路徑
* @return
*/
public static String decoderQRCode(String imgPath) throws Exception {
// QRCode 二維碼圖片的文件
File imageFile = new File(imgPath);
BufferedImage bufImg = null;
String content = null;
try {
bufImg = ImageIO.read(imageFile);
QRCodeDecoder decoder = new QRCodeDecoder();
content = new String(decoder.decode(new TwoDimensionCodeImage(
bufImg)), "utf-8");
} catch (IOException e) {
// System.out.println("Error: " + e.getMessage());
// e.printStackTrace();
} catch (DecodingFailedException dfe) {
// System.out.println("Error: " + dfe.getMessage());
// dfe.printStackTrace();
}
return content;
}
/**
* 解析二維碼(QRCode)
*
* @param input
* 輸入流
* @return
*/
public static String decoderQRCode(InputStream input) {
BufferedImage bufImg = null;
String content = null;
try {
bufImg = ImageIO.read(input);
QRCodeDecoder decoder = new QRCodeDecoder();
content = new String(decoder.decode(new TwoDimensionCodeImage(
bufImg)), "utf-8");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
} catch (DecodingFailedException dfe) {
System.out.println("Error: " + dfe.getMessage());
dfe.printStackTrace();
}
return content;
}
/**
* 二維碼繪制logo
*
* @param twodimensioncodeImg
* 二維碼圖片文件
* @param logoImg
* logo圖片文件
* @throws IOException
* */
public static BufferedImage encodeImgLogo(File twodimensioncodeImg,
File logoImg) throws IOException {
BufferedImage twodimensioncode = null;
try {
if (!twodimensioncodeImg.isFile() || !logoImg.isFile()) {
System.out.println("輸入非圖片");
return null;
}
// 讀取二維碼圖片
twodimensioncode = ImageIO.read(twodimensioncodeImg);
// 獲取畫筆
Graphics2D g = twodimensioncode.createGraphics();
// 讀取logo圖片
BufferedImage logo = ImageIO.read(logoImg);
// 設置二維碼大小,太大,會覆蓋二維碼,此處20%
int logoWidth = logo.getWidth(null) > twodimensioncode.getWidth() * 2 / 10 ? (twodimensioncode
.getWidth() * 2 / 10) : logo.getWidth(null);
int logoHeight = logo.getHeight(null) > twodimensioncode
.getHeight() * 2 / 10 ? (twodimensioncode.getHeight() * 2 / 10)
: logo.getHeight(null);
// 設置logo圖片放置位置
// 中心
int x = (twodimensioncode.getWidth() - logoWidth) / 2;
int y = (twodimensioncode.getHeight() - logoHeight) / 2;
// 右下角,15為調整值
// int x = twodimensioncode.getWidth() - logoWidth-15;
// int y = twodimensioncode.getHeight() - logoHeight-15;
// 開始合並繪制圖片
g.drawImage(logo, x, y, logoWidth, logoHeight, null);
g.drawRoundRect(x, y, logoWidth, logoHeight, 15, 15);
// logo邊框大小
g.setStroke(new BasicStroke(2));
// logo邊框顏色
g.setColor(Color.WHITE);
g.drawRect(x, y, logoWidth, logoHeight);
g.dispose();
logo.flush();
twodimensioncode.flush();
} catch (Exception e) {
System.out.println("二維碼繪制logo失敗");
}
ImageIO.write(twodimensioncode, "png", twodimensioncodeImg);// 輸出png圖片
return twodimensioncode;
}
/**
* 二維碼輸出到文件
*
* @param twodimensioncodeImg
* 二維碼圖片文件
* @param logoImg
* logo圖片文件
* @param format
* 圖片格式
* @param file
* 輸出文件
* @throws IOException
* */
public static void writeToFile(File twodimensioncodeImg, File logoImg,
String format, File file) throws IOException {
BufferedImage image = encodeImgLogo(twodimensioncodeImg, logoImg);
try {
ImageIO.write(image, format, file);
} catch (IOException e) {
System.out.println("二維碼寫入文件失敗" + e.getMessage());
}
}
/**
* 二維碼流式輸出
*
* @param twodimensioncodeImg
* 二維碼圖片文件
* @param logoImg
* logo圖片文件
* @param format
* 圖片格式
* @param stream
* 輸出流
* @throws IOException
* */
public static void writeToStream(File twodimensioncodeImg, File logoImg,
String format, OutputStream stream) throws IOException {
BufferedImage image = encodeImgLogo(twodimensioncodeImg, logoImg);
try {
ImageIO.write(image, format, stream);
} catch (IOException e) {
System.out.println("二維碼寫入流失敗" + e.getMessage());
}
}
public static void main(String[] args) {
//在這測試
}
}
package com.fh.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class FontImage {
public static void main(String[] args) throws Exception {
createImage("102桌", new Font("宋體", Font.BOLD, 14), new File("g:/a.png"));
createImage("102桌", new Font("黑體", Font.BOLD, 14),new File("g:/a1.png"));
createImage("102桌", new Font("黑體", Font.PLAIN, 14), new File("g:/a2.png"));
}
// 根據str,font的樣式以及輸出文件目錄
public static void createImage(String str, Font font, File outFile)
throws Exception {
int width = 40;
int height = 40;
// 創建圖片
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_BGR);
Graphics g = image.getGraphics();
g.setClip(0, 0, width, height);
g.setColor(Color.white);
g.fillRect(0, 0, width, height);// 先用黑色填充整張圖片,也就是背景
g.setColor(Color.red);// 在換成黑色
g.setFont(font);// 設置畫筆字體
/** 用於獲得垂直居中y */
Rectangle clip = g.getClipBounds();
FontMetrics fm = g.getFontMetrics(font);
int ascent = fm.getAscent();
int descent = fm.getDescent();
int y = (clip.height - (ascent + descent)) / 2 + ascent;
for (int i = 0; i < 6; i++) {// 256 340 0 680
g.drawString(str, i * 680, y);// 畫出字符串
}
g.dispose();
ImageIO.write(image, "png", outFile);// 輸出png圖片
}
}