Fixed or removed a bunch of TODOs, and enforced the 100 columns limit in a bunch...
[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       @Override
79       public void handleMessage(Message message) {
80         switch (message.what) {
81           case R.id.decode:
82             decode((byte[]) message.obj, message.arg1, message.arg2);
83             break;
84           case R.id.quit:
85             Looper.myLooper().quit();
86             break;
87         }
88       }
89     };
90     Looper.loop();
91   }
92
93   private void setDecodeProductMode() {
94     Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
95     Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
96     vector.addElement(BarcodeFormat.UPC_A);
97     vector.addElement(BarcodeFormat.UPC_E);
98     vector.addElement(BarcodeFormat.EAN_13);
99     vector.addElement(BarcodeFormat.EAN_8);
100     hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
101     mMultiFormatReader.setHints(hints);
102   }
103
104   /**
105    * Select the 1D formats we want this client to decode by hand.
106    */
107   private void setDecode1DMode() {
108     Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
109     Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
110     vector.addElement(BarcodeFormat.UPC_A);
111     vector.addElement(BarcodeFormat.UPC_E);
112     vector.addElement(BarcodeFormat.EAN_13);
113     vector.addElement(BarcodeFormat.EAN_8);
114     vector.addElement(BarcodeFormat.CODE_39);
115     vector.addElement(BarcodeFormat.CODE_128);
116     vector.addElement(BarcodeFormat.ITF);
117     hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
118     mMultiFormatReader.setHints(hints);
119   }
120
121   private void setDecodeQRMode() {
122     Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
123     Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
124     vector.addElement(BarcodeFormat.QR_CODE);
125     hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
126     mMultiFormatReader.setHints(hints);
127   }
128
129   /**
130    * Instead of calling setHints(null), which would allow new formats to sneak in, we
131    * explicitly set which formats are available.
132    */
133   private void setDecodeAllMode() {
134     Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
135     Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
136     vector.addElement(BarcodeFormat.UPC_A);
137     vector.addElement(BarcodeFormat.UPC_E);
138     vector.addElement(BarcodeFormat.EAN_13);
139     vector.addElement(BarcodeFormat.EAN_8);
140     vector.addElement(BarcodeFormat.CODE_39);
141     vector.addElement(BarcodeFormat.CODE_128);
142     vector.addElement(BarcodeFormat.ITF);
143     vector.addElement(BarcodeFormat.QR_CODE);
144     hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
145     mMultiFormatReader.setHints(hints);
146   }
147
148   /**
149    * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
150    * reuse the same reader objects from one decode to the next.
151    *
152    * @param data   The YUV preview frame.
153    * @param width  The width of the preview frame.
154    * @param height The height of the preview frame.
155    */
156   private void decode(byte[] data, int width, int height) {
157     long start = System.currentTimeMillis();
158     boolean success;
159     Result rawResult = null;
160     YUVMonochromeBitmapSource source = new YUVMonochromeBitmapSource(data, width, height,
161         CameraManager.get().getFramingRect());
162     try {
163       rawResult = mMultiFormatReader.decodeWithState(source);
164       success = true;
165     } catch (ReaderException e) {
166       success = false;
167     }
168     long end = System.currentTimeMillis();
169
170     if (success) {
171       Message message = Message.obtain(mActivity.mHandler, R.id.decode_succeeded, rawResult);
172       message.arg1 = (int) (end - start);
173       Bundle bundle = new Bundle();
174       bundle.putParcelable(BARCODE_BITMAP, source.renderToBitmap());
175       message.setData(bundle);
176       message.sendToTarget();
177     } else {
178       Message message = Message.obtain(mActivity.mHandler, R.id.decode_failed);
179       message.arg1 = (int) (end - start);
180       message.sendToTarget();
181     }
182   }
183
184 }