java生成SVG图片格式二维码(maven环境)

 <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>


import java.util.ArrayList;
import java.util.List;

import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;

import de.erichseifert.vectorgraphics2d.VectorGraphics2D;
/**
*
* @ClassName: ExportQrCode
* @Description: TODO(
SEE:https://blog.csdn.net/a263295782/article/details/89221090
java实现生成SVG格式的二维码)
* @author heshengjin-何胜金
* @date 2019年7月3日
*
*/
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)));
}
}
}
}




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;
/**
*
* @ClassName: ExportQrCode
* @Description: TODO(
SEE:https://blog.csdn.net/a263295782/article/details/89221090
java实现生成SVG格式的二维码)
* @author heshengjin-何胜金
* @date 2019年7月3日
*
*/
public class GenerictorSvg {
private static final String FINALLYFILENAME = "D:/zxingSVG_hsj.svg";
private static final String CONTENT = "https://www.baidu.com/10000000?p=hsj";

// 直接复制代码就可生成,所要引入的pom在后面
public static void main(String[] args) throws WriterException, FileNotFoundException {
File file = new File(FINALLYFILENAME);
PrintStream psFile = new PrintStream(file);
psFile.append(startSVG( CONTENT));
psFile.close();
}

public static String startSVG(String content) throws WriterException, FileNotFoundException {
double point_x = 0;
double point_y = 0;
final int blockSize = 1;
SVGGraphics2D funcOld = new SVGGraphics2D(point_x, point_y, 800 * blockSize, 800 * blockSize);
ExportQrCode.fill2VectorLine(funcOld, GetBitMatrix(content, 800, ErrorCorrectionLevel.M),
blockSize);
return funcOld.toString();
/*
* File file = new File(finallyFileName); PrintStream psFile = new
* PrintStream(file); psFile.append(funcOld.toString()); psFile.close();
*/
}
private 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;
}
}

生成的二维码

发表评论
留言与评论(共有 0 条评论)
   
验证码:

相关文章

推荐文章

'); })();