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

QR Code with a Physical Size in Millimeters at a Printer DPI

See more QR Code Examples

Demonstrates sizing a QR code for print. With render.units set to mm (or in, or pt) the module size is a physical length, and render.dpi says how many pixels the printer or the image has per inch. A module is always a whole number of pixels, because modules of uneven width scan badly, so the size asked for is rounded to the nearest pixel and the response reports the size actually used. The PNG records the dpi, so software that honors it prints the image at that physical size.

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.

    CkJsonObject json = new CkJsonObject();
    json.UpdateString("payload.text","https://www.chilkatsoft.com/");

    //  Modules of 0.5 mm, for a 300 dpi printer.
    json.UpdateString("render.units","mm");
    json.UpdateNumber("render.moduleSize","0.5");
    json.UpdateInt("render.dpi",300);

    json.UpdateString("output.path","qa_output/qr_300dpi.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;
        }

    //  0.5 mm at 300 dpi is 5.9 pixels, so each module is drawn 6 pixels wide.
    Log.i(TAG, "requested module: " + jsonOut.stringOf("measurements[0].requestedPitch") + " pixels");
    Log.i(TAG, "module drawn: " + String.valueOf(jsonOut.IntOf("measurements[0].pitch")) + " pixels = " + jsonOut.stringOf("measurements[0].moduleSize") + " mm");
    Log.i(TAG, "size was rounded: " + String.valueOf(jsonOut.BoolOf("measurements[0].pitchAdjusted")));
    Log.i(TAG, "image: " + String.valueOf(jsonOut.IntOf("measurements[0].canvasWidth")) + " x " + String.valueOf(jsonOut.IntOf("measurements[0].canvasHeight")) + " pixels");
    Log.i(TAG, "printed size: " + jsonOut.stringOf("measurements[0].physicalWidth") + " x " + jsonOut.stringOf("measurements[0].physicalHeight") + " mm");

    //  The image.  A web page shows it pixel for pixel.  Printed at 300 dpi it is 18.8 mm wide:

    //  image

    //  When the exact size matters more than getting an image, requireExactPitch makes a size
    //  that is not a whole number of pixels a failure (noFit) instead of rounding it.
    json.UpdateBool("render.requireExactPitch",true);
    success = fmt.QrCode("generate",json,jsonOut);
    if (success == false) {
        Log.i(TAG, "requireExactPitch: " + jsonOut.stringOf("errors[0].code") + " (" + jsonOut.stringOf("errors[0].message") + ")");
        }

    //  Sample output:

    //  requested module: 5.905512 pixels
    //  module drawn: 6 pixels = 0.508 mm
    //  size was rounded: True
    //  image: 222 x 222 pixels
    //  printed size: 18.796 x 18.796 mm
    //  requireExactPitch: noFit (the symbol does not fit the render constraints: pitchAdjusted)

  }

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