Finished work on the local binarizer and renamed it to HybridBinarizer. It uses the...
[zxing.git] / androidtest / src / com / google / zxing / client / androidtest / BenchmarkThread.java
1 /*
2  * Copyright (C) 2008 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.client.androidtest;
18
19 import com.google.zxing.BinaryBitmap;
20 import com.google.zxing.MultiFormatReader;
21 import com.google.zxing.ReaderException;
22 import com.google.zxing.Result;
23 import com.google.zxing.common.HybridBinarizer;
24
25 import android.os.Debug;
26 import android.os.Message;
27 import android.util.Log;
28
29 import java.io.File;
30 import java.io.FileNotFoundException;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
34
35 final class BenchmarkThread extends Thread {
36
37   private static final String TAG = "BenchmarkThread";
38   private static final int RUNS = 10;
39
40   private final BenchmarkActivity mActivity;
41   private final String mPath;
42   private MultiFormatReader mMultiFormatReader;
43
44   BenchmarkThread(BenchmarkActivity activity, String path) {
45     mActivity = activity;
46     mPath = path;
47   }
48
49   @Override
50   public void run() {
51     mMultiFormatReader = new MultiFormatReader();
52     mMultiFormatReader.setHints(null);
53     // Try to get in a known state before starting the benchmark
54     System.gc();
55
56     List<BenchmarkItem> items = new ArrayList<BenchmarkItem>();
57     walkTree(mPath, items);
58     Message message = Message.obtain(mActivity.mHandler, R.id.benchmark_done);
59     message.obj = items;
60     message.sendToTarget();
61   }
62
63   // Recurse to allow subdirectories
64   private void walkTree(String path, List<BenchmarkItem> items) {
65     File file = new File(path);
66     if (file.isDirectory()) {
67       String[] files = file.list();
68       Arrays.sort(files);
69       for (int x = 0; x < files.length; x++) {
70         walkTree(file.getAbsolutePath() + '/' + files[x], items);
71       }
72     } else {
73       BenchmarkItem item = decode(path);
74       if (item != null) {
75         items.add(item);
76       }
77     }
78   }
79
80   private BenchmarkItem decode(String path) {
81     RGBLuminanceSource source;
82     try {
83       source = new RGBLuminanceSource(path);
84     } catch (FileNotFoundException e) {
85       Log.e(TAG, e.toString());
86       return null;
87     }
88
89     BenchmarkItem item = new BenchmarkItem(path, RUNS);
90     for (int x = 0; x < RUNS; x++) {
91       boolean success;
92       Result result = null;
93       // Using this call instead of getting the time should eliminate a lot of variability due to
94       // scheduling and what else is happening in the system.
95       long now = Debug.threadCpuTimeNanos();
96       try {
97         BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
98         result = mMultiFormatReader.decodeWithState(bitmap);
99         success = true;
100       } catch (ReaderException e) {
101         success = false;
102       }
103       now = Debug.threadCpuTimeNanos() - now;
104       if (x == 0) {
105         item.setDecoded(success);
106         item.setFormat(result != null ? result.getBarcodeFormat() : null);
107       }
108       item.addResult((int) (now / 1000));
109     }
110     return item;
111   }
112
113 }