From 5decd5cfdfc8a9b8a0c8bf5cf4f4b1c5b48374fd Mon Sep 17 00:00:00 2001 From: srowen Date: Thu, 27 May 2010 11:25:00 +0000 Subject: More encoding related changes for encoding Chinese chars in QR codes git-svn-id: http://zxing.googlecode.com/svn/trunk/android@1392 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- .../zxing/client/android/encode/EncodeThread.java | 60 ++++++++++++++++++++++ .../zxing/client/android/encode/QRCodeEncoder.java | 51 ++++++------------ 2 files changed, 76 insertions(+), 35 deletions(-) create mode 100644 src/com/google/zxing/client/android/encode/EncodeThread.java diff --git a/src/com/google/zxing/client/android/encode/EncodeThread.java b/src/com/google/zxing/client/android/encode/EncodeThread.java new file mode 100644 index 00000000..02477f57 --- /dev/null +++ b/src/com/google/zxing/client/android/encode/EncodeThread.java @@ -0,0 +1,60 @@ +/* + * 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/src/com/google/zxing/client/android/encode/QRCodeEncoder.java b/src/com/google/zxing/client/android/encode/QRCodeEncoder.java index 358795d3..2a130110 100755 --- a/src/com/google/zxing/client/android/encode/QRCodeEncoder.java +++ b/src/com/google/zxing/client/android/encode/QRCodeEncoder.java @@ -17,6 +17,7 @@ package com.google.zxing.client.android.encode; import com.google.zxing.BarcodeFormat; +import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.Result; import com.google.zxing.WriterException; @@ -34,7 +35,6 @@ import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.os.Handler; -import android.os.Message; import android.provider.Contacts; import android.telephony.PhoneNumberUtils; import android.util.Log; @@ -42,6 +42,7 @@ import android.util.Log; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.util.Hashtable; /** * This class does the work of decoding the user's request and extracting all the data @@ -308,8 +309,14 @@ final class QRCodeEncoder { BarcodeFormat format, int desiredWidth, int desiredHeight) throws WriterException { - BitMatrix result = new MultiFormatWriter().encode(contents, format, - desiredWidth, desiredHeight); + Hashtable hints = null; + String encoding = guessAppropriateEncoding(contents); + if (encoding != null) { + hints = new Hashtable(2); + hints.put(EncodeHintType.CHARACTER_SET, encoding); + } + MultiFormatWriter writer = new MultiFormatWriter(); + BitMatrix result = writer.encode(contents, format, desiredWidth, desiredHeight, hints); int width = result.getWidth(); int height = result.getHeight(); int[] pixels = new int[width * height]; @@ -326,39 +333,13 @@ final class QRCodeEncoder { return bitmap; } - private static 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 = 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(); + private static String guessAppropriateEncoding(CharSequence contents) { + // Very crude at the moment + for (int i = 0; i < contents.length(); i++) { + if (contents.charAt(i) > 0xFF) { + return "UTF-8"; } } + return null; } } -- cgit v1.2.3