forked from psuter/openwhisk-java-gradle
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Generate.java
38 lines (28 loc) · 1.07 KB
/
Generate.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package qr;
import java.io.*;
import java.util.Base64;
import com.google.gson.JsonObject;
import com.google.zxing.*;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
public class Generate {
public static JsonObject main(JsonObject args) throws Exception {
String property = "text";
String text = "Hello. Try with a 'text' value next time.";
if (args.has(property)) {
text = args.get(property).toString();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream b64os = Base64.getEncoder().wrap(baos);
BitMatrix matrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, 300, 300);
MatrixToImageWriter.writeToStream(matrix, "png", b64os);
b64os.close();
String output = baos.toString("utf-8");
JsonObject response = new JsonObject();
JsonObject headers = new JsonObject();
headers.addProperty("content-type", "image/png; charset=UTF-8");
response.add("headers", headers);
response.addProperty("body", output);
return response;
}
}