Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added OutputStream as svg sink #113

Merged
merged 4 commits into from Oct 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
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();
```

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`:
Expand Down
Expand Up @@ -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 {
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 {
SVGGraphics2D g2 = toSvgDocument(matrix, matrixToImageConfig);

Expand Down
8 changes: 8 additions & 0 deletions javase/src/main/java/net/glxn/qrgen/javase/QRCode.java
Expand Up @@ -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");
}
Expand Down
32 changes: 32 additions & 0 deletions javase/src/test/java/net/glxn/qrgen/javase/QRCodeTest.java
Expand Up @@ -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();
Expand All @@ -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));
Expand Down