Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from atomfrede/95-add-svg-support-again
add svg support again
- Loading branch information
Showing
7 changed files
with
190 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
javase/src/main/java/net/glxn/qrgen/javase/MatrixToSvgWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package net.glxn.qrgen.javase; | ||
|
||
import com.google.zxing.client.j2se.MatrixToImageConfig; | ||
import com.google.zxing.common.BitMatrix; | ||
import org.apache.batik.dom.GenericDOMImplementation; | ||
import org.apache.batik.svggen.SVGGraphics2D; | ||
import org.w3c.dom.DOMImplementation; | ||
import org.w3c.dom.Document; | ||
|
||
import java.awt.*; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
|
||
class MatrixToSvgWriter { | ||
|
||
private MatrixToSvgWriter() { | ||
// private utility class constuctor | ||
} | ||
|
||
private static SVGGraphics2D toSvgDocument(BitMatrix matrix, MatrixToImageConfig config) { | ||
|
||
int width = matrix.getWidth(); | ||
int height = matrix.getHeight(); | ||
|
||
DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); | ||
String svgNS = "http://www.w3.org/2000/svg"; | ||
Document document = domImpl.createDocument(svgNS, "svg", null); | ||
|
||
SVGGraphics2D svgGraphics = new SVGGraphics2D(document); | ||
svgGraphics.setColor(new Color(config.getPixelOffColor())); | ||
svgGraphics.fillRect(0,0, width, height); | ||
for (int x = 0; x < width; x++) { | ||
for (int y = 0; y < height; y++) { | ||
if (matrix.get(x,y)) { | ||
svgGraphics.setColor(new Color(config.getPixelOnColor())); | ||
svgGraphics.fillRect(x, y, 1, 1); | ||
} | ||
} | ||
} | ||
|
||
return svgGraphics; | ||
|
||
|
||
} | ||
|
||
static void writeToPath(BitMatrix matrix, Path file, MatrixToImageConfig matrixToImageConfig) throws IOException { | ||
SVGGraphics2D g2 = toSvgDocument(matrix, matrixToImageConfig); | ||
|
||
FileWriter out = new FileWriter(file.toFile()); | ||
g2.stream(out); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.