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

Handle QR Code Errors

See more QR Code Examples

Demonstrates what happens when a request is refused. QrCode returns false and the response holds errors[0] with three members: a code that does not change between versions, the path of the request member at fault (a JSON Pointer such as /encoding/qr/version), and a message. Requests are checked strictly, so a misspelled option is an error rather than being silently ignored. LastErrorText has the same information in more detail.

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();

    //  1. A misspelled option: "moduleSise" for "moduleSize".
    json.UpdateString("payload.text","https://www.chilkatsoft.com/");
    json.UpdateInt("render.moduleSise",8);
    success = fmt.QrCode("generate",json,jsonOut);
    if (success == false) {
        Log.i(TAG, "code: " + jsonOut.stringOf("errors[0].code"));
        Log.i(TAG, "path: " + jsonOut.stringOf("errors[0].path"));
        Log.i(TAG, "message: " + jsonOut.stringOf("errors[0].message"));
        }

    //  2. A value out of range: QR versions are 1 to 40.
    json.Clear();
    json.UpdateString("payload.text","https://www.chilkatsoft.com/");
    json.UpdateInt("encoding.qr.version",41);
    success = fmt.QrCode("generate",json,jsonOut);
    if (success == false) {
        Log.i(TAG, "code: " + jsonOut.stringOf("errors[0].code"));
        Log.i(TAG, "path: " + jsonOut.stringOf("errors[0].path"));
        Log.i(TAG, "message: " + jsonOut.stringOf("errors[0].message"));
        }

    //  3. Too much data: this text does not fit a version 1 symbol.
    json.Clear();
    json.UpdateString("payload.text","https://www.chilkatsoft.com/barcode_overview.asp");
    json.UpdateInt("encoding.qr.version",1);
    success = fmt.QrCode("generate",json,jsonOut);
    if (success == false) {
        Log.i(TAG, "code: " + jsonOut.stringOf("errors[0].code"));
        Log.i(TAG, "path: " + jsonOut.stringOf("errors[0].path"));
        Log.i(TAG, "message: " + jsonOut.stringOf("errors[0].message"));
        }

    //  A program can act on the code.
    if (jsonOut.stringOf("errors[0].code") == "capacityExceeded") {
        Log.i(TAG, "The text is too long for the symbol size allowed.");
        }

    //  Sample output:

    //  code: unknownOption
    //  path: /render/moduleSise
    //  message: unknown option
    //  code: outOfRange
    //  path: /encoding/qr/version
    //  message: the value is out of range
    //  code: capacityExceeded
    //  path: /payload
    //  message: the payload does not fit the requested QR version at the requested error-correction level
    //  The text is too long for the symbol size allowed.

  }

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