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

Validate a QR Code Request

See more QR Code Examples

Demonstrates the validate operation, which examines a request without producing anything. The scope says how far to look: definition checks the options, capacity also checks that the data fits a QR symbol, and layout also checks the rendered size against the limits. The response has a validationStatus of valid or invalid, a list of checks, and findings[] describing what is wrong. A request that cannot be understood at all, such as one with a misspelled option, still makes the call fail in the usual way.

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 jsonOut = new CkJsonObject();
    int i;
    int n;

    CkJsonObject json = new CkJsonObject();
    json.UpdateString("target","symbol");
    json.UpdateString("validation.scope","capacity");
    json.UpdateString("payload.text","https://www.chilkatsoft.com/barcode_overview.asp");

    success = fmt.QrCode("validate",json,jsonOut);
    if (success == false) {
        Log.i(TAG, fmt.lastErrorText());
        return;
        }

    Log.i(TAG, "status: " + jsonOut.stringOf("validationStatus"));
    n = jsonOut.SizeOfArray("checks");
    for (i = 0; i <= n - 1; i++) {
        jsonOut.put_I(i);
        Log.i(TAG, "  " + jsonOut.stringOf("checks[i].name") + ": " + jsonOut.stringOf("checks[i].status"));
        }

    //  The same text does not fit version 2 at error-correction level H.
    json.UpdateInt("encoding.qr.maxVersion",2);
    json.UpdateString("encoding.qr.errorCorrection","H");

    success = fmt.QrCode("validate",json,jsonOut);
    if (success == false) {
        Log.i(TAG, fmt.lastErrorText());
        return;
        }

    Log.i(TAG, "status: " + jsonOut.stringOf("validationStatus"));
    n = jsonOut.SizeOfArray("checks");
    for (i = 0; i <= n - 1; i++) {
        jsonOut.put_I(i);
        Log.i(TAG, "  " + jsonOut.stringOf("checks[i].name") + ": " + jsonOut.stringOf("checks[i].status"));
        }

    n = jsonOut.SizeOfArray("findings");
    for (i = 0; i <= n - 1; i++) {
        jsonOut.put_I(i);
        Log.i(TAG, "finding: " + jsonOut.stringOf("findings[i].code") + " at " + jsonOut.stringOf("findings[i].path"));
        Log.i(TAG, "  " + jsonOut.stringOf("findings[i].message"));
        }

    //  Sample output:

    //  status: valid
    //    definition: passed
    //    capacity: passed
    //    layout: notApplicable
    //  status: invalid
    //    definition: passed
    //    capacity: failed
    //    layout: notApplicable
    //  finding: capacityExceeded at /payload
    //    the payload does not fit any admitted QR version at the selected error-correction level

  }

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