Java Requires Chilkat v11.7.0+
Chilkat2 Java
Generate a QR Code as SVG
See more QR Code Examples
Demonstrates SVG output. An SVG is a vector image, so it stays sharp at any size, which suits print and responsive web pages. Setting output.format to svg and output.return to text returns the SVG document as a string in outputs[0].text. The SVG can also be written straight to a file with output.path, the same as a PNG.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Formats;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;
public class ChilkatExample {
public static void main(String[] args) {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
JsonObject json = new JsonObject();
json.updateString("payload.text", "https://www.chilkatsoft.com/");
json.updateString("output.format", "svg");
// An SVG is text, so it can be returned as text rather than as base64.
json.updateString("output.return", "text");
Formats fmt = new Formats();
JsonObject jsonOut = new JsonObject();
try {
fmt.qrCode("generate", json, jsonOut);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
StringBuilder sbSvg = new StringBuilder();
jsonOut.stringOfSb("outputs[0].text", sbSvg);
System.out.println("MIME type: " + jsonOut.stringOf("outputs[0].mimeType"));
System.out.println("size: " + jsonOut.intOf("outputs[0].width") + " x " + jsonOut.intOf("outputs[0].height") + " " + jsonOut.stringOf("outputs[0].units"));
System.out.println("SVG length: " + sbSvg.getLength() + " characters");
// The SVG is a complete document that can be saved, or placed inline in an HTML page.
try {
sbSvg.writeFile("qa_output/qr_chilkat.svg", "utf-8", false);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// The SVG:
//
// Sample output:
// MIME type: image/svg+xml
// size: 148 x 148 px
// SVG length: 12500 characters
}
}