生成带用户图片的二维码,代码如下:
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";
// 设置二维码中间图片的宽高
private int imageWidth = 30;
private int imageHeight = 30;
/**
*
* 生成二维码(QRCode)图片
*
* @param content
* 存储内容
*
* @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
* 存储内容
* @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
* 存储内容
*
* @param imgType
* 图片类型
*
* @param size
* 二维码尺寸
*
* @return
*
*/
private BufferedImage qRCodeCommon(String content, String imgType, int size) {
BufferedImage bufImg = null;
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.toString().getBytes(codeType);
// 图片尺寸
int imgSize = 67 + 12 * (size - 1);
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;
}
/**
* 在二维码中间加入图片
*
* @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("解析结果如下:");
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 {
// 获取图片的宽高http://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);
}
}
实体类:
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();
}
}