Small speedups in time-related code
[zxing.git] / android / src / com / google / zxing / client / android / DecodeThread.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.android;
18
19 import android.content.SharedPreferences;
20 import android.os.Bundle;
21 import android.os.Handler;
22 import android.os.Looper;
23 import android.os.Message;
24 import android.preference.PreferenceManager;
25 import com.google.zxing.BarcodeFormat;
26 import com.google.zxing.DecodeHintType;
27 import com.google.zxing.MultiFormatReader;
28 import com.google.zxing.ReaderException;
29 import com.google.zxing.Result;
30
31 import java.util.Hashtable;
32 import java.util.Vector;
33
34 /**
35  * This thread does all the heavy lifting of decoding the images.
36  */
37 final class DecodeThread extends Thread {
38
39   public static final String BARCODE_BITMAP = "barcode_bitmap";
40
41   public Handler mHandler;
42   private final CaptureActivity mActivity;
43   private final MultiFormatReader mMultiFormatReader;
44
45   DecodeThread(CaptureActivity activity, String mode) {
46     mActivity = activity;
47     mMultiFormatReader = new MultiFormatReader();
48
49     // The prefs can't change while the thread is running, so pick them up once here.
50     if (mode == null || mode.length() == 0) {
51       SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
52       boolean decode1D = prefs.getBoolean(PreferencesActivity.KEY_DECODE_1D, true);
53       boolean decodeQR = prefs.getBoolean(PreferencesActivity.KEY_DECODE_QR, true);
54       if (decode1D && decodeQR) {
55         setDecodeAllMode();
56       } else if (decode1D) {
57         setDecode1DMode();
58       } else if (decodeQR) {
59         setDecodeQRMode();
60       }
61     } else {
62       if (mode.equals(Intents.Scan.PRODUCT_MODE)) {
63         setDecodeProductMode();
64       } else if (mode.equals(Intents.Scan.ONE_D_MODE)) {
65         setDecode1DMode();
66       } else if (mode.equals(Intents.Scan.QR_CODE_MODE)) {
67         setDecodeQRMode();
68       } else {
69         setDecodeAllMode();
70       }
71     }
72   }
73
74   @Override
75   public void run() {
76     Looper.prepare();
77     mHandler = new Handler() {
78       public void handleMessage(Message message) {
79         switch (message.what) {
80           case R.id.decode:
81             decode((byte[]) message.obj, message.arg1, message.arg2);
82             break;
83           case R.id.quit:
84             Looper.myLooper().quit();
85             break;
86         }
87       }
88     };
89     Looper.loop();
90   }
91
92   private void setDecodeProductMode() {
93     Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
94     Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
95     vector.addElement(BarcodeFormat.UPC_A);
96     vector.addElement(BarcodeFormat.UPC_E);
97     vector.addElement(BarcodeFormat.EAN_13);
98     vector.addElement(BarcodeFormat.EAN_8);
99     hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
100     mMultiFormatReader.setHints(hints);
101   }
102
103   // TODO: This is fragile in case we add new formats. It would be better to have a new enum
104   // value which represented all 1D formats.
105   private void setDecode1DMode() {
106     Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
107     Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
108     vector.addElement(BarcodeFormat.UPC_A);
109     vector.addElement(BarcodeFormat.UPC_E);
110     vector.addElement(BarcodeFormat.EAN_13);
111     vector.addElement(BarcodeFormat.EAN_8);
112     vector.addElement(BarcodeFormat.CODE_39);
113     vector.addElement(BarcodeFormat.CODE_128);
114     hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
115     mMultiFormatReader.setHints(hints);
116   }
117
118   private void setDecodeQRMode() {
119     Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
120     Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
121     vector.addElement(BarcodeFormat.QR_CODE);
122     hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
123     mMultiFormatReader.setHints(hints);
124   }
125
126   /**
127    * Instead of calling setHints(null), which would allow new formats like ITF to sneak in, we
128    * explicitly set which formats are available.
129    */
130   private void setDecodeAllMode() {
131     Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
132     Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
133     vector.addElement(BarcodeFormat.UPC_A);
134     vector.addElement(BarcodeFormat.UPC_E);
135     vector.addElement(BarcodeFormat.EAN_13);
136     vector.addElement(BarcodeFormat.EAN_8);
137     vector.addElement(BarcodeFormat.CODE_39);
138     vector.addElement(BarcodeFormat.CODE_128);
139     vector.addElement(BarcodeFormat.QR_CODE);
140     hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
141     mMultiFormatReader.setHints(hints);
142   }
143
144   /**
145    * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
146    * reuse the same reader objects from one decode to the next.
147    *
148    * @param data   The YUV preview frame.
149    * @param width  The width of the preview frame.
150    * @param height The height of the preview frame.
151    */
152   private void decode(byte[] data, int width, int height) {
153     long start = System.currentTimeMillis();
154     boolean success;
155     Result rawResult = null;
156     YUVMonochromeBitmapSource source = new YUVMonochromeBitmapSource(data, width, height,
157         CameraManager.get().getFramingRect());
158     try {
159       rawResult = mMultiFormatReader.decodeWithState(source);
160       success = true;
161     } catch (ReaderException e) {
162       success = false;
163     }
164     long end = System.currentTimeMillis();
165
166     if (success) {
167       Message message = Message.obtain(mActivity.mHandler, R.id.decode_succeeded, rawResult);
168       message.arg1 = (int) (end - start);
169       Bundle bundle = new Bundle();
170       bundle.putParcelable(BARCODE_BITMAP, source.renderToBitmap());
171       message.setData(bundle);
172       message.sendToTarget();
173     } else {
174       Message message = Message.obtain(mActivity.mHandler, R.id.decode_failed);
175       message.arg1 = (int) (end - start);
176       message.sendToTarget();
177     }
178   }
179
180 }