java - How to create and show Qr Code in Android App -
this question has answer here:
can me please. want write small app android has 1 button. when push it must show qr code. qr code must generated form string.
tryed far:
public void onclick(view v){ qrcodeencoder qrcodeencoder = new qrcodeencoder("hello", null, contents.type.text, barcodeformat.qr_code.tostring(), smallerdimension); bitmap bitmap = qrcodeencoder.encodeasbitmap(); }
first need add zing2.1.jar file in project below code
qrcodeencoder.java
public final class qrcodeencoder { private static final int white = 0xffffffff; private static final int black = 0xff000000; private int dimension = integer.min_value; private string contents = null; private string displaycontents = null; private string title = null; private barcodeformat format = null; private boolean encoded = false; public qrcodeencoder(string data, bundle bundle, string type, string format, int dimension) { this.dimension = dimension; encoded = encodecontents(data, bundle, type, format); } public string getcontents() { return contents; } public string getdisplaycontents() { return displaycontents; } public string gettitle() { return title; } private boolean encodecontents(string data, bundle bundle, string type, string formatstring) { // default qr_code if no format given. format = null; if (formatstring != null) { try { format = barcodeformat.valueof(formatstring); } catch (illegalargumentexception iae) { // ignore } } if (format == null || format == barcodeformat.qr_code) { this.format = barcodeformat.qr_code; encodeqrcodecontents(data, bundle, type); } else if (data != null && data.length() > 0) { contents = data; displaycontents = data; title = "text"; } return contents != null && contents.length() > 0; } private void encodeqrcodecontents(string data, bundle bundle, string type) { if (type.equals(contents.type.text)) { if (data != null && data.length() > 0) { contents = data; displaycontents = data; title = "text"; } } else if (type.equals(contents.type.email)) { data = trim(data); if (data != null) { contents = "mailto:" + data; displaycontents = data; title = "e-mail"; } } else if (type.equals(contents.type.phone)) { data = trim(data); if (data != null) { contents = "tel:" + data; displaycontents = phonenumberutils.formatnumber(data); title = "phone"; } } else if (type.equals(contents.type.sms)) { data = trim(data); if (data != null) { contents = "sms:" + data; displaycontents = phonenumberutils.formatnumber(data); title = "sms"; } } else if (type.equals(contents.type.contact)) { if (bundle != null) { stringbuilder newcontents = new stringbuilder(100); stringbuilder newdisplaycontents = new stringbuilder(100); newcontents.append("mecard:"); string name = trim(bundle.getstring(contactscontract.intents.insert.name)); if (name != null) { newcontents.append("n:").append(escapemecard(name)).append(';'); newdisplaycontents.append(name); } string address = trim(bundle.getstring(contactscontract.intents.insert.postal)); if (address != null) { newcontents.append("adr:").append(escapemecard(address)).append(';'); newdisplaycontents.append('\n').append(address); } collection<string> uniquephones = new hashset<string>(contents.phone_keys.length); (int x = 0; x < contents.phone_keys.length; x++) { string phone = trim(bundle.getstring(contents.phone_keys[x])); if (phone != null) { uniquephones.add(phone); } } (string phone : uniquephones) { newcontents.append("tel:").append(escapemecard(phone)).append(';'); newdisplaycontents.append('\n').append(phonenumberutils.formatnumber(phone)); } collection<string> uniqueemails = new hashset<string>(contents.email_keys.length); (int x = 0; x < contents.email_keys.length; x++) { string email = trim(bundle.getstring(contents.email_keys[x])); if (email != null) { uniqueemails.add(email); } } (string email : uniqueemails) { newcontents.append("email:").append(escapemecard(email)).append(';'); newdisplaycontents.append('\n').append(email); } string url = trim(bundle.getstring(contents.url_key)); if (url != null) { // escapemecard(url) -> wrong escape e.g. http\://zxing.google.com newcontents.append("url:").append(url).append(';'); newdisplaycontents.append('\n').append(url); } string note = trim(bundle.getstring(contents.note_key)); if (note != null) { newcontents.append("note:").append(escapemecard(note)).append(';'); newdisplaycontents.append('\n').append(note); } // make sure we've encoded @ least 1 field. if (newdisplaycontents.length() > 0) { newcontents.append(';'); contents = newcontents.tostring(); displaycontents = newdisplaycontents.tostring(); title = "contact"; } else { contents = null; displaycontents = null; } } } else if (type.equals(contents.type.location)) { if (bundle != null) { // these must use bundle.getfloat(), not getdouble(), it's part of api. float latitude = bundle.getfloat("lat", float.max_value); float longitude = bundle.getfloat("long", float.max_value); if (latitude != float.max_value && longitude != float.max_value) { contents = "geo:" + latitude + ',' + longitude; displaycontents = latitude + "," + longitude; title = "location"; } } } } public bitmap encodeasbitmap() throws writerexception { if (!encoded) return null; map<encodehinttype, object> hints = null; string encoding = guessappropriateencoding(contents); if (encoding != null) { hints = new enummap<encodehinttype, object>(encodehinttype.class); hints.put(encodehinttype.character_set, encoding); } multiformatwriter writer = new multiformatwriter(); bitmatrix result = writer.encode(contents, format, dimension, dimension, hints); int width = result.getwidth(); int height = result.getheight(); int[] pixels = new int[width * height]; // 0, or black, default (int y = 0; y < height; y++) { int offset = y * width; (int x = 0; x < width; x++) { pixels[offset + x] = result.get(x, y) ? black : white; } } bitmap bitmap = bitmap.createbitmap(width, height, bitmap.config.argb_8888); bitmap.setpixels(pixels, 0, width, 0, 0, width, height); return bitmap; } private static string guessappropriateencoding(charsequence contents) { // crude @ moment (int = 0; < contents.length(); i++) { if (contents.charat(i) > 0xff) { return "utf-8"; } } return null; } private static string trim(string s) { if (s == null) { return null; } string result = s.trim(); return result.length() == 0 ? null : result; } private static string escapemecard(string input) { if (input == null || (input.indexof(':') < 0 && input.indexof(';') < 0)) { return input; } int length = input.length(); stringbuilder result = new stringbuilder(length); (int = 0; < length; i++) { char c = input.charat(i); if (c == ':' || c == ';') { result.append('\\'); } result.append(c); } return result.tostring(); } }
contents.java
import android.provider.contactscontract; public final class contents { private contents() { } public static final class type { // plain text. use intent.putextra(data, string). can used urls too, string // must include "http://" or "https://". public static final string text = "text_type"; // email type. use intent.putextra(data, string) string email address. public static final string email = "email_type"; // use intent.putextra(data, string) string phone number call. public static final string phone = "phone_type"; // sms type. use intent.putextra(data, string) string number sms. public static final string sms = "sms_type"; // contact. send request encode follows: // <p/> // import android.provider.contacts; // <p/> // intent intent = new intent(intents.encode.action); intent.putextra(intents.encode.type, // contact); bundle bundle = new bundle(); bundle.putstring(contacts.intents.insert.name, // "jenny"); bundle.putstring(contacts.intents.insert.phone, "8675309"); // bundle.putstring(contacts.intents.insert.email, "jenny@the80s.com"); // bundle.putstring(contacts.intents.insert.postal, "123 fake st. san francisco, ca 94102"); // intent.putextra(intents.encode.data, bundle); public static final string contact = "contact_type"; // geographic location. use follows: // bundle bundle = new bundle(); // bundle.putfloat("lat", latitude); // bundle.putfloat("long", longitude); // intent.putextra(intents.encode.data, bundle); public static final string location = "location_type"; private type() { } } public static final string url_key = "url_key"; public static final string note_key = "note_key"; // when using type.contact, these arrays provide keys adding or retrieving multiple // phone numbers , addresses. public static final string[] phone_keys = { contactscontract.intents.insert.phone, contactscontract.intents.insert.secondary_phone, contactscontract.intents.insert.tertiary_phone }; public static final string[] phone_type_keys = { contactscontract.intents.insert.phone_type, contactscontract.intents.insert.secondary_phone_type, contactscontract.intents.insert.tertiary_phone_type }; public static final string[] email_keys = { contactscontract.intents.insert.email, contactscontract.intents.insert.secondary_email, contactscontract.intents.insert.tertiary_email }; public static final string[] email_type_keys = { contactscontract.intents.insert.email_type, contactscontract.intents.insert.secondary_email_type, contactscontract.intents.insert.tertiary_email_type }; }
do below code add string , set imageview
string qrdata = "name : "+name+"\n company : "+comp; int qrcodedimention = 500; qrcodeencoder qrcodeencoder = new qrcodeencoder(qrdata, null, contents.type.text, barcodeformat.qr_code.tostring(), qrcodedimention); try { bitmap bitmap = qrcodeencoder.encodeasbitmap(); imageview.setimagebitmap(bitmap); } catch (writerexception e) { e.printstacktrace(); }
Comments
Post a Comment