Android™ Requires Chilkat v11.7.0+
Android™
Embed a QR Code in HTML as a Data URI
See more QR Code Examples
Demonstrates putting a QR code directly into an HTML page or HTML email with no separate image file. The base64 PNG returned by QrCode becomes the src of an img tag as a data URI, which is the MIME type from outputs[0].mimeType followed by the base64 from outputs[0].base64.Chilkat Android™ Downloads
// 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/");
json.UpdateInt("render.moduleSize",5);
CkFormats fmt = new CkFormats();
CkJsonObject jsonOut = new CkJsonObject();
success = fmt.QrCode("generate",json,jsonOut);
if (success == false) {
Log.i(TAG, fmt.lastErrorText());
return;
}
// A data URI has the form data:<MIME type>;base64,<base64 data>
CkStringBuilder sbHtml = new CkStringBuilder();
sbHtml.Append("<html><body><p>Scan to visit chilkatsoft.com</p><img alt=\"QR code\" width=\"");
sbHtml.AppendInt(jsonOut.IntOf("outputs[0].width"));
sbHtml.Append("\" height=\"");
sbHtml.AppendInt(jsonOut.IntOf("outputs[0].height"));
sbHtml.Append("\" src=\"data:");
sbHtml.Append(jsonOut.stringOf("outputs[0].mimeType"));
sbHtml.Append(";base64,");
sbHtml.Append(jsonOut.stringOf("outputs[0].base64"));
sbHtml.Append("\"></body></html>");
success = sbHtml.WriteFile("qa_output/qr_page.html","utf-8",false);
if (success == false) {
Log.i(TAG, sbHtml.lastErrorText());
return;
}
Log.i(TAG, "image: " + String.valueOf(jsonOut.IntOf("outputs[0].width")) + " x " + String.valueOf(jsonOut.IntOf("outputs[0].height")) + " pixels");
Log.i(TAG, "HTML length: " + String.valueOf(sbHtml.get_Length()));
// The page, as a browser shows it:
//
// Sample output:
// image: 185 x 185 pixels
// HTML length: 1211
}
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."
}
}