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

Create a vCard (Contact) QR Code

See more QR Code Examples

Demonstrates a QR code that adds a contact to a phone when scanned. The text is a vCard, the standard format for contact details: lines between BEGIN:VCARD and END:VCARD, each ending with CRLF. A vCard is fairly long, so the symbol is larger than one holding a web address. Leave out the fields that are not needed to keep it small.

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 second argument of AppendLine is cktrue to end each line with CRLF.
    CkStringBuilder sbCard = new CkStringBuilder();
    sbCard.AppendLine("BEGIN:VCARD",true);
    sbCard.AppendLine("VERSION:3.0",true);
    sbCard.AppendLine("N:Smith;Alex;;;",true);
    sbCard.AppendLine("FN:Alex Smith",true);
    sbCard.AppendLine("ORG:Example Company",true);
    sbCard.AppendLine("TITLE:Sales Manager",true);
    sbCard.AppendLine("TEL;TYPE=CELL:+1-555-0100",true);
    sbCard.AppendLine("EMAIL:alex.smith@example.com",true);
    sbCard.AppendLine("URL:https://www.example.com",true);
    sbCard.AppendLine("END:VCARD",true);

    CkJsonObject json = new CkJsonObject();
    json.UpdateSb("payload.text",sbCard);
    json.UpdateInt("render.moduleSize",6);
    json.UpdateString("output.path","qa_output/qr_vcard.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, "vCard length: " + String.valueOf(sbCard.get_Length()));
    Log.i(TAG, "QR version: " + String.valueOf(jsonOut.IntOf("symbols[0].selectedEncoding.version")));
    Log.i(TAG, "modules: " + String.valueOf(jsonOut.IntOf("symbols[0].dimensions.modulesWide")) + " x " + String.valueOf(jsonOut.IntOf("symbols[0].dimensions.modulesHigh")));
    Log.i(TAG, "image: " + String.valueOf(jsonOut.IntOf("outputs[0].width")) + " x " + String.valueOf(jsonOut.IntOf("outputs[0].height")) + " pixels");

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

    //  image

    //  Sample output:

    //  vCard length: 197
    //  QR version: 10
    //  modules: 57 x 57
    //  image: 390 x 390 pixels

  }

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