2 * Copyright (C) 2008 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.client.android.encode;
19 import com.google.zxing.BarcodeFormat;
20 import com.google.zxing.EncodeHintType;
21 import com.google.zxing.MultiFormatWriter;
22 import com.google.zxing.Result;
23 import com.google.zxing.WriterException;
24 import com.google.zxing.client.android.Contents;
25 import com.google.zxing.client.android.Intents;
26 import com.google.zxing.client.android.R;
27 import com.google.zxing.client.result.AddressBookParsedResult;
28 import com.google.zxing.client.result.ParsedResult;
29 import com.google.zxing.client.result.ResultParser;
30 import com.google.zxing.common.BitMatrix;
32 import android.app.Activity;
33 import android.content.Intent;
34 import android.graphics.Bitmap;
35 import android.net.Uri;
36 import android.os.Bundle;
37 import android.os.Handler;
38 import android.provider.Contacts;
39 import android.telephony.PhoneNumberUtils;
40 import android.util.Log;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.util.Hashtable;
47 * This class does the work of decoding the user's request and extracting all the data
48 * to be encoded in a barcode.
50 * @author dswitkin@google.com (Daniel Switkin)
52 final class QRCodeEncoder {
54 private static final String TAG = QRCodeEncoder.class.getSimpleName();
56 private static final int WHITE = 0xFFFFFFFF;
57 private static final int BLACK = 0xFF000000;
59 private final Activity activity;
60 private String contents;
61 private String displayContents;
63 private BarcodeFormat format;
65 QRCodeEncoder(Activity activity, Intent intent) {
66 this.activity = activity;
68 throw new IllegalArgumentException("No valid data to encode.");
71 String action = intent.getAction();
72 if (action.equals(Intents.Encode.ACTION)) {
73 if (!encodeContentsFromZXingIntent(intent)) {
74 throw new IllegalArgumentException("No valid data to encode.");
76 } else if (action.equals(Intent.ACTION_SEND)) {
77 if (!encodeContentsFromShareIntent(intent)) {
78 throw new IllegalArgumentException("No valid data to encode.");
83 public void requestBarcode(Handler handler, int pixelResolution) {
84 Thread encodeThread = new EncodeThread(contents, handler, pixelResolution,
89 public String getContents() {
93 public String getDisplayContents() {
94 return displayContents;
97 public String getTitle() {
101 public String getFormat() {
102 return format.toString();
105 // It would be nice if the string encoding lived in the core ZXing library,
106 // but we use platform specific code like PhoneNumberUtils, so it can't.
107 private boolean encodeContentsFromZXingIntent(Intent intent) {
108 // Default to QR_CODE if no format given.
109 String formatString = intent.getStringExtra(Intents.Encode.FORMAT);
111 format = BarcodeFormat.valueOf(formatString);
112 } catch (IllegalArgumentException iae) {
117 if (format == null || BarcodeFormat.QR_CODE.equals(format)) {
118 String type = intent.getStringExtra(Intents.Encode.TYPE);
119 if (type == null || type.length() == 0) {
122 this.format = BarcodeFormat.QR_CODE;
123 encodeQRCodeContents(intent, type);
125 String data = intent.getStringExtra(Intents.Encode.DATA);
126 if (data != null && data.length() > 0) {
128 displayContents = data;
129 title = activity.getString(R.string.contents_text);
132 return contents != null && contents.length() > 0;
135 // Handles send intents from the Contacts app, retrieving a contact as a VCARD.
136 private boolean encodeContentsFromShareIntent(Intent intent) {
137 format = BarcodeFormat.QR_CODE;
139 Uri uri = (Uri)intent.getExtras().getParcelable(Intent.EXTRA_STREAM);
140 InputStream stream = activity.getContentResolver().openInputStream(uri);
141 int length = stream.available();
143 Log.w(TAG, "Content stream is empty");
146 byte[] vcard = new byte[length];
147 int bytesRead = stream.read(vcard, 0, length);
148 if (bytesRead < length) {
149 Log.w(TAG, "Unable to fully read available bytes from content stream");
152 String vcardString = new String(vcard, 0, bytesRead, "UTF-8");
153 Log.d(TAG, "Encoding share intent content:");
154 Log.d(TAG, vcardString);
155 Result result = new Result(vcardString, vcard, null, BarcodeFormat.QR_CODE);
156 ParsedResult parsedResult = ResultParser.parseResult(result);
157 if (!(parsedResult instanceof AddressBookParsedResult)) {
158 Log.d(TAG, "Result was not an address");
161 if (!encodeQRCodeContents((AddressBookParsedResult) parsedResult)) {
162 Log.d(TAG, "Unable to encode contents");
165 } catch (IOException e) {
168 } catch (NullPointerException e) {
170 // In case the uri was not found in the Intent.
173 return contents != null && contents.length() > 0;
176 private void encodeQRCodeContents(Intent intent, String type) {
177 if (type.equals(Contents.Type.TEXT)) {
178 String data = intent.getStringExtra(Intents.Encode.DATA);
179 if (data != null && data.length() > 0) {
181 displayContents = data;
182 title = activity.getString(R.string.contents_text);
184 } else if (type.equals(Contents.Type.EMAIL)) {
185 String data = trim(intent.getStringExtra(Intents.Encode.DATA));
187 contents = "mailto:" + data;
188 displayContents = data;
189 title = activity.getString(R.string.contents_email);
191 } else if (type.equals(Contents.Type.PHONE)) {
192 String data = trim(intent.getStringExtra(Intents.Encode.DATA));
194 contents = "tel:" + data;
195 displayContents = PhoneNumberUtils.formatNumber(data);
196 title = activity.getString(R.string.contents_phone);
198 } else if (type.equals(Contents.Type.SMS)) {
199 String data = trim(intent.getStringExtra(Intents.Encode.DATA));
201 contents = "sms:" + data;
202 displayContents = PhoneNumberUtils.formatNumber(data);
203 title = activity.getString(R.string.contents_sms);
205 } else if (type.equals(Contents.Type.CONTACT)) {
206 Bundle bundle = intent.getBundleExtra(Intents.Encode.DATA);
207 if (bundle != null) {
208 StringBuilder newContents = new StringBuilder();
209 StringBuilder newDisplayContents = new StringBuilder();
210 newContents.append("MECARD:");
211 String name = trim(bundle.getString(Contacts.Intents.Insert.NAME));
213 newContents.append("N:").append(name).append(';');
214 newDisplayContents.append(name);
216 String address = trim(bundle.getString(Contacts.Intents.Insert.POSTAL));
217 if (address != null) {
218 newContents.append("ADR:").append(address).append(';');
219 newDisplayContents.append('\n').append(address);
221 for (int x = 0; x < Contents.PHONE_KEYS.length; x++) {
222 String phone = trim(bundle.getString(Contents.PHONE_KEYS[x]));
224 newContents.append("TEL:").append(phone).append(';');
225 newDisplayContents.append('\n').append(PhoneNumberUtils.formatNumber(phone));
228 for (int x = 0; x < Contents.EMAIL_KEYS.length; x++) {
229 String email = trim(bundle.getString(Contents.EMAIL_KEYS[x]));
231 newContents.append("EMAIL:").append(email).append(';');
232 newDisplayContents.append('\n').append(email);
235 // Make sure we've encoded at least one field.
236 if (newDisplayContents.length() > 0) {
237 newContents.append(';');
238 contents = newContents.toString();
239 displayContents = newDisplayContents.toString();
240 title = activity.getString(R.string.contents_contact);
243 displayContents = null;
246 } else if (type.equals(Contents.Type.LOCATION)) {
247 Bundle bundle = intent.getBundleExtra(Intents.Encode.DATA);
248 if (bundle != null) {
249 // These must use Bundle.getFloat(), not getDouble(), it's part of the API.
250 float latitude = bundle.getFloat("LAT", Float.MAX_VALUE);
251 float longitude = bundle.getFloat("LONG", Float.MAX_VALUE);
252 if (latitude != Float.MAX_VALUE && longitude != Float.MAX_VALUE) {
253 contents = "geo:" + latitude + ',' + longitude;
254 displayContents = latitude + "," + longitude;
255 title = activity.getString(R.string.contents_location);
261 private boolean encodeQRCodeContents(AddressBookParsedResult contact) {
262 StringBuilder newContents = new StringBuilder();
263 StringBuilder newDisplayContents = new StringBuilder();
264 newContents.append("MECARD:");
265 String[] names = contact.getNames();
266 if (names != null && names.length > 0) {
267 String name = trim(names[0]);
269 newContents.append("N:").append(name).append(';');
270 newDisplayContents.append(name);
273 String[] addresses = contact.getAddresses();
274 if (addresses != null) {
275 for (String address : addresses) {
276 address = trim(address);
277 if (address != null) {
278 newContents.append("ADR:").append(address).append(';');
279 newDisplayContents.append('\n').append(address);
283 String[] phoneNumbers = contact.getPhoneNumbers();
284 if (phoneNumbers != null) {
285 for (String phone : phoneNumbers) {
288 newContents.append("TEL:").append(phone).append(';');
289 newDisplayContents.append('\n').append(PhoneNumberUtils.formatNumber(phone));
293 String[] emails = contact.getEmails();
294 if (emails != null) {
295 for (String email : emails) {
298 newContents.append("EMAIL:").append(email).append(';');
299 newDisplayContents.append('\n').append(email);
303 String url = trim(contact.getURL());
305 newContents.append("URL:").append(url).append(';');
306 newDisplayContents.append('\n').append(url);
308 // Make sure we've encoded at least one field.
309 if (newDisplayContents.length() > 0) {
310 newContents.append(';');
311 contents = newContents.toString();
312 displayContents = newDisplayContents.toString();
313 title = activity.getString(R.string.contents_contact);
317 displayContents = null;
322 static Bitmap encodeAsBitmap(String contents,
323 BarcodeFormat format,
325 int desiredHeight) throws WriterException {
326 Hashtable hints = null;
327 String encoding = guessAppropriateEncoding(contents);
328 if (encoding != null) {
329 hints = new Hashtable(2);
330 hints.put(EncodeHintType.CHARACTER_SET, encoding);
332 MultiFormatWriter writer = new MultiFormatWriter();
333 BitMatrix result = writer.encode(contents, format, desiredWidth, desiredHeight, hints);
334 int width = result.getWidth();
335 int height = result.getHeight();
336 int[] pixels = new int[width * height];
337 // All are 0, or black, by default
338 for (int y = 0; y < height; y++) {
339 int offset = y * width;
340 for (int x = 0; x < width; x++) {
341 pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
345 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
346 bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
350 private static String guessAppropriateEncoding(CharSequence contents) {
351 // Very crude at the moment
352 for (int i = 0; i < contents.length(); i++) {
353 if (contents.charAt(i) > 0xFF) {
360 private static String trim(String s) {
365 return s.length() == 0 ? null : s;