Small speedups in time-related code

git-svn-id: https://zxing.googlecode.com/svn/trunk@780 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2008-12-09 16:20:32 +00:00
parent f265d0872f
commit 1f2699c196
4 changed files with 32 additions and 15 deletions

View file

@ -28,7 +28,6 @@ import com.google.zxing.MultiFormatReader;
import com.google.zxing.ReaderException; import com.google.zxing.ReaderException;
import com.google.zxing.Result; import com.google.zxing.Result;
import java.util.Date;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Vector; import java.util.Vector;
@ -151,7 +150,7 @@ final class DecodeThread extends Thread {
* @param height The height of the preview frame. * @param height The height of the preview frame.
*/ */
private void decode(byte[] data, int width, int height) { private void decode(byte[] data, int width, int height) {
Date startDate = new Date(); long start = System.currentTimeMillis();
boolean success; boolean success;
Result rawResult = null; Result rawResult = null;
YUVMonochromeBitmapSource source = new YUVMonochromeBitmapSource(data, width, height, YUVMonochromeBitmapSource source = new YUVMonochromeBitmapSource(data, width, height,
@ -162,18 +161,18 @@ final class DecodeThread extends Thread {
} catch (ReaderException e) { } catch (ReaderException e) {
success = false; success = false;
} }
Date endDate = new Date(); long end = System.currentTimeMillis();
if (success) { if (success) {
Message message = Message.obtain(mActivity.mHandler, R.id.decode_succeeded, rawResult); Message message = Message.obtain(mActivity.mHandler, R.id.decode_succeeded, rawResult);
message.arg1 = (int) (endDate.getTime() - startDate.getTime()); message.arg1 = (int) (end - start);
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putParcelable(BARCODE_BITMAP, source.renderToBitmap()); bundle.putParcelable(BARCODE_BITMAP, source.renderToBitmap());
message.setData(bundle); message.setData(bundle);
message.sendToTarget(); message.sendToTarget();
} else { } else {
Message message = Message.obtain(mActivity.mHandler, R.id.decode_failed); Message message = Message.obtain(mActivity.mHandler, R.id.decode_failed);
message.arg1 = (int) (endDate.getTime() - startDate.getTime()); message.arg1 = (int) (end - start);
message.sendToTarget(); message.sendToTarget();
} }
} }

View file

@ -32,6 +32,8 @@ import java.util.Date;
public final class AddressBookResultHandler extends ResultHandler { public final class AddressBookResultHandler extends ResultHandler {
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
private final boolean[] mFields; private final boolean[] mFields;
private int mButtonCount; private int mButtonCount;
@ -145,8 +147,10 @@ public final class AddressBookResultHandler extends ResultHandler {
String birthday = result.getBirthday(); String birthday = result.getBirthday();
if (birthday != null && birthday.length() > 0) { if (birthday != null && birthday.length() > 0) {
DateFormat format = new SimpleDateFormat("yyyyMMdd"); Date date;
Date date = format.parse(birthday, new ParsePosition(0)); synchronized (DATE_FORMAT) {
date = DATE_FORMAT.parse(birthday, new ParsePosition(0));
}
ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), contents); ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), contents);
} }
ParsedResult.maybeAppend(result.getNote(), contents); ParsedResult.maybeAppend(result.getNote(), contents);

View file

@ -30,6 +30,9 @@ import java.util.GregorianCalendar;
public final class CalendarResultHandler extends ResultHandler { public final class CalendarResultHandler extends ResultHandler {
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
private static final int[] mButtons = { private static final int[] mButtons = {
R.string.button_add_calendar R.string.button_add_calendar
}; };
@ -77,13 +80,17 @@ public final class CalendarResultHandler extends ResultHandler {
private void appendTime(String when, StringBuffer result) { private void appendTime(String when, StringBuffer result) {
if (when.length() == 8) { if (when.length() == 8) {
// Show only year/month/day // Show only year/month/day
DateFormat format = new SimpleDateFormat("yyyyMMdd"); Date date;
Date date = format.parse(when, new ParsePosition(0)); synchronized (DATE_FORMAT) {
date = DATE_FORMAT.parse(when, new ParsePosition(0));
}
ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), result); ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), result);
} else { } else {
// The when string can be local time, or UTC if it ends with a Z // The when string can be local time, or UTC if it ends with a Z
DateFormat format = new SimpleDateFormat("yyyyMMdd'T'HHmmss"); Date date;
Date date = format.parse(when.substring(0, 15), new ParsePosition(0)); synchronized (DATE_TIME_FORMAT) {
date = DATE_TIME_FORMAT.parse(when.substring(0, 15), new ParsePosition(0));
}
long milliseconds = date.getTime(); long milliseconds = date.getTime();
if (when.length() == 16 && when.charAt(15) == 'Z') { if (when.length() == 16 && when.charAt(15) == 'Z') {
Calendar calendar = new GregorianCalendar(); Calendar calendar = new GregorianCalendar();

View file

@ -37,6 +37,9 @@ import java.util.GregorianCalendar;
public abstract class ResultHandler { public abstract class ResultHandler {
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
public static final int MAX_BUTTON_COUNT = 4; public static final int MAX_BUTTON_COUNT = 4;
protected final ParsedResult mResult; protected final ParsedResult mResult;
@ -119,13 +122,17 @@ public abstract class ResultHandler {
private long calculateMilliseconds(String when) { private long calculateMilliseconds(String when) {
if (when.length() == 8) { if (when.length() == 8) {
// Only contains year/month/day // Only contains year/month/day
DateFormat format = new SimpleDateFormat("yyyyMMdd"); Date date;
Date date = format.parse(when, new ParsePosition(0)); synchronized (DATE_FORMAT) {
date = DATE_FORMAT.parse(when, new ParsePosition(0));
}
return date.getTime(); return date.getTime();
} else { } else {
// The when string can be local time, or UTC if it ends with a Z // The when string can be local time, or UTC if it ends with a Z
DateFormat format = new SimpleDateFormat("yyyyMMdd'T'HHmmss"); Date date;
Date date = format.parse(when.substring(0, 15), new ParsePosition(0)); synchronized (DATE_TIME_FORMAT) {
date = DATE_TIME_FORMAT.parse(when.substring(0, 15), new ParsePosition(0));
}
long milliseconds = date.getTime(); long milliseconds = date.getTime();
if (when.length() == 16 && when.charAt(15) == 'Z') { if (when.length() == 16 && when.charAt(15) == 'Z') {
Calendar calendar = new GregorianCalendar(); Calendar calendar = new GregorianCalendar();