Updated the Android client to use native/local QR Code encoding. For now it still runs on a thread and has a temporary "Working..." dialog. As I optimize a bit further, these may not be necessary.

git-svn-id: https://zxing.googlecode.com/svn/trunk@739 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
dswitkin 2008-11-19 20:54:39 +00:00
parent 8a018c8af8
commit 7b0a66862d
5 changed files with 39 additions and 41 deletions

View file

@ -35,5 +35,7 @@
android:gravity="center" android:gravity="center"
android:textColor="@color/contents_text" android:textColor="@color/contents_text"
android:textSize="20.0sp" android:textSize="20.0sp"
android:paddingRight="8px"/> android:paddingRight="8px"
android:paddingTop="8px"
android:paddingBottom="8px"/>
</LinearLayout> </LinearLayout>

View file

@ -35,5 +35,7 @@
android:gravity="center" android:gravity="center"
android:textColor="@color/contents_text" android:textColor="@color/contents_text"
android:textSize="20.0sp" android:textSize="20.0sp"
android:paddingBottom="8px"/> android:paddingBottom="8px"
android:paddingLeft="8px"
android:paddingRight="8px"/>
</LinearLayout> </LinearLayout>

View file

@ -59,7 +59,7 @@
<string name="msg_default_status">Place a barcode inside the viewfinder rectangle to read it. <string name="msg_default_status">Place a barcode inside the viewfinder rectangle to read it.
</string> </string>
<string name="msg_default_type">Type</string> <string name="msg_default_type">Type</string>
<string name="msg_encode_barcode_failed">Could not retrieve a barcode from the network.</string> <string name="msg_encode_barcode_failed">Could not generate the requested barcode.</string>
<string name="msg_encode_contents_failed">Could not encode a barcode from the data provided. <string name="msg_encode_contents_failed">Could not encode a barcode from the data provided.
</string> </string>
<string name="msg_encode_in_progress">Generating a barcode\u2026</string> <string name="msg_encode_in_progress">Generating a barcode\u2026</string>

View file

@ -19,18 +19,15 @@ package com.google.zxing.client.android;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.provider.Contacts; import android.provider.Contacts;
import android.util.Log; import android.util.Log;
import org.apache.http.HttpEntity; import com.google.zxing.BarcodeFormat;
import org.apache.http.HttpResponse; import com.google.zxing.MultiFormatWriter;
import org.apache.http.client.methods.HttpGet; import com.google.zxing.WriterException;
import org.apache.http.client.methods.HttpUriRequest; import com.google.zxing.common.ByteMatrix;
import java.net.URI;
public final class QRCodeEncoder { public final class QRCodeEncoder {
@ -40,21 +37,17 @@ public final class QRCodeEncoder {
private String mContents; private String mContents;
private String mDisplayContents; private String mDisplayContents;
private String mTitle; private String mTitle;
private String mUserAgent;
public QRCodeEncoder(Activity activity, Intent intent) { public QRCodeEncoder(Activity activity, Intent intent) {
mActivity = activity; mActivity = activity;
if (!encodeContents(intent)) { if (!encodeContents(intent)) {
throw new IllegalArgumentException("No valid data to encode."); throw new IllegalArgumentException("No valid data to encode.");
} }
mUserAgent = mActivity.getString(R.string.zxing_user_agent);
} }
// Once the core ZXing library supports encoding, we'll be able to generate the bitmap
// synchronously. For now, it's a network request, so it's handled on a thread.
public void requestBarcode(Handler handler, int pixelResolution) { public void requestBarcode(Handler handler, int pixelResolution) {
Thread mNetworkThread = new NetworkThread(mContents, handler, pixelResolution); Thread encodeThread = new EncodeThread(mContents, handler, pixelResolution);
mNetworkThread.start(); encodeThread.start();
} }
public String getContents() { public String getContents() {
@ -69,7 +62,7 @@ public final class QRCodeEncoder {
return mTitle; return mTitle;
} }
// Perhaps the string encoding should live in the core ZXing library too. // TODO: The string encoding should live in the core ZXing library.
private boolean encodeContents(Intent intent) { private boolean encodeContents(Intent intent) {
if (intent == null) return false; if (intent == null) return false;
String type = intent.getStringExtra(Intents.Encode.TYPE); String type = intent.getStringExtra(Intents.Encode.TYPE);
@ -144,45 +137,46 @@ public final class QRCodeEncoder {
return mContents != null && mContents.length() > 0; return mContents != null && mContents.length() > 0;
} }
private final class NetworkThread extends Thread { private final static class EncodeThread extends Thread {
private final String mContents; private final String mContents;
private final Handler mHandler; private final Handler mHandler;
private final int mPixelResolution; private final int mPixelResolution;
public NetworkThread(String contents, Handler handler, int pixelResolution) { public EncodeThread(String contents, Handler handler, int pixelResolution) {
mContents = contents; mContents = contents;
mHandler = handler; mHandler = handler;
mPixelResolution = pixelResolution; mPixelResolution = pixelResolution;
} }
public final void run() { public final void run() {
AndroidHttpClient client = null;
try { try {
URI uri = new URI("http", null, "chart.apis.google.com", -1, "/chart", ByteMatrix result = new MultiFormatWriter().encode(mContents, BarcodeFormat.QR_CODE,
"cht=qr&chs=" + mPixelResolution + "x" + mPixelResolution + "&chl=" + mContents, null); mPixelResolution, mPixelResolution);
HttpUriRequest get = new HttpGet(uri); int width = result.width();
client = AndroidHttpClient.newInstance(mUserAgent); int height = result.height();
HttpResponse response = client.execute(get); byte[][] array = result.getArray();
HttpEntity entity = response.getEntity(); int[] pixels = new int[width * height];
Bitmap image = BitmapFactory.decodeStream(entity.getContent()); for (int y = 0; y < height; y++) {
if (image != null) { for (int x = 0; x < width; x++) {
Message message = Message.obtain(mHandler, R.id.encode_succeeded); int grey = array[y][x] & 0xff;
message.obj = image; pixels[y * width + x] = (0xff << 24) | (grey << 16) | (grey << 8) | grey;
message.sendToTarget(); }
} else {
Log.e(TAG, "Could not decode png from the network");
Message message = Message.obtain(mHandler, R.id.encode_failed);
message.sendToTarget();
} }
} catch (Exception e) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
Message message = Message.obtain(mHandler, R.id.encode_succeeded);
message.obj = bitmap;
message.sendToTarget();
} catch (WriterException e) {
Log.e(TAG, e.toString());
Message message = Message.obtain(mHandler, R.id.encode_failed);
message.sendToTarget();
} catch (IllegalArgumentException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
Message message = Message.obtain(mHandler, R.id.encode_failed); Message message = Message.obtain(mHandler, R.id.encode_failed);
message.sendToTarget(); message.sendToTarget();
} finally {
if (client != null) {
client.close();
}
} }
} }
} }

View file

@ -95,7 +95,7 @@ final class YUVMonochromeBitmapSource extends BaseMonochromeBitmapSource {
/** /**
* Create a greyscale Android Bitmap from the YUV data based on the crop rectangle. * Create a greyscale Android Bitmap from the YUV data based on the crop rectangle.
* *
* @return A 565 bitmap. * @return An 8888 bitmap.
*/ */
public Bitmap renderToBitmap() { public Bitmap renderToBitmap() {
int width = mCrop.width(); int width = mCrop.width();