Sample code for 30+ languages & platforms
Android™ Requires Chilkat v11.7.0+

Generate a QR Code PNG in Memory

See more QR Code Examples

Demonstrates getting a QR code image without writing a file. With no output options the result is a PNG returned as base64 in outputs[0].base64 of the response. The example decodes it into a BinData, which holds the bytes of the PNG to send in an HTTP response, store in a database, attach to an email, or save.

Chilkat Android™ Downloads

Android™
// 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.UpdateInt("render.moduleSize",6);

    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, "format: " + jsonOut.stringOf("outputs[0].format"));
    Log.i(TAG, "MIME type: " + jsonOut.stringOf("outputs[0].mimeType"));
    Log.i(TAG, "image: " + String.valueOf(jsonOut.IntOf("outputs[0].width")) + " x " + String.valueOf(jsonOut.IntOf("outputs[0].height")) + " pixels");

    //  outputs[0].base64 is the PNG file, base64 encoded.
    CkBinData bdPng = new CkBinData();
    success = bdPng.AppendEncoded(jsonOut.stringOf("outputs[0].base64"),"base64");
    if (success == false) {
        Log.i(TAG, bdPng.lastErrorText());
        return;
        }

    Log.i(TAG, "PNG bytes: " + String.valueOf(bdPng.get_NumBytes()));

    //  The BinData holds the image.  Here it is simply saved.
    success = bdPng.WriteFile("qa_output/qr_in_memory.png");
    if (success == false) {
        Log.i(TAG, bdPng.lastErrorText());
        return;
        }

    //  The PNG held in bdPng:

    //  image

    //  Sample output:

    //  format: png
    //  MIME type: image/png
    //  image: 222 x 222 pixels
    //  PNG bytes: 877

  }

  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."
  }
}