Skip to content

Commit

Permalink
More encoding related changes for encoding Chinese chars in QR codes
Browse files Browse the repository at this point in the history
  • Loading branch information
srowen committed May 27, 2010
1 parent 9e2bd33 commit 1a93435
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 39 deletions.
@@ -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();
}
}
}
Expand Up @@ -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;
Expand All @@ -34,14 +35,14 @@
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;

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
Expand Down Expand Up @@ -308,8 +309,14 @@ static Bitmap encodeAsBitmap(String contents,
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];
Expand All @@ -326,39 +333,13 @@ static Bitmap encodeAsBitmap(String contents,
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;
}
}
37 changes: 33 additions & 4 deletions core/src/com/google/zxing/client/result/VCardResultParser.java
Expand Up @@ -18,6 +18,8 @@

import com.google.zxing.Result;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Vector;

/**
Expand Down Expand Up @@ -93,6 +95,7 @@ private static String[] matchVCardPrefixedField(String prefix, String rawText, b
}

boolean quotedPrintable = false;
String quotedPrintableCharset = null;
if (i > metadataStart) {
// There was something after the tag, before colon
int j = metadataStart+1;
Expand All @@ -107,6 +110,8 @@ private static String[] matchVCardPrefixedField(String prefix, String rawText, b
if (value.equalsIgnoreCase("QUOTED-PRINTABLE")) {
quotedPrintable = true;
}
} else if (key.equalsIgnoreCase("CHARSET")) {
quotedPrintableCharset = value;
}
}
metadataStart = j;
Expand Down Expand Up @@ -149,7 +154,7 @@ private static String[] matchVCardPrefixedField(String prefix, String rawText, b
element = element.trim();
}
if (quotedPrintable) {
element = decodeQuotedPrintable(element);
element = decodeQuotedPrintable(element, quotedPrintableCharset);
} else {
element = stripContinuationCRLF(element);
}
Expand Down Expand Up @@ -191,9 +196,10 @@ private static String stripContinuationCRLF(String value) {
return result.toString();
}

private static String decodeQuotedPrintable(String value) {
private static String decodeQuotedPrintable(String value, String charset) {
int length = value.length();
StringBuffer result = new StringBuffer(length);
ByteArrayOutputStream fragmentBuffer = new ByteArrayOutputStream();
for (int i = 0; i < length; i++) {
char c = value.charAt(i);
switch (c) {
Expand All @@ -208,8 +214,8 @@ private static String decodeQuotedPrintable(String value) {
} else {
char nextNextChar = value.charAt(i+2);
try {
int encodedChar = 16 * toHexValue(nextChar) + toHexValue(nextNextChar);
result.append((char) encodedChar);
int encodedByte = 16 * toHexValue(nextChar) + toHexValue(nextNextChar);
fragmentBuffer.write(encodedByte);
} catch (IllegalArgumentException iae) {
// continue, assume it was incorrectly encoded
}
Expand All @@ -218,9 +224,11 @@ private static String decodeQuotedPrintable(String value) {
}
break;
default:
maybeAppendFragment(fragmentBuffer, charset, result);
result.append(c);
}
}
maybeAppendFragment(fragmentBuffer, charset, result);
return result.toString();
}

Expand All @@ -235,6 +243,27 @@ private static int toHexValue(char c) {
throw new IllegalArgumentException();
}

private static void maybeAppendFragment(ByteArrayOutputStream fragmentBuffer,
String charset,
StringBuffer result) {
if (fragmentBuffer.size() > 0) {
byte[] fragmentBytes = fragmentBuffer.toByteArray();
String fragment;
if (charset == null) {
fragment = new String(fragmentBytes);
} else {
try {
fragment = new String(fragmentBytes, charset);
} catch (UnsupportedEncodingException e) {
// Yikes, well try anyway:
fragment = new String(fragmentBytes);
}
}
fragmentBuffer.reset();
result.append(fragment);
}
}

static String matchSingleVCardPrefixedField(String prefix, String rawText, boolean trim) {
String[] values = matchVCardPrefixedField(prefix, rawText, trim);
return values == null ? null : values[0];
Expand Down

0 comments on commit 1a93435

Please sign in to comment.