Fix camera/storage permissions problems in test app; refactor handling of files in benchmark

This commit is contained in:
Sean Owen 2017-01-31 15:21:49 +00:00
parent 27b686e132
commit e8d9002390
No known key found for this signature in database
GPG key ID: F6CE9695C9318406
4 changed files with 32 additions and 22 deletions

View file

@ -21,8 +21,9 @@
android:installLocation="auto"> android:installLocation="auto">
<uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="24"/> <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="22"/>
<uses-feature android:name="android.hardware.camera.any"/> <uses-feature android:name="android.hardware.camera.any"/>

View file

@ -16,6 +16,8 @@
package com.google.zxing.client.androidtest; package com.google.zxing.client.androidtest;
import java.io.File;
import android.app.Activity; import android.app.Activity;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
@ -37,7 +39,8 @@ public final class BenchmarkActivity extends Activity {
public void onClick(View v) { public void onClick(View v) {
if (benchmarkTask == null) { if (benchmarkTask == null) {
String path = Environment.getExternalStorageDirectory().getPath() + "/zxingbenchmark"; String path = Environment.getExternalStorageDirectory().getPath() + "/zxingbenchmark";
benchmarkTask = new BenchmarkAsyncTask(BenchmarkActivity.this, path); File dir = new File(path, "zxingbenchmark");
benchmarkTask = new BenchmarkAsyncTask(BenchmarkActivity.this, dir);
runBenchmarkButton.setEnabled(false); runBenchmarkButton.setEnabled(false);
textView.setText(R.string.benchmark_running); textView.setText(R.string.benchmark_running);
benchmarkTask.execute(AsyncTask.THREAD_POOL_EXECUTOR); benchmarkTask.execute(AsyncTask.THREAD_POOL_EXECUTOR);

View file

@ -42,11 +42,11 @@ public final class BenchmarkAsyncTask extends AsyncTask<Object,Object,String> {
private static final int RUNS = 10; private static final int RUNS = 10;
private final BenchmarkActivity benchmarkActivity; private final BenchmarkActivity benchmarkActivity;
private final String path; private final File file;
BenchmarkAsyncTask(BenchmarkActivity benchmarkActivity, String path) { BenchmarkAsyncTask(BenchmarkActivity benchmarkActivity, File file) {
this.benchmarkActivity = benchmarkActivity; this.benchmarkActivity = benchmarkActivity;
this.path = path; this.file = file;
} }
@Override @Override
@ -57,7 +57,7 @@ public final class BenchmarkAsyncTask extends AsyncTask<Object,Object,String> {
System.gc(); System.gc();
List<BenchmarkItem> items = new ArrayList<>(); List<BenchmarkItem> items = new ArrayList<>();
walkTree(reader, path, items); walkTree(reader, file, items);
int count = 0; int count = 0;
int time = 0; int time = 0;
@ -76,27 +76,31 @@ public final class BenchmarkAsyncTask extends AsyncTask<Object,Object,String> {
benchmarkActivity.onBenchmarkDone(totals); benchmarkActivity.onBenchmarkDone(totals);
} }
private static void walkTree(MultiFormatReader reader, String currentPath, List<BenchmarkItem> items) { private static void walkTree(MultiFormatReader reader,
File file = new File(currentPath); File fileOrDir,
if (file.isDirectory()) { List<BenchmarkItem> items) {
String[] files = file.list(); Log.i(TAG, "Decoding " + fileOrDir);
if (fileOrDir.isDirectory()) {
File[] files = fileOrDir.listFiles();
if (files != null) {
Arrays.sort(files); Arrays.sort(files);
for (String fileName : files) { for (File file : files) {
walkTree(reader, file.getAbsolutePath() + '/' + fileName, items); walkTree(reader, file, items);
}
} }
} else { } else {
BenchmarkItem item = decode(reader, currentPath); BenchmarkItem item = decode(reader, fileOrDir);
if (item != null) { if (item != null) {
items.add(item); items.add(item);
} }
} }
} }
private static BenchmarkItem decode(MultiFormatReader reader, String path) { private static BenchmarkItem decode(MultiFormatReader reader, File file) {
Bitmap imageBitmap = BitmapFactory.decodeFile(path); Bitmap imageBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
if (imageBitmap == null) { if (imageBitmap == null) {
Log.e(TAG, "Couldn't open " + path); Log.e(TAG, "Couldn't open " + file);
return null; return null;
} }
@ -107,7 +111,7 @@ public final class BenchmarkAsyncTask extends AsyncTask<Object,Object,String> {
RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels); RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
BenchmarkItem item = new BenchmarkItem(path, RUNS); BenchmarkItem item = new BenchmarkItem(file, RUNS);
for (int x = 0; x < RUNS; x++) { for (int x = 0; x < RUNS; x++) {
boolean success; boolean success;
Result result = null; Result result = null;

View file

@ -16,21 +16,23 @@
package com.google.zxing.client.androidtest; package com.google.zxing.client.androidtest;
import java.io.File;
import com.google.zxing.BarcodeFormat; import com.google.zxing.BarcodeFormat;
final class BenchmarkItem { final class BenchmarkItem {
private final String path; private final File file;
private final int[] times; private final int[] times;
private int position; private int position;
private boolean decoded; private boolean decoded;
private BarcodeFormat format; private BarcodeFormat format;
BenchmarkItem(String path, int runs) { BenchmarkItem(File file, int runs) {
if (runs <= 0) { if (runs <= 0) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
this.path = path; this.file = file;
times = new int[runs]; times = new int[runs];
position = 0; position = 0;
decoded = false; decoded = false;
@ -52,7 +54,7 @@ final class BenchmarkItem {
@Override @Override
public String toString() { public String toString() {
return (decoded ? "DECODED " + format + ": " : "FAILED: ") + path + return (decoded ? "DECODED " + format + ": " : "FAILED: ") + file +
" (" + getAverageTime() + " us average)"; " (" + getAverageTime() + " us average)";
} }