From b40c3cc7e00ca7c5dcc689ade07f5b300da8dbfc Mon Sep 17 00:00:00 2001 From: srowen Date: Tue, 30 Nov 2010 11:57:07 +0000 Subject: [PATCH] Simplify encoding to fix odd display problem -- no thread, no layout listener git-svn-id: https://zxing.googlecode.com/svn/trunk@1676 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- android/res/layout-land/encode.xml | 2 + android/res/values/ids.xml | 2 - .../client/android/encode/EncodeActivity.java | 87 +++++++------------ .../client/android/encode/EncodeThread.java | 60 ------------- .../client/android/encode/QRCodeEncoder.java | 21 ++--- 5 files changed, 37 insertions(+), 135 deletions(-) delete mode 100644 android/src/com/google/zxing/client/android/encode/EncodeThread.java diff --git a/android/res/layout-land/encode.xml b/android/res/layout-land/encode.xml index 00ff780fc..4c1d64f1b 100755 --- a/android/res/layout-land/encode.xml +++ b/android/res/layout-land/encode.xml @@ -37,6 +37,8 @@ - - diff --git a/android/src/com/google/zxing/client/android/encode/EncodeActivity.java b/android/src/com/google/zxing/client/android/encode/EncodeActivity.java index 8b6815b46..560a58561 100755 --- a/android/src/com/google/zxing/client/android/encode/EncodeActivity.java +++ b/android/src/com/google/zxing/client/android/encode/EncodeActivity.java @@ -16,7 +16,9 @@ package com.google.zxing.client.android.encode; -import com.google.zxing.BarcodeFormat; +import android.content.Context; +import android.view.Display; +import android.view.WindowManager; import com.google.zxing.WriterException; import com.google.zxing.client.android.FinishListener; import com.google.zxing.client.android.Intents; @@ -28,14 +30,11 @@ import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; -import android.os.Handler; import android.os.Environment; -import android.os.Message; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; -import android.view.ViewTreeObserver.OnGlobalLayoutListener; import android.widget.ImageView; import android.widget.TextView; @@ -54,57 +53,9 @@ public final class EncodeActivity extends Activity { private static final String TAG = EncodeActivity.class.getSimpleName(); - private static final int SHARE_BARCODE_DIMENSION = 300; private static final int MAX_BARCODE_FILENAME_LENGTH = 24; private QRCodeEncoder qrCodeEncoder; - private boolean firstLayout; - - /** - * This needs to be delayed until after the first layout so that the view dimensions will be - * available. - */ - private final OnGlobalLayoutListener layoutListener = new OnGlobalLayoutListener() { - public void onGlobalLayout() { - if (firstLayout) { - View layout = findViewById(R.id.encode_view); - int width = layout.getWidth(); - int height = layout.getHeight(); - int smallerDimension = width < height ? width : height; - smallerDimension = smallerDimension * 7 / 8; - - Intent intent = getIntent(); - try { - qrCodeEncoder = new QRCodeEncoder(EncodeActivity.this, intent); - setTitle(getString(R.string.app_name) + " - " + qrCodeEncoder.getTitle()); - qrCodeEncoder.requestBarcode(handler, smallerDimension); - } catch (IllegalArgumentException e) { - showErrorMessage(R.string.msg_encode_contents_failed); - } - firstLayout = false; - } - } - }; - - private final Handler handler = new Handler() { - @Override - public void handleMessage(Message message) { - switch (message.what) { - case R.id.encode_succeeded: - Bitmap image = (Bitmap) message.obj; - ImageView view = (ImageView) findViewById(R.id.image_view); - view.setImageBitmap(image); - TextView contents = (TextView) findViewById(R.id.contents_text_view); - contents.setText(qrCodeEncoder.getDisplayContents()); - //qrCodeEncoder = null; - break; - case R.id.encode_failed: - showErrorMessage(R.string.msg_encode_barcode_failed); - qrCodeEncoder = null; - break; - } - } - }; @Override public void onCreate(Bundle icicle) { @@ -138,8 +89,7 @@ public final class EncodeActivity extends Activity { String contents = qrCodeEncoder.getContents(); Bitmap bitmap; try { - bitmap = QRCodeEncoder.encodeAsBitmap(contents, BarcodeFormat.QR_CODE, - SHARE_BARCODE_DIMENSION, SHARE_BARCODE_DIMENSION); + bitmap = qrCodeEncoder.encodeAsBitmap(); } catch (WriterException we) { Log.w(TAG, we); return true; @@ -200,9 +150,32 @@ public final class EncodeActivity extends Activity { @Override protected void onResume() { super.onResume(); - View layout = findViewById(R.id.encode_view); - layout.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener); - firstLayout = true; + // This assumes the view is full screen, which is a good assumption + WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE); + Display display = manager.getDefaultDisplay(); + int width = display.getWidth(); + int height = display.getHeight(); + int smallerDimension = width < height ? width : height; + smallerDimension = smallerDimension * 7 / 8; + + Intent intent = getIntent(); + try { + qrCodeEncoder = new QRCodeEncoder(this, intent, smallerDimension); + setTitle(getString(R.string.app_name) + " - " + qrCodeEncoder.getTitle()); + Bitmap bitmap = qrCodeEncoder.encodeAsBitmap(); + ImageView view = (ImageView) findViewById(R.id.image_view); + view.setImageBitmap(bitmap); + TextView contents = (TextView) findViewById(R.id.contents_text_view); + contents.setText(qrCodeEncoder.getDisplayContents()); + } catch (WriterException e) { + Log.e(TAG, "Could not encode barcode", e); + showErrorMessage(R.string.msg_encode_contents_failed); + qrCodeEncoder = null; + } catch (IllegalArgumentException e) { + Log.e(TAG, "Could not encode barcode", e); + showErrorMessage(R.string.msg_encode_contents_failed); + qrCodeEncoder = null; + } } private void showErrorMessage(int message) { diff --git a/android/src/com/google/zxing/client/android/encode/EncodeThread.java b/android/src/com/google/zxing/client/android/encode/EncodeThread.java deleted file mode 100644 index 02477f574..000000000 --- a/android/src/com/google/zxing/client/android/encode/EncodeThread.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (C) 2010 ZXing authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.zxing.client.android.encode; - -import android.graphics.Bitmap; -import android.os.Handler; -import android.os.Message; -import android.util.Log; -import com.google.zxing.BarcodeFormat; -import com.google.zxing.WriterException; -import com.google.zxing.client.android.R; - -final class EncodeThread extends Thread { - - private static final String TAG = EncodeThread.class.getSimpleName(); - - private final String contents; - private final Handler handler; - private final int pixelResolution; - private final BarcodeFormat format; - - EncodeThread(String contents, Handler handler, int pixelResolution, BarcodeFormat format) { - this.contents = contents; - this.handler = handler; - this.pixelResolution = pixelResolution; - this.format = format; - } - - @Override - public void run() { - try { - Bitmap bitmap = QRCodeEncoder.encodeAsBitmap(contents, format, pixelResolution, pixelResolution); - Message message = Message.obtain(handler, R.id.encode_succeeded); - message.obj = bitmap; - message.sendToTarget(); - } catch (WriterException e) { - Log.e(TAG, "Could not encode barcode", e); - Message message = Message.obtain(handler, R.id.encode_failed); - message.sendToTarget(); - } catch (IllegalArgumentException e) { - Log.e(TAG, "Could not encode barcode", e); - Message message = Message.obtain(handler, R.id.encode_failed); - message.sendToTarget(); - } - } -} diff --git a/android/src/com/google/zxing/client/android/encode/QRCodeEncoder.java b/android/src/com/google/zxing/client/android/encode/QRCodeEncoder.java index 8cf6693e6..d83793195 100755 --- a/android/src/com/google/zxing/client/android/encode/QRCodeEncoder.java +++ b/android/src/com/google/zxing/client/android/encode/QRCodeEncoder.java @@ -34,7 +34,6 @@ import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; -import android.os.Handler; import android.provider.Contacts; import android.telephony.PhoneNumberUtils; import android.util.Log; @@ -61,8 +60,9 @@ final class QRCodeEncoder { private String displayContents; private String title; private BarcodeFormat format; + private final int dimension; - QRCodeEncoder(Activity activity, Intent intent) { + QRCodeEncoder(Activity activity, Intent intent, int dimension) { this.activity = activity; if (intent == null) { throw new IllegalArgumentException("No valid data to encode."); @@ -78,12 +78,8 @@ final class QRCodeEncoder { throw new IllegalArgumentException("No valid data to encode."); } } - } - public void requestBarcode(Handler handler, int pixelResolution) { - Thread encodeThread = new EncodeThread(contents, handler, pixelResolution, - format); - encodeThread.start(); + this.dimension = dimension; } public String getContents() { @@ -98,10 +94,6 @@ final class QRCodeEncoder { return title; } - public String getFormat() { - return format.toString(); - } - // It would be nice if the string encoding lived in the core ZXing library, // but we use platform specific code like PhoneNumberUtils, so it can't. private boolean encodeContentsFromZXingIntent(Intent intent) { @@ -318,10 +310,7 @@ final class QRCodeEncoder { } } - static Bitmap encodeAsBitmap(String contents, - BarcodeFormat format, - int desiredWidth, - int desiredHeight) throws WriterException { + Bitmap encodeAsBitmap() throws WriterException { Hashtable hints = null; String encoding = guessAppropriateEncoding(contents); if (encoding != null) { @@ -329,7 +318,7 @@ final class QRCodeEncoder { hints.put(EncodeHintType.CHARACTER_SET, encoding); } MultiFormatWriter writer = new MultiFormatWriter(); - BitMatrix result = writer.encode(contents, format, desiredWidth, desiredHeight, hints); + BitMatrix result = writer.encode(contents, format, dimension, dimension, hints); int width = result.getWidth(); int height = result.getHeight(); int[] pixels = new int[width * height];