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

View file

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

View file

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

View file

@ -17,75 +17,46 @@
package com.google.zxing.client.androidtest; package com.google.zxing.client.androidtest;
import android.app.Activity; import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.TextView; import android.widget.TextView;
public final class BenchmarkActivity extends Activity { 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 View runBenchmarkButton;
private TextView textView; private TextView textView;
private Thread benchmarkThread; private AsyncTask<?,?,?> benchmarkTask;
private final Button.OnClickListener runBenchmark = new Button.OnClickListener() { private final Button.OnClickListener runBenchmark = new Button.OnClickListener() {
@Override @Override
public void onClick(View v) { 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); runBenchmarkButton.setEnabled(false);
textView.setText(R.string.benchmark_running); textView.setText(R.string.benchmark_running);
benchmarkThread = new Thread(new BenchmarkThread(BenchmarkActivity.this, PATH)); benchmarkTask.execute(AsyncTask.THREAD_POOL_EXECUTOR);
benchmarkThread.start();
} }
} }
}; };
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 @Override
public void onCreate(Bundle icicle) { public void onCreate(Bundle icicle) {
super.onCreate(icicle); super.onCreate(icicle);
setContentView(R.layout.benchmark); setContentView(R.layout.benchmark);
runBenchmarkButton = findViewById(R.id.benchmark_run); runBenchmarkButton = findViewById(R.id.benchmark_run);
runBenchmarkButton.setOnClickListener(runBenchmark); runBenchmarkButton.setOnClickListener(runBenchmark);
textView = (TextView) findViewById(R.id.benchmark_help); 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.Bitmap;
import android.graphics.BitmapFactory; 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.BinaryBitmap;
import com.google.zxing.MultiFormatReader; import com.google.zxing.MultiFormatReader;
import com.google.zxing.RGBLuminanceSource; import com.google.zxing.RGBLuminanceSource;
@ -25,61 +28,68 @@ import com.google.zxing.ReaderException;
import com.google.zxing.Result; import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer; import com.google.zxing.common.HybridBinarizer;
import android.os.Debug;
import android.os.Message;
import android.util.Log;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; 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 static final int RUNS = 10;
private final BenchmarkActivity activity; private final BenchmarkActivity benchmarkActivity;
private final String path; private final String path;
private MultiFormatReader multiFormatReader;
BenchmarkThread(BenchmarkActivity activity, String path) { BenchmarkAsyncTask(BenchmarkActivity benchmarkActivity, String path) {
this.activity = activity; this.benchmarkActivity = benchmarkActivity;
this.path = path; this.path = path;
} }
@Override @Override
public void run() { protected String doInBackground(Object... params) {
multiFormatReader = new MultiFormatReader(); MultiFormatReader reader = new MultiFormatReader();
multiFormatReader.setHints(null); reader.setHints(null);
// Try to get in a known state before starting the benchmark // Try to get in a known state before starting the benchmark
System.gc(); System.gc();
List<BenchmarkItem> items = new ArrayList<BenchmarkItem>(); List<BenchmarkItem> items = new ArrayList<BenchmarkItem>();
walkTree(path, items); walkTree(reader, path, items);
Message message = Message.obtain(activity.getHandler(), R.id.benchmark_done);
message.obj = items; int count = 0;
message.sendToTarget(); 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 @Override
private void walkTree(String currentPath, List<BenchmarkItem> items) { protected void onPostExecute(String totals) {
benchmarkActivity.onBenchmarkDone(totals);
}
private static void walkTree(MultiFormatReader reader, String currentPath, List<BenchmarkItem> items) {
File file = new File(currentPath); File file = new File(currentPath);
if (file.isDirectory()) { if (file.isDirectory()) {
String[] files = file.list(); String[] files = file.list();
Arrays.sort(files); Arrays.sort(files);
for (String fileName : files) { for (String fileName : files) {
walkTree(file.getAbsolutePath() + '/' + fileName, items); walkTree(reader, file.getAbsolutePath() + '/' + fileName, items);
} }
} else { } else {
BenchmarkItem item = decode(currentPath); BenchmarkItem item = decode(reader, currentPath);
if (item != null) { if (item != null) {
items.add(item); items.add(item);
} }
} }
} }
private BenchmarkItem decode(String path) { private static BenchmarkItem decode(MultiFormatReader reader, String path) {
Bitmap imageBitmap = BitmapFactory.decodeFile(path); Bitmap imageBitmap = BitmapFactory.decodeFile(path);
if (imageBitmap == null) { if (imageBitmap == null) {
@ -103,7 +113,7 @@ final class BenchmarkThread implements Runnable {
long now = Debug.threadCpuTimeNanos(); long now = Debug.threadCpuTimeNanos();
try { try {
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
result = multiFormatReader.decodeWithState(bitmap); result = reader.decodeWithState(bitmap);
success = true; success = true;
} catch (ReaderException ignored) { } catch (ReaderException ignored) {
success = false; success = false;
@ -118,4 +128,5 @@ final class BenchmarkThread implements Runnable {
return item; return item;
} }
} }

View file

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