Permalink
Browse files

Merge pull request #113 from tr00per/master

Added OutputStream as svg sink
  • Loading branch information...
kenglxn committed Oct 14, 2018
2 parents 7bb6f68 + 6e7da5a commit 1d84899c5cee01fb2b329512be7d96cb22a4714d
View
@@ -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`:
@@ -10,6 +10,8 @@
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 +46,13 @@ private static SVGGraphics2D toSvgDocument(BitMatrix matrix, MatrixToImageConfig
}
static void writeToStream(BitMatrix matrix, OutputStream outs, MatrixToImageConfig matrixToImageConfig) throws IOException {
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).startsWith("<?xml".getBytes());
assertThat(data).endsWith("</svg\n>\n".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).startsWith("<?xml".getBytes());
assertThat(data).endsWith("</svg\n>\n".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).startsWith("<?xml".getBytes());
assertThat(data).endsWith("</svg\n>\n".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 1d84899

Please sign in to comment.