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

QR Code Error Correction Levels

See more QR Code Examples

Demonstrates the four QR error-correction levels. L, M, Q and H allow a reader to recover the data when about 7%, 15%, 25% and 30% of the symbol is damaged or covered. More error correction leaves less room for data, so the same text needs a larger symbol at a higher level. The default is M. The example also shows boostErrorCorrection, which raises the level for free when the chosen version has room to spare.

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.

    CkFormats fmt = new CkFormats();
    CkJsonObject json = new CkJsonObject();
    CkJsonObject jsonOut = new CkJsonObject();
    json.UpdateString("payload.text","https://www.chilkatsoft.com/barcode_overview.asp");

    //  Encode the same text at each level.
    CkJsonArray levels = new CkJsonArray();
    levels.Load("[\"L\",\"M\",\"Q\",\"H\"]");
    int i;
    for (i = 0; i <= 3; i++) {
        json.UpdateString("encoding.qr.errorCorrection",levels.stringAt(i));
        success = fmt.QrCode("generate",json,jsonOut);
        if (success == false) {
            Log.i(TAG, fmt.lastErrorText());
            return;
            }

        Log.i(TAG, "level " + levels.stringAt(i) + ": version " + String.valueOf(jsonOut.IntOf("symbols[0].selectedEncoding.version")) + ", " + String.valueOf(jsonOut.IntOf("symbols[0].dimensions.modulesWide"))
             + " modules wide");
        }

    //  The four symbols.  They hold the same text, and the higher the level, the larger the symbol:

    //  image

    //  A symbol often has room left over.  boostErrorCorrection uses it: the level is raised
    //  as far as the same version allows, so the symbol is no larger but is more robust.
    CkJsonObject json2 = new CkJsonObject();
    json2.UpdateString("payload.text","https://chilkatsoft.com");
    json2.UpdateString("encoding.qr.errorCorrection","L");
    success = fmt.QrCode("generate",json2,jsonOut);
    if (success == false) {
        Log.i(TAG, fmt.lastErrorText());
        return;
        }

    Log.i(TAG, "level L requested: version " + String.valueOf(jsonOut.IntOf("symbols[0].selectedEncoding.version")) + ", level " + jsonOut.stringOf("symbols[0].selectedEncoding.errorCorrection"));

    json2.UpdateBool("encoding.qr.boostErrorCorrection",true);
    success = fmt.QrCode("generate",json2,jsonOut);
    if (success == false) {
        Log.i(TAG, fmt.lastErrorText());
        return;
        }

    Log.i(TAG, "level L with boost: version " + String.valueOf(jsonOut.IntOf("symbols[0].selectedEncoding.version")) + ", level " + jsonOut.stringOf("symbols[0].selectedEncoding.errorCorrection"));

    //  Level L, and level M reached by the boost.  The two symbols are the same size:

    //  image

    //  Sample output:

    //  level L: version 3, 29 modules wide
    //  level M: version 4, 33 modules wide
    //  level Q: version 5, 37 modules wide
    //  level H: version 6, 41 modules wide
    //  level L requested: version 2, level L
    //  level L with boost: version 2, level M

  }

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