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">
<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"/>

View file

@ -16,6 +16,8 @@
package com.google.zxing.client.androidtest;
import java.io.File;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
@ -37,7 +39,8 @@ public final class BenchmarkActivity extends Activity {
public void onClick(View v) {
if (benchmarkTask == null) {
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);
textView.setText(R.string.benchmark_running);
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 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.path = path;
this.file = file;
}
@Override
@ -57,7 +57,7 @@ public final class BenchmarkAsyncTask extends AsyncTask<Object,Object,String> {
System.gc();
List<BenchmarkItem> items = new ArrayList<>();
walkTree(reader, path, items);
walkTree(reader, file, items);
int count = 0;
int time = 0;
@ -76,27 +76,31 @@ public final class BenchmarkAsyncTask extends AsyncTask<Object,Object,String> {
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(reader, file.getAbsolutePath() + '/' + fileName, items);
private static void walkTree(MultiFormatReader reader,
File fileOrDir,
List<BenchmarkItem> items) {
Log.i(TAG, "Decoding " + fileOrDir);
if (fileOrDir.isDirectory()) {
File[] files = fileOrDir.listFiles();
if (files != null) {
Arrays.sort(files);
for (File file : files) {
walkTree(reader, file, items);
}
}
} else {
BenchmarkItem item = decode(reader, currentPath);
BenchmarkItem item = decode(reader, fileOrDir);
if (item != null) {
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) {
Log.e(TAG, "Couldn't open " + path);
Log.e(TAG, "Couldn't open " + file);
return null;
}
@ -107,7 +111,7 @@ public final class BenchmarkAsyncTask extends AsyncTask<Object,Object,String> {
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++) {
boolean success;
Result result = null;

View file

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