Sample code for 30+ languages & platforms
React Native Requires Chilkat v11.7.0+

Measure a QR Code without Rendering It

See more QR Code Examples

Demonstrates the measure operation, which does everything generate does except draw the image. It reports the version chosen and the exact size the image would have, which is useful for laying out a page or a label before any image is made. Unlike generate, a code that does not fit the limits is not a failure for measure: the call succeeds, fits is false, and reasons[] says why.

Chilkat React Native Downloads

React Native
import { Formats, JsonObject } from '@chilkat/react-native'

function chilkatExample() {
  // This example requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  const json = new JsonObject();
  json.updateString('payload.text', 'https://www.chilkatsoft.com/barcode_overview.asp');
  json.updateInt('render.moduleSize', 8);

  const fmt = new Formats();
  const jsonOut = new JsonObject();
  try {
    fmt.qrCode('measure', json, jsonOut);
  } catch {
    console.log(fmt.lastErrorText);
    return;
  }

  console.log(`fits: ${jsonOut.boolOf('fits')}`);
  console.log(`QR version: ${jsonOut.intOf('symbols[0].selectedEncoding.version')}`);
  console.log(`image would be: ${jsonOut.intOf('measurements[0].canvasWidth')} x ${jsonOut.intOf('measurements[0].canvasHeight')} pixels`);
  console.log(`symbol: ${jsonOut.intOf('measurements[0].symbolWidth')} pixels wide at x = ${jsonOut.intOf('measurements[0].symbolX')}`);

  // Would it fit in a space 200 pixels wide?
  json.updateInt('render.maxWidth', 200);
  try {
    fmt.qrCode('measure', json, jsonOut);
  } catch {
    console.log(fmt.lastErrorText);
    return;
  }

  console.log(`within 200 pixels, fits: ${jsonOut.boolOf('fits')}`);
  if (!jsonOut.boolOf('fits')) {
    console.log(`reason: ${jsonOut.stringOf('reasons[0].code')} (${jsonOut.stringOf('reasons[0].message')})`);
  }

  // What measure reported, drawn as a diagram.  The measure operation itself makes no image:

  // image

  // Sample output:

  // fits: True
  // QR version: 4
  // image would be: 328 x 328 pixels
  // symbol: 264 pixels wide at x = 32
  // within 200 pixels, fits: False
  // reason: noFit (exceedsMaxWidth)
}