Permalink
Browse files

Added OutputStream as svg sink

closes #112
  • Loading branch information...
tr00per committed Oct 8, 2018
1 parent 7bb6f68 commit a66cabc5c4715d51f6f2660cc22bc2a958dd784f
@@ -201,6 +201,13 @@ File file = QRCode.from("www.example.org").svg();
File file = QRCode.from("www.example.com").withSize(250, 250).withColor(30, 90).svg();
```
It's also possible to write svg to an `OutputStream` with terminal operation:
```java
OutputStream outs = // ...
QRCode.from("www.example.org").svg(outs);
```
#### Android only
On Android you have a special method `bitmap()` which returns a `android.graphics.Bitmap` without creating a `File` object before, so you can use the generated `android.graphics.Bitmap` immediately inside an `ImageView`:
@@ -4,12 +4,15 @@
import com.google.zxing.common.BitMatrix;
import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.svggen.SVGGraphics2DIOException;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import java.awt.*;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.file.Path;
class MatrixToSvgWriter {
@@ -44,6 +47,13 @@ private static SVGGraphics2D toSvgDocument(BitMatrix matrix, MatrixToImageConfig
}
static void writeToStream(BitMatrix matrix, OutputStream outs, MatrixToImageConfig matrixToImageConfig) throws SVGGraphics2DIOException {
SVGGraphics2D g2 = toSvgDocument(matrix, matrixToImageConfig);
OutputStreamWriter out = new OutputStreamWriter(outs);
g2.stream(out);
}
static void writeToPath(BitMatrix matrix, Path file, MatrixToImageConfig matrixToImageConfig) throws IOException {
SVGGraphics2D g2 = toSvgDocument(matrix, matrixToImageConfig);
@@ -172,6 +172,14 @@ public File svg(String name) {
return file;
}
public void svg(OutputStream outs) {
try {
MatrixToSvgWriter.writeToStream(createMatrix(text), outs, matrixToImageConfig);
} catch (Exception e) {
throw new QRGenerationException("Failed to create QR svg from text due to underlying exception", e);
}
}
private File createTempSvgFile() throws IOException {
return createTempSvgFile("QRCode");
}
@@ -230,13 +230,15 @@ public void shouldGetSvgFromText() {
assertThat(file).canRead();
assertThat(file.length()).isGreaterThan(0);
}
@Test
public void shouldGetSvgWithSizeFromText() {
File file = QRCode.from("www.example.com").withSize(250, 250).svg();
assertThat(file).exists();
assertThat(file).canRead();
assertThat(file.length()).isGreaterThan(0);
}
@Test
public void shouldGetSvgWithSizeAndColorFromText() {
File file = QRCode.from("www.example.com").withSize(250, 250).withColor(30, 90).svg();
@@ -245,6 +247,36 @@ public void shouldGetSvgWithSizeAndColorFromText() {
assertThat(file.length()).isGreaterThan(0);
}
@Test
public void shouldGetSvgFromTextAsStream() {
ByteArrayOutputStream outs = new ByteArrayOutputStream();
QRCode.from("www.example.org").svg(outs);
byte[] data = outs.toByteArray();
assertThat(data).isNotEmpty();
assertThat(data.length).isGreaterThan(0);
assertThat(data).startsWith("<?xml".getBytes());
}
@Test
public void shouldGetSvgWithSizeFromTextAsStream() {
ByteArrayOutputStream outs = new ByteArrayOutputStream();
QRCode.from("www.example.com").withSize(250, 250).svg(outs);
byte[] data = outs.toByteArray();
assertThat(data).isNotEmpty();
assertThat(data.length).isGreaterThan(0);
assertThat(data).startsWith("<?xml".getBytes());
}
@Test
public void shouldGetSvgWithSizeAndColorFromTextAsStream() {
ByteArrayOutputStream outs = new ByteArrayOutputStream();
QRCode.from("www.example.com").withSize(250, 250).withColor(30, 90).svg(outs);
byte[] data = outs.toByteArray();
assertThat(data).isNotEmpty();
assertThat(data.length).isGreaterThan(0);
assertThat(data).startsWith("<?xml".getBytes());
}
@SuppressWarnings("unchecked")
private void assertCapturedHint(Object expected, Object[] capture, EncodeHintType type) {
Assert.assertEquals(expected, ((Map<EncodeHintType, ?>) capture[0]).get(type));

0 comments on commit a66cabc

Please sign in to comment.