今天,要完成一个批量生成二维码的工作,并将生成好的二维码与另外一张底图拼接,生成一张新图。
Java生成二维码主要的sdk为,qrcode和zxing,二者差别不大,这里采用了zxing。
首先,直接使用其core.jar,然后实现MatrixToImageWriter类。
在使用zxing时,我们发现默认生成的白边太大,想进行修改,通过updateBit方法进行指定白边的宽度。在修改完其白边后,会影响默认设置的二维码大小,通过zoomInImage方法进行修复。
在合成两张图片时,通过SmallBigImage类的overlapImage方法进行实现。
整个项目具体代码如下:
Main类:
packageerweima;
importjava.awt.Graphics;
importjava.awt.image.BufferedImage;
import java.io.File;
import java.util.*;
importjavax.imageio.ImageIO;
importcom.google.zxing.*;
importcom.google.zxing.common.BitMatrix;
publicclass Main {
static String bigPath="E:/erweiimg/background.jpg";
publicstaticvoid main(String[] args) {
try {
for (inti = 0; i < 100; i++) {
String content = "http://101.201.143.212/erweima/carTel.php?qrid="
+ i;
String path = "E:/erweiimg/";
intsize = 751; // 二维码图片大小
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
// hints.put(EncodeHintType.MARGIN, 0);
BitMatrix bitMatrix = multiFormatWriter.encode(content,
BarcodeFormat.QR_CODE, size, size, hints);
// bitMatrix=deleteWhite(bitMatrix);
bitMatrix = updateBit(bitMatrix, 10);
File file1 = new File(path, "qrid_" + i + ".jpg");
File file2 = new File(path, "qrid__" + i + ".jpg");
File file3 = new File(path, "qrid___" + i + ".jpg");
MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file1);
BufferedImage bi = MatrixToImageWriter
.toBufferedImage(bitMatrix);
bi = zoomInImage(bi, size, size);// 根据size放大、缩小生成的二维码
BufferedImage big=ImageIO.read(new File(bigPath));
BufferedImage hebing=SmallBigImage.overlapImage(big,bi);;
//ImageIO.write(bi, "png",file2); // 生成二维码图片
ImageIO.write(hebing, "png", file3);
}
} catch (Exception e) {
e.printStackTrace();
}
}
publicstatic BitMatrix deleteWhite(BitMatrix matrix) {
int[] rec = matrix.getEnclosingRectangle();
intresWidth = rec[2] + 1;
intresHeight = rec[3] + 1;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
resMatrix.clear();
for (inti = 0; i < resWidth; i++) {
for (intj = 0; j < resHeight; j++) {
if (matrix.get(i + rec[0], j + rec[1]))
resMatrix.set(i, j);
}
}
returnresMatrix;
}
privatestatic BitMatrix updateBit(BitMatrix matrix, intmargin) {
inttempM = margin * 2;
int[] rec = matrix.getEnclosingRectangle(); // 获取二维码图案的属性
intresWidth = rec[2] + tempM;
intresHeight = rec[3] + tempM;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); // 按照自定义边框生成新的BitMatrix
resMatrix.clear();
for (inti = margin; i < resWidth - margin; i++) { // 循环,将二维码图案绘制到新的bitMatrix中
for (intj = margin; j < resHeight - margin; j++) {
if (matrix.get(i - margin + rec[0], j - margin + rec[1])) {
resMatrix.set(i, j);
}
}
}
returnresMatrix;
}
publicstatic BufferedImagezoomInImage(BufferedImage originalImage,
intwidth, intheight) {
BufferedImage newImage = new BufferedImage(width, height,
originalImage.getType());
Graphics g = newImage.getGraphics();
g.drawImage(originalImage, 0,0, width, height, null);
g.dispose();
returnnewImage;
}
}
SmallBigImage类:
packageerweima;
importjava.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
importjava.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
publicfinalclass SmallBigImage {
publicstaticfinal BufferedImage overlapImage(BufferedImage big, BufferedImage small) {
try {
// BufferedImage big = ImageIO.read(newFile(bigPath));
// BufferedImage small = ImageIO.read(newFile(smallPath));
Graphics2D g = big.createGraphics();
intx = 197;
inty = 65;
g.drawImage(small, x, y, small.getWidth(), small.getHeight(), null);
g.dispose();
// ImageIO.write(big, "jpg",new File("config/resource/BigSmall.jpg"));
returnbig;
} catch (Exception e) {
e.printStackTrace();
}
returnbig;
}
}
MatrixToImageWriter类:
packageerweima;
importcom.google.zxing.common.BitMatrix;
importjavax.imageio.ImageIO;
importjava.io.File;
importjava.io.OutputStream;
importjava.io.IOException;
importjava.awt.image.BufferedImage;
publicfinalclass MatrixToImageWriter {
privatestaticfinalintBLACK = 0xFF000000;
privatestaticfinalintWHITE = 0xFFFFFFFF;
private MatrixToImageWriter() {
}
publicstatic BufferedImagetoBufferedImage(BitMatrix matrix) {
intwidth = matrix.getWidth();
intheight = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
for (intx = 0; x < width; x++) {
for (inty = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
returnimage;
}
publicstaticvoid writeToFile(BitMatrix matrix, String format, File file)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, file)) {
thrownew IOException("Couldnot write an image of format "
+ format + " to " + file);
}
}
publicstaticvoid writeToStream(BitMatrix matrix, String format,
OutputStream stream) throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, stream)) {
thrownew IOException("Couldnot write an image of format "
+ format);
}
}
}