mirror of
https://github.com/zxing/zxing.git
synced 2025-02-02 05:41:08 -08:00
More encoding related changes for encoding Chinese chars in QR codes
git-svn-id: https://zxing.googlecode.com/svn/trunk@1392 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
a0ecb24f54
commit
1ef423bf68
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -17,6 +17,7 @@
|
||||||
package com.google.zxing.client.android.encode;
|
package com.google.zxing.client.android.encode;
|
||||||
|
|
||||||
import com.google.zxing.BarcodeFormat;
|
import com.google.zxing.BarcodeFormat;
|
||||||
|
import com.google.zxing.EncodeHintType;
|
||||||
import com.google.zxing.MultiFormatWriter;
|
import com.google.zxing.MultiFormatWriter;
|
||||||
import com.google.zxing.Result;
|
import com.google.zxing.Result;
|
||||||
import com.google.zxing.WriterException;
|
import com.google.zxing.WriterException;
|
||||||
|
@ -34,7 +35,6 @@ import android.graphics.Bitmap;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Message;
|
|
||||||
import android.provider.Contacts;
|
import android.provider.Contacts;
|
||||||
import android.telephony.PhoneNumberUtils;
|
import android.telephony.PhoneNumberUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
@ -42,6 +42,7 @@ import android.util.Log;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.util.Hashtable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class does the work of decoding the user's request and extracting all the data
|
* 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,
|
BarcodeFormat format,
|
||||||
int desiredWidth,
|
int desiredWidth,
|
||||||
int desiredHeight) throws WriterException {
|
int desiredHeight) throws WriterException {
|
||||||
BitMatrix result = new MultiFormatWriter().encode(contents, format,
|
Hashtable hints = null;
|
||||||
desiredWidth, desiredHeight);
|
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 width = result.getWidth();
|
||||||
int height = result.getHeight();
|
int height = result.getHeight();
|
||||||
int[] pixels = new int[width * height];
|
int[] pixels = new int[width * height];
|
||||||
|
@ -326,39 +333,13 @@ final class QRCodeEncoder {
|
||||||
return bitmap;
|
return bitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final class EncodeThread extends Thread {
|
private static String guessAppropriateEncoding(CharSequence contents) {
|
||||||
|
// Very crude at the moment
|
||||||
private static final String TAG = EncodeThread.class.getSimpleName();
|
for (int i = 0; i < contents.length(); i++) {
|
||||||
|
if (contents.charAt(i) > 0xFF) {
|
||||||
private final String contents;
|
return "UTF-8";
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,8 @@ package com.google.zxing.client.result;
|
||||||
|
|
||||||
import com.google.zxing.Result;
|
import com.google.zxing.Result;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -93,6 +95,7 @@ final class VCardResultParser extends ResultParser {
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean quotedPrintable = false;
|
boolean quotedPrintable = false;
|
||||||
|
String quotedPrintableCharset = null;
|
||||||
if (i > metadataStart) {
|
if (i > metadataStart) {
|
||||||
// There was something after the tag, before colon
|
// There was something after the tag, before colon
|
||||||
int j = metadataStart+1;
|
int j = metadataStart+1;
|
||||||
|
@ -107,6 +110,8 @@ final class VCardResultParser extends ResultParser {
|
||||||
if (value.equalsIgnoreCase("QUOTED-PRINTABLE")) {
|
if (value.equalsIgnoreCase("QUOTED-PRINTABLE")) {
|
||||||
quotedPrintable = true;
|
quotedPrintable = true;
|
||||||
}
|
}
|
||||||
|
} else if (key.equalsIgnoreCase("CHARSET")) {
|
||||||
|
quotedPrintableCharset = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
metadataStart = j;
|
metadataStart = j;
|
||||||
|
@ -149,7 +154,7 @@ final class VCardResultParser extends ResultParser {
|
||||||
element = element.trim();
|
element = element.trim();
|
||||||
}
|
}
|
||||||
if (quotedPrintable) {
|
if (quotedPrintable) {
|
||||||
element = decodeQuotedPrintable(element);
|
element = decodeQuotedPrintable(element, quotedPrintableCharset);
|
||||||
} else {
|
} else {
|
||||||
element = stripContinuationCRLF(element);
|
element = stripContinuationCRLF(element);
|
||||||
}
|
}
|
||||||
|
@ -191,9 +196,10 @@ final class VCardResultParser extends ResultParser {
|
||||||
return result.toString();
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String decodeQuotedPrintable(String value) {
|
private static String decodeQuotedPrintable(String value, String charset) {
|
||||||
int length = value.length();
|
int length = value.length();
|
||||||
StringBuffer result = new StringBuffer(length);
|
StringBuffer result = new StringBuffer(length);
|
||||||
|
ByteArrayOutputStream fragmentBuffer = new ByteArrayOutputStream();
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
char c = value.charAt(i);
|
char c = value.charAt(i);
|
||||||
switch (c) {
|
switch (c) {
|
||||||
|
@ -208,8 +214,8 @@ final class VCardResultParser extends ResultParser {
|
||||||
} else {
|
} else {
|
||||||
char nextNextChar = value.charAt(i+2);
|
char nextNextChar = value.charAt(i+2);
|
||||||
try {
|
try {
|
||||||
int encodedChar = 16 * toHexValue(nextChar) + toHexValue(nextNextChar);
|
int encodedByte = 16 * toHexValue(nextChar) + toHexValue(nextNextChar);
|
||||||
result.append((char) encodedChar);
|
fragmentBuffer.write(encodedByte);
|
||||||
} catch (IllegalArgumentException iae) {
|
} catch (IllegalArgumentException iae) {
|
||||||
// continue, assume it was incorrectly encoded
|
// continue, assume it was incorrectly encoded
|
||||||
}
|
}
|
||||||
|
@ -218,9 +224,11 @@ final class VCardResultParser extends ResultParser {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
maybeAppendFragment(fragmentBuffer, charset, result);
|
||||||
result.append(c);
|
result.append(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
maybeAppendFragment(fragmentBuffer, charset, result);
|
||||||
return result.toString();
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -235,6 +243,27 @@ final class VCardResultParser extends ResultParser {
|
||||||
throw new IllegalArgumentException();
|
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) {
|
static String matchSingleVCardPrefixedField(String prefix, String rawText, boolean trim) {
|
||||||
String[] values = matchVCardPrefixedField(prefix, rawText, trim);
|
String[] values = matchVCardPrefixedField(prefix, rawText, trim);
|
||||||
return values == null ? null : values[0];
|
return values == null ? null : values[0];
|
||||||
|
|
Loading…
Reference in a new issue