Skip to content

Commit

Permalink
Merge pull request #113 from tr00per/master
Browse files Browse the repository at this point in the history
Added OutputStream as svg sink
  • Loading branch information
kenglxn committed Oct 14, 2018
2 parents 7bb6f68 + 6e7da5a commit 1d84899
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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(); 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 #### 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`: 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`:
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.awt.*; import java.awt.*;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.file.Path; import java.nio.file.Path;


class MatrixToSvgWriter { class MatrixToSvgWriter {
Expand Down Expand Up @@ -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 { static void writeToPath(BitMatrix matrix, Path file, MatrixToImageConfig matrixToImageConfig) throws IOException {
SVGGraphics2D g2 = toSvgDocument(matrix, matrixToImageConfig); SVGGraphics2D g2 = toSvgDocument(matrix, matrixToImageConfig);


Expand Down
8 changes: 8 additions & 0 deletions javase/src/main/java/net/glxn/qrgen/javase/QRCode.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ public File svg(String name) {
return file; 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 { private File createTempSvgFile() throws IOException {
return createTempSvgFile("QRCode"); return createTempSvgFile("QRCode");
} }
Expand Down
32 changes: 32 additions & 0 deletions javase/src/test/java/net/glxn/qrgen/javase/QRCodeTest.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -230,13 +230,15 @@ public void shouldGetSvgFromText() {
assertThat(file).canRead(); assertThat(file).canRead();
assertThat(file.length()).isGreaterThan(0); assertThat(file.length()).isGreaterThan(0);
} }

@Test @Test
public void shouldGetSvgWithSizeFromText() { public void shouldGetSvgWithSizeFromText() {
File file = QRCode.from("www.example.com").withSize(250, 250).svg(); File file = QRCode.from("www.example.com").withSize(250, 250).svg();
assertThat(file).exists(); assertThat(file).exists();
assertThat(file).canRead(); assertThat(file).canRead();
assertThat(file.length()).isGreaterThan(0); assertThat(file.length()).isGreaterThan(0);
} }

@Test @Test
public void shouldGetSvgWithSizeAndColorFromText() { public void shouldGetSvgWithSizeAndColorFromText() {
File file = QRCode.from("www.example.com").withSize(250, 250).withColor(30, 90).svg(); File file = QRCode.from("www.example.com").withSize(250, 250).withColor(30, 90).svg();
Expand All @@ -245,6 +247,36 @@ public void shouldGetSvgWithSizeAndColorFromText() {
assertThat(file.length()).isGreaterThan(0); 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") @SuppressWarnings("unchecked")
private void assertCapturedHint(Object expected, Object[] capture, EncodeHintType type) { private void assertCapturedHint(Object expected, Object[] capture, EncodeHintType type) {
Assert.assertEquals(expected, ((Map<EncodeHintType, ?>) capture[0]).get(type)); Assert.assertEquals(expected, ((Map<EncodeHintType, ?>) capture[0]).get(type));
Expand Down

0 comments on commit 1d84899

Please sign in to comment.