2 * Copyright (C) 2008 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.androidtest;
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;
26 import java.io.FileNotFoundException;
27 import java.util.Date;
28 import java.util.Vector;
30 final class BenchmarkThread extends Thread {
32 private static final String TAG = "BenchmarkThread";
33 private static final int RUNS = 10;
35 private BenchmarkActivity mActivity;
37 private MultiFormatReader mMultiFormatReader;
39 BenchmarkThread(BenchmarkActivity activity, String path) {
46 mMultiFormatReader = new MultiFormatReader();
47 mMultiFormatReader.setHints(null);
49 Vector<BenchmarkItem> items = new Vector<BenchmarkItem>();
50 walkTree(mPath, items);
51 Message message = Message.obtain(mActivity.mHandler, R.id.benchmark_done);
53 message.sendToTarget();
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);
65 BenchmarkItem item = decode(path);
67 items.addElement(item);
72 private BenchmarkItem decode(String path) {
73 RGBMonochromeBitmapSource source = null;
75 source = new RGBMonochromeBitmapSource(path);
76 } catch (FileNotFoundException e) {
77 Log.e(TAG, e.toString());
81 BenchmarkItem item = new BenchmarkItem(path, RUNS);
82 for (int x = 0; x < RUNS; x++) {
83 Date startDate = new Date();
87 result = mMultiFormatReader.decodeWithState(source);
89 } catch (ReaderException e) {
92 Date endDate = new Date();
94 item.setDecoded(success);
95 item.setFormat(result != null ? result.getBarcodeFormat() : null);
97 item.addResult((int) (endDate.getTime() - startDate.getTime()));