Java Requires Chilkat v11.7.0+
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.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
boolean success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkJsonObject json = new CkJsonObject();
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");
CkFormats fmt = new CkFormats();
CkJsonObject jsonOut = new CkJsonObject();
success = fmt.QrCode("generate",json,jsonOut);
if (success == false) {
System.out.println(fmt.lastErrorText());
return;
}
CkStringBuilder sbSvg = new CkStringBuilder();
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.get_Length() + " characters");
// The SVG is a complete document that can be saved, or placed inline in an HTML page.
success = sbSvg.WriteFile("qa_output/qr_chilkat.svg","utf-8",false);
if (success == false) {
System.out.println(sbSvg.lastErrorText());
return;
}
// The SVG:
//
// Sample output:
// MIME type: image/svg+xml
// size: 148 x 148 px
// SVG length: 12500 characters
}
}