Android™ Requires Chilkat v11.7.0+
Android™
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 Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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) {
Log.i(TAG, fmt.lastErrorText());
return;
}
CkStringBuilder sbSvg = new CkStringBuilder();
jsonOut.StringOfSb("outputs[0].text",sbSvg);
Log.i(TAG, "MIME type: " + jsonOut.stringOf("outputs[0].mimeType"));
Log.i(TAG, "size: " + String.valueOf(jsonOut.IntOf("outputs[0].width")) + " x " + String.valueOf(jsonOut.IntOf("outputs[0].height")) + " " + jsonOut.stringOf("outputs[0].units"));
Log.i(TAG, "SVG length: " + String.valueOf(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) {
Log.i(TAG, sbSvg.lastErrorText());
return;
}
// The SVG:
//
// Sample output:
// MIME type: image/svg+xml
// size: 148 x 148 px
// SVG length: 12500 characters
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}