Wrote a benchmark activity for Android which reads images recursively from the SD...
[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 android.os.Message;
20 import android.util.Log;
21 import com.google.zxing.MultiFormatReader;
22 import com.google.zxing.ReaderException;
23 import com.google.zxing.Result;
24
25 import java.io.File;
26 import java.io.FileNotFoundException;
27 import java.util.Date;
28 import java.util.Vector;
29
30 final class BenchmarkThread extends Thread {
31
32   private static final String TAG = "BenchmarkThread";
33   private static final int RUNS = 10;
34
35   private BenchmarkActivity mActivity;
36   private String mPath;
37   private MultiFormatReader mMultiFormatReader;
38
39   BenchmarkThread(BenchmarkActivity activity, String path) {
40     mActivity = activity;
41     mPath = path;
42   }
43
44   @Override
45   public void run() {
46     mMultiFormatReader = new MultiFormatReader();
47     mMultiFormatReader.setHints(null);
48
49     Vector<BenchmarkItem> items = new Vector<BenchmarkItem>();
50     walkTree(mPath, items);
51     Message message = Message.obtain(mActivity.mHandler, R.id.benchmark_done);
52     message.obj = items;
53     message.sendToTarget();
54   }
55
56   // Recurse to allow subdirectories
57   private void walkTree(String path, Vector<BenchmarkItem> items) {
58     File file = new File(path);
59     if (file.isDirectory()) {
60       String[] files = file.list();
61       for (int x = 0; x < files.length; x++) {
62         walkTree(file.getAbsolutePath() + "/" + files[x], items);
63       }
64     } else {
65       BenchmarkItem item = decode(path);
66       if (item != null) {
67         items.addElement(item);
68       }
69     }
70   }
71
72   private BenchmarkItem decode(String path) {
73     RGBMonochromeBitmapSource source = null;
74     try {
75       source = new RGBMonochromeBitmapSource(path);
76     } catch (FileNotFoundException e) {
77       Log.e(TAG, e.toString());
78       return null;
79     }
80
81     BenchmarkItem item = new BenchmarkItem(path, RUNS);
82     for (int x = 0; x < RUNS; x++) {
83       Date startDate = new Date();
84       boolean success;
85       Result result = null;
86       try {
87         result = mMultiFormatReader.decodeWithState(source);
88         success = true;
89       } catch (ReaderException e) {
90         success = false;
91       }
92       Date endDate = new Date();
93       if (x == 0) {
94         item.setDecoded(success);
95         item.setFormat(result != null ? result.getBarcodeFormat() : null);
96       }
97       item.addResult((int) (endDate.getTime() - startDate.getTime()));
98     }
99     return item;
100   }
101
102 }