mirror of
https://github.com/zxing/zxing.git
synced 2025-02-21 02:55:27 -08:00
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
This commit is contained in:
parent
46e8cef52e
commit
b40c3cc7e0
|
@ -37,6 +37,8 @@
|
|||
<TextView android:id="@+id/contents_text_view"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="fill_parent"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:gravity="center"
|
||||
android:textColor="@color/contents_text"
|
||||
android:textSize="18sp"
|
||||
android:paddingRight="8dip"
|
||||
|
|
|
@ -20,8 +20,6 @@
|
|||
<item type="id" name="decode"/>
|
||||
<item type="id" name="decode_failed"/>
|
||||
<item type="id" name="decode_succeeded"/>
|
||||
<item type="id" name="encode_failed"/>
|
||||
<item type="id" name="encode_succeeded"/>
|
||||
<item type="id" name="launch_product_query"/>
|
||||
<item type="id" name="quit"/>
|
||||
<item type="id" name="restart_preview"/>
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<EncodeHintType,Object> 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];
|
||||
|
|
Loading…
Reference in a new issue