Android™ Requires Chilkat v11.7.0+
Android™
Generate a QR Code PNG File
See more QR Code Examples
Demonstrates the simplest way to create a QR code: the text to encode goes in payload.text, and output.path names the PNG file to write. QrCode chooses the smallest QR version that holds the text at error-correction level M, and the response reports what it chose and the size of the image. The request and the response are both JSON objects, and they must be two different objects.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/");
// Each module (one of the small squares) is drawn 8 pixels wide. The default is 4.
json.UpdateInt("render.moduleSize",8);
// Write the image to a file. An existing file is replaced only when output.overwrite is true.
// Otherwise the call fails and the existing file is left as it was.
json.UpdateString("output.path","qa_output/qr_chilkat.png");
json.UpdateBool("output.overwrite",true);
// The image is also returned in the response as base64 unless output.return is "none".
json.UpdateString("output.return","none");
CkFormats fmt = new CkFormats();
CkJsonObject jsonOut = new CkJsonObject();
success = fmt.QrCode("generate",json,jsonOut);
if (success == false) {
Log.i(TAG, fmt.lastErrorText());
return;
}
Log.i(TAG, "file: " + jsonOut.stringOf("outputs[0].path"));
Log.i(TAG, "image: " + String.valueOf(jsonOut.IntOf("outputs[0].width")) + " x " + String.valueOf(jsonOut.IntOf("outputs[0].height")) + " pixels, " + String.valueOf(jsonOut.IntOf("outputs[0].byteCount"))
+ " bytes");
Log.i(TAG, "QR version: " + String.valueOf(jsonOut.IntOf("symbols[0].selectedEncoding.version")));
Log.i(TAG, "error correction: " + jsonOut.stringOf("symbols[0].selectedEncoding.errorCorrection"));
Log.i(TAG, "modules: " + String.valueOf(jsonOut.IntOf("symbols[0].dimensions.modulesWide")) + " x " + String.valueOf(jsonOut.IntOf("symbols[0].dimensions.modulesHigh")));
// The image written to qa_output/qr_chilkat.png:
//
// Sample output:
// file: qa_output/qr_chilkat.png
// image: 296 x 296 pixels, 1251 bytes
// QR version: 3
// error correction: M
// modules: 29 x 29
}
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."
}
}