Additional changes for Android 4

git-svn-id: https://zxing.googlecode.com/svn/trunk@2881 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen@gmail.com 2013-09-02 06:05:31 +00:00
parent 0d6eabd6fc
commit b7188c4312
6 changed files with 66 additions and 68 deletions

View file

@ -17,7 +17,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.zxing.client.androidtest"
android:versionName="1.4"
android:versionCode="8"
android:versionCode="9"
android:installLocation="auto">
<uses-permission android:name="android.permission.CAMERA"/>

View file

@ -19,6 +19,7 @@
android:layout_height="fill_parent"
android:orientation="vertical">
<Button android:id="@+id/benchmark_run"
style="@android:style/Widget.Holo.Button.Borderless"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/benchmark_run"/>

View file

@ -18,65 +18,80 @@
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button android:id="@+id/run_benchmark"
style="@android:style/Widget.Holo.Button.Borderless"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/benchmark_run"/>
<Button android:id="@+id/get_camera_parameters"
style="@android:style/Widget.Holo.Button.Borderless"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/get_camera_parameters"/>
<Button android:id="@+id/scan_product"
style="@android:style/Widget.Holo.Button.Borderless"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/scan_product"/>
<Button android:id="@+id/scan_qr_code"
style="@android:style/Widget.Holo.Button.Borderless"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/scan_qr_code"/>
<Button android:id="@+id/scan_anything"
style="@android:style/Widget.Holo.Button.Borderless"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/scan_anything"/>
<Button android:id="@+id/search_book_contents"
style="@android:style/Widget.Holo.Button.Borderless"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/search_book_contents"/>
<Button android:id="@+id/encode_url"
style="@android:style/Widget.Holo.Button.Borderless"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/encode_url"/>
<Button android:id="@+id/encode_email"
style="@android:style/Widget.Holo.Button.Borderless"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/encode_email"/>
<Button android:id="@+id/encode_phone"
style="@android:style/Widget.Holo.Button.Borderless"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/encode_phone"/>
<Button android:id="@+id/encode_sms"
style="@android:style/Widget.Holo.Button.Borderless"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/encode_sms"/>
<Button android:id="@+id/encode_contact"
style="@android:style/Widget.Holo.Button.Borderless"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/encode_contact"/>
<Button android:id="@+id/encode_location"
style="@android:style/Widget.Holo.Button.Borderless"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/encode_location"/>
<Button android:id="@+id/encode_hidden_data"
style="@android:style/Widget.Holo.Button.Borderless"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/encode_hidden_data"/>
<Button android:id="@+id/encode_bad_data"
style="@android:style/Widget.Holo.Button.Borderless"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/encode_bad_data"/>
<Button android:id="@+id/share_via_barcode"
style="@android:style/Widget.Holo.Button.Borderless"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/share_via_barcode"/>

View file

@ -17,75 +17,46 @@
package com.google.zxing.client.androidtest;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public final class BenchmarkActivity extends Activity {
private static final String TAG = BenchmarkActivity.class.getSimpleName();
private static final String PATH = Environment.getExternalStorageDirectory().getPath() + "/zxingbenchmark";
private View runBenchmarkButton;
private TextView textView;
private Thread benchmarkThread;
private AsyncTask<?,?,?> benchmarkTask;
private final Button.OnClickListener runBenchmark = new Button.OnClickListener() {
@Override
public void onClick(View v) {
if (benchmarkThread == null) {
if (benchmarkTask == null) {
String path = Environment.getExternalStorageDirectory().getPath() + "/zxingbenchmark";
benchmarkTask = new BenchmarkAsyncTask(BenchmarkActivity.this, path);
runBenchmarkButton.setEnabled(false);
textView.setText(R.string.benchmark_running);
benchmarkThread = new Thread(new BenchmarkThread(BenchmarkActivity.this, PATH));
benchmarkThread.start();
benchmarkTask.execute(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
};
private final Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
if (message.what == R.id.benchmark_done) {
@SuppressWarnings("unchecked")
Iterable<BenchmarkItem> items = (Iterable<BenchmarkItem>) message.obj;
int count = 0;
int time = 0;
for (BenchmarkItem item : items) {
if (item != null) {
Log.v(TAG, item.toString());
count++;
time += item.getAverageTime();
}
}
String totals = "TOTAL: Decoded " + count + " images in " + time + " us";
Log.v(TAG, totals);
textView.setText(totals + "\n\n" + getString(R.string.benchmark_help));
benchmarkThread = null;
runBenchmarkButton.setEnabled(true);
}
}
};
Handler getHandler() {
return handler;
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.benchmark);
runBenchmarkButton = findViewById(R.id.benchmark_run);
runBenchmarkButton.setOnClickListener(runBenchmark);
textView = (TextView) findViewById(R.id.benchmark_help);
benchmarkTask = null;
}
benchmarkThread = null;
void onBenchmarkDone(String message) {
textView.setText(message + "\n\n" + getString(R.string.benchmark_help));
runBenchmarkButton.setEnabled(true);
benchmarkTask = null;
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (C) 2008 ZXing authors
* Copyright (C) 2013 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -18,6 +18,9 @@ package com.google.zxing.client.androidtest;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Debug;
import android.util.Log;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.RGBLuminanceSource;
@ -25,61 +28,68 @@ import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
import android.os.Debug;
import android.os.Message;
import android.util.Log;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
final class BenchmarkThread implements Runnable {
public final class BenchmarkAsyncTask extends AsyncTask<Object,Object,String> {
private static final String TAG = BenchmarkThread.class.getSimpleName();
private static final String TAG = BenchmarkAsyncTask.class.getSimpleName();
private static final int RUNS = 10;
private final BenchmarkActivity activity;
private final BenchmarkActivity benchmarkActivity;
private final String path;
private MultiFormatReader multiFormatReader;
BenchmarkThread(BenchmarkActivity activity, String path) {
this.activity = activity;
BenchmarkAsyncTask(BenchmarkActivity benchmarkActivity, String path) {
this.benchmarkActivity = benchmarkActivity;
this.path = path;
}
@Override
public void run() {
multiFormatReader = new MultiFormatReader();
multiFormatReader.setHints(null);
protected String doInBackground(Object... params) {
MultiFormatReader reader = new MultiFormatReader();
reader.setHints(null);
// Try to get in a known state before starting the benchmark
System.gc();
List<BenchmarkItem> items = new ArrayList<BenchmarkItem>();
walkTree(path, items);
Message message = Message.obtain(activity.getHandler(), R.id.benchmark_done);
message.obj = items;
message.sendToTarget();
walkTree(reader, path, items);
int count = 0;
int time = 0;
for (BenchmarkItem item : items) {
if (item != null) {
Log.v(TAG, item.toString());
count++;
time += item.getAverageTime();
}
}
return "TOTAL: Decoded " + count + " images in " + time + " us";
}
// Recurse to allow subdirectories
private void walkTree(String currentPath, List<BenchmarkItem> items) {
@Override
protected void onPostExecute(String totals) {
benchmarkActivity.onBenchmarkDone(totals);
}
private static void walkTree(MultiFormatReader reader, String currentPath, List<BenchmarkItem> items) {
File file = new File(currentPath);
if (file.isDirectory()) {
String[] files = file.list();
Arrays.sort(files);
for (String fileName : files) {
walkTree(file.getAbsolutePath() + '/' + fileName, items);
walkTree(reader, file.getAbsolutePath() + '/' + fileName, items);
}
} else {
BenchmarkItem item = decode(currentPath);
BenchmarkItem item = decode(reader, currentPath);
if (item != null) {
items.add(item);
}
}
}
private BenchmarkItem decode(String path) {
private static BenchmarkItem decode(MultiFormatReader reader, String path) {
Bitmap imageBitmap = BitmapFactory.decodeFile(path);
if (imageBitmap == null) {
@ -103,7 +113,7 @@ final class BenchmarkThread implements Runnable {
long now = Debug.threadCpuTimeNanos();
try {
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
result = multiFormatReader.decodeWithState(bitmap);
result = reader.decodeWithState(bitmap);
success = true;
} catch (ReaderException ignored) {
success = false;
@ -118,4 +128,5 @@ final class BenchmarkThread implements Runnable {
return item;
}
}

View file

@ -253,7 +253,7 @@ public final class ZXingTestActivity extends Activity {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title);
builder.setMessage(message);
builder.setPositiveButton("OK", null);
builder.setPositiveButton(R.string.ok_button, null);
builder.show();
}