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

QR Code with Binary Data

See more QR Code Examples

Demonstrates a QR code that holds arbitrary bytes rather than text. The payload type is binary and the bytes are given as base64 in payload.base64. Any byte value may be used, including zero. Keep in mind that many phone camera apps expect text, so binary payloads are for readers that you control.

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.

    //  The bytes to encode.  Here they come from hex, but they could be loaded from a file with LoadFile.
    CkBinData bd = new CkBinData();
    success = bd.AppendEncoded("000102030405FF00FE4368696C6B6174","hex");
    if (success == false) {
        Log.i(TAG, bd.lastErrorText());
        return;
        }

    CkJsonObject json = new CkJsonObject();
    json.UpdateString("payload.type","binary");
    json.UpdateString("payload.base64",bd.getEncoded("base64"));
    json.UpdateString("output.path","qa_output/qr_binary.png");
    json.UpdateBool("output.overwrite",true);
    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, "bytes given: " + String.valueOf(bd.get_NumBytes()));
    Log.i(TAG, "bytes encoded: " + String.valueOf(jsonOut.IntOf("symbols[0].payloadBytes")));
    Log.i(TAG, "mode: " + jsonOut.stringOf("symbols[0].selectedEncoding.mode"));
    Log.i(TAG, "QR version: " + String.valueOf(jsonOut.IntOf("symbols[0].selectedEncoding.version")));

    //  The image written to qa_output/qr_binary.png:

    //  image

    //  Sample output:

    //  bytes given: 16
    //  bytes encoded: 16
    //  mode: byte
    //  QR version: 2

  }

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