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