这2天跟图片杠上了,项目经理要求上传到腾讯云上的二维码既要有img格式的,又要要svg格式,谷歌了半天在网上找了个swt做的maven项目,可以生成各种格式的二维码,奈何代码全程没注解,好在方法名起的比较规范,就用了2个小时把需要的功能提炼出来,下面为在本地的的示例demo,测试可实现.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Hashtable;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import de.erichseifert.vectorgraphics2d.SVGGraphics2D;
public class Test {
//直接复制代码就可生成,所要引入的pom在后面
public static void main(String[] args) throws WriterException, FileNotFoundException {
double point_x = 0;
double point_y = 0;
final int blockSize = 1;
SVGGraphics2D funcOld = new SVGGraphics2D(point_x, point_y, 300 * blockSize, 300 * blockSize);
ExportQrCode.fill2VectorLine(funcOld,GetBitMatrix("https://www.baidu.com/", 300,ErrorCorrectionLevel.M), blockSize);
File file = new File("C:/Users/Administrator/Desktop/2.svg");
PrintStream psFile = new PrintStream(file);
psFile.append(funcOld.toString());
psFile.close();
}
public static BitMatrix GetBitMatrix(String content, int size,ErrorCorrectionLevel errorCorrectionLevel ) throws WriterException {
size = (size <= 0) ? 100 : size;
BitMatrix bitMatrix = null;
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.MARGIN, 1); // 控制码图白边
hints.put(EncodeHintType.ERROR_CORRECTION, errorCorrectionLevel); // 容错率
bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, size, size, hints);
return bitMatrix;
}
}
import java.util.ArrayList;
import java.util.List;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import de.erichseifert.vectorgraphics2d.VectorGraphics2D;
public class ExportQrCode {
private static void drawLine(int blockSize, VectorGraphics2D funcOld, int x1, int y1, int x2, int y2) {
java.awt.Rectangle s = new java.awt.Rectangle(x1, y1, blockSize, (y2 - y1 + 1) * blockSize);
funcOld.fill(s);
}
public static void fill2VectorLine(VectorGraphics2D funcOld, BitMatrix bitMatrix, int blockSize)
throws WriterException {
if (funcOld == null || bitMatrix == null)
return;
// 256 和32是有却别的,
double width = bitMatrix.getWidth();
double height = bitMatrix.getHeight();
for (int x = 0; x < width; x++) {
int theX = x * blockSize;
List tmp = new ArrayList();
int jsq = 0;
int prev = -1;
for (int y = 0; y < height; y++) {
if (bitMatrix.get(x, y)) {
if (prev == -1) {
jsq++;
prev = y;
continue;
}
if (1 == y - prev) {// 判断是否是连续的 下一个-上一个=1
jsq++;
prev = y;
} else {
tmp.add(String.format("a:%s->%s", (y - jsq), (y)));
drawLine(blockSize, funcOld, theX, (y - jsq), theX, y);
jsq = 0;
prev = y;
}
} else {
if (prev >= 0) {
int y1 = (prev - jsq + 1);
int y2 = prev;
tmp.add(String.format("b:%s->%s", (y1), (y2)));
if (y1 == y2) {
funcOld.fillRect(theX, y1, blockSize, blockSize);
} else {
drawLine(blockSize, funcOld, theX, y1, theX, y2);
}
jsq = 0;
prev = -1;
}
}
}
if (jsq > 0) {
drawLine(blockSize, funcOld, theX, prev, theX, (prev + jsq - 1));
tmp.add(String.format("c:%s->%s", (prev), (prev + jsq - 1)));
}
}
}
}
//pom文件,注意,这里我只导入了需要生成svg格式的jar包
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>de.erichseifert.vectorgraphics2d</groupId>
<artifactId>VectorGraphics2D</artifactId>
<version>0.9.3</version>
</dependency>
参考maven项目链接:download.csdn.net/download/frogchouchou/10343272?utm_source=bbsseo
本文介绍了一种使用Java和Maven项目生成SVG与IMG格式二维码的方法。通过解析无注释代码并提炼核心功能,实现了从URL生成指定格式的二维码,并在本地进行测试验证。
2012

被折叠的 条评论
为什么被折叠?



