生成帶用戶圖片的二維碼,代碼如下:
package Fuck;
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.OutputStream;
import javax.imageio.ImageIO;
import org.im4java.core.ConvertCmd;
import org.im4java.core.IM4JavaException;
import org.im4java.core.IMOperation;
import com.swetake.util.Qrcode;
public class TwoDimensionCode {
private static String imgType = "png";// 二維碼圖片格式
private static int size = 7;// 二維碼尺寸
private String codeType = "utf-8";
public static String IMAGEMAGICK = "/usr/local/Cellar/imagemagick/7.0.5-0/bin";
private static String person = "http://wx.qlogo.cn/mmopen/SGIdzVKlfzFA5k0GvqTjEEldrm3cuZypKQ8Z1WrPa5I83dOVwibQ65fJwbchgWouLPvexzlksN5ch0SLgUicMSdlxxaAMlGje1/0";
// 設(shè)置二維碼中間圖片的寬高
private int imageWidth = 30;
private int imageHeight = 30;
/**
*
* 生成二維碼(QRCode)圖片
*
* @param content
*? ? ? ? ? ? 存儲(chǔ)內(nèi)容
*
* @param imgPath
*? ? ? ? ? ? 圖片路徑
*
* @param imgType
*? ? ? ? ? ? 圖片類型
*
* @param size
*? ? ? ? ? ? 二維碼尺寸
*
*/
public void encoderQRCode(String content, String imgPath, String imgType, int size) {
try {
BufferedImage bufImg = this.qRCodeCommon(content, imgType, size);
// 在二維碼中間加入圖片
createPhotoAtCenter(bufImg);
File imgFile = new File(imgPath);
// 生成二維碼QRCode圖片
ImageIO.write(bufImg, imgType, imgFile);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 生成二維碼(QRCode)圖片
*
* @param content
*? ? ? ? ? ? 存儲(chǔ)內(nèi)容
* @param output
*? ? ? ? ? ? 輸出流
* @param imgType
*? ? ? ? ? ? 圖片類型
* @param size
*? ? ? ? ? ? 二維碼尺寸
*/
public void encoderQRCode(String content, OutputStream output, String imgType, int size) {
try {
BufferedImage bufImg = this.qRCodeCommon(content, imgType, size);
// 在二維碼中間加入圖片
// createPhotoAtCenter(bufImg);
// 生成二維碼QRCode圖片
ImageIO.write(bufImg, imgType, output);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* 生成二維碼(QRCode)圖片的公共方法
*
* @param content
*? ? ? ? ? ? 存儲(chǔ)內(nèi)容
*
* @param imgType
*? ? ? ? ? ? 圖片類型
*
* @param size
*? ? ? ? ? ? 二維碼尺寸
*
* @return
*
*/
private BufferedImage qRCodeCommon(String content, String imgType, int size) {
BufferedImage bufImg = null;
try {
Qrcode qrcodeHandler = new Qrcode();
// 設(shè)置二維碼排錯(cuò)率,可選L(7%)散庶、M(15%)采呐、Q(25%)若锁、H(30%),排錯(cuò)率越高可存儲(chǔ)的信息越少斧吐,但對(duì)二維碼清晰度的要求越小
qrcodeHandler.setQrcodeErrorCorrect('M');
qrcodeHandler.setQrcodeEncodeMode('B');
// 設(shè)置設(shè)置二維碼尺寸又固,取值范圍1-40,值越大尺寸越大煤率,可存儲(chǔ)的信息越大
qrcodeHandler.setQrcodeVersion(size);
// 獲得內(nèi)容的字節(jié)數(shù)組仰冠,設(shè)置編碼格式
byte[] contentBytes = content.toString().getBytes(codeType);
// 圖片尺寸
int imgSize = 67 + 12 * (size - 1);
bufImg = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB);
Graphics2D gs = bufImg.createGraphics();
// 設(shè)置背景顏色
gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, imgSize, imgSize);
// 設(shè)定圖像顏色> BLACK
gs.setColor(Color.BLACK);
// 設(shè)置偏移量,不設(shè)置可能導(dǎo)致解析出錯(cuò)
int pixoff = 2;
// 輸出內(nèi)容> 二維碼
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;
}
/**
* 在二維碼中間加入圖片
*
* @param bugImg
* @return
*/
private BufferedImage createPhotoAtCenter(BufferedImage bufImg) throws Exception {
Image im = ImageIO.read(new File("/Users/song/Desktop/2.png"));
Graphics2D g = bufImg.createGraphics();
// 獲取bufImg的中間位置
int centerX = bufImg.getMinX() + bufImg.getWidth() / 2 - imageWidth / 2;
int centerY = bufImg.getMinY() + bufImg.getHeight() / 2 - imageHeight / 2;
g.drawImage(im, centerX, centerY, imageWidth, imageHeight, null);
g.dispose();
bufImg.flush();
return bufImg;
}
public static void main(String[] args) throws IOException {
// String imgPath = "/Users/song/Desktop/8.jpeg";
// String encoderContent = "13663865897";
// TwoDimensionCode handler = new TwoDimensionCode();
// handler.encoderQRCode(encoderContent, imgPath, imgType, size);
System.out.println("解析結(jié)果如下:");
try {
maskCompositePic("/Users/song/Desktop/80.jpg");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IM4JavaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void maskCompositePic(String desPath)
throws IOException, InterruptedException, IM4JavaException {
// 獲取圖片的寬高h(yuǎn)ttp://image.haiziyouke.com/010101.jpg? width750:height900
// BufferedImage bi = ImageIO.read(new File("/Users/song/Desktop/2.jpg"));
// int width = bi.getWidth();
// int height = bi.getHeight();
// System.out.println("width" + width + ":height" + height);
IMOperation op = new IMOperation();
op.addImage("/Users/song/Desktop/8.jpg");
op.compose("atop");
op.addImage("/Users/song/Desktop/8.jpeg");
//op.addImage(TO);width + 60, height + 60
//op.geometry(width + 60, height + 60, 482,630);
op.geometry(110,110, 479,575);
op.composite();
op.addImage(desPath);
ConvertCmd cmd = new ConvertCmd();
cmd.setSearchPath(IMAGEMAGICK);
cmd.run(op);
}
}
實(shí)體類:
package Fuck;
import java.awt.image.BufferedImage;
import jp.sourceforge.qrcode.data.QRCodeImage;
public class TwoDimensionCodeImage implements QRCodeImage{
BufferedImage bufImg;
public TwoDimensionCodeImage(BufferedImage bufImg) {
this.bufImg = bufImg;
}
public int getHeight() {
return bufImg.getHeight();
}
public int getPixel(int x, int y) {
return bufImg.getRGB(x, y);
}
public int getWidth() {
return bufImg.getWidth();
}
}