Turned on ITF support in the Android client, and fixed a bug in the ITF result points...
[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   // TODO: This is fragile in case we add new formats. It would be better to have a new enum
105   // value which represented all 1D formats.
106   private void setDecode1DMode() {
107     Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
108     Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
109     vector.addElement(BarcodeFormat.UPC_A);
110     vector.addElement(BarcodeFormat.UPC_E);
111     vector.addElement(BarcodeFormat.EAN_13);
112     vector.addElement(BarcodeFormat.EAN_8);
113     vector.addElement(BarcodeFormat.CODE_39);
114     vector.addElement(BarcodeFormat.CODE_128);
115     vector.addElement(BarcodeFormat.ITF);
116     hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
117     mMultiFormatReader.setHints(hints);
118   }
119
120   private void setDecodeQRMode() {
121     Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
122     Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
123     vector.addElement(BarcodeFormat.QR_CODE);
124     hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
125     mMultiFormatReader.setHints(hints);
126   }
127
128   /**
129    * Instead of calling setHints(null), which would allow new formats to sneak in, we
130    * explicitly set which formats are available.
131    */
132   private void setDecodeAllMode() {
133     Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
134     Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
135     vector.addElement(BarcodeFormat.UPC_A);
136     vector.addElement(BarcodeFormat.UPC_E);
137     vector.addElement(BarcodeFormat.EAN_13);
138     vector.addElement(BarcodeFormat.EAN_8);
139     vector.addElement(BarcodeFormat.CODE_39);
140     vector.addElement(BarcodeFormat.CODE_128);
141     vector.addElement(BarcodeFormat.ITF);
142     vector.addElement(BarcodeFormat.QR_CODE);
143     hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
144     mMultiFormatReader.setHints(hints);
145   }
146
147   /**
148    * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
149    * reuse the same reader objects from one decode to the next.
150    *
151    * @param data   The YUV preview frame.
152    * @param width  The width of the preview frame.
153    * @param height The height of the preview frame.
154    */
155   private void decode(byte[] data, int width, int height) {
156     long start = System.currentTimeMillis();
157     boolean success;
158     Result rawResult = null;
159     YUVMonochromeBitmapSource source = new YUVMonochromeBitmapSource(data, width, height,
160         CameraManager.get().getFramingRect());
161     try {
162       rawResult = mMultiFormatReader.decodeWithState(source);
163       success = true;
164     } catch (ReaderException e) {
165       success = false;
166     }
167     long end = System.currentTimeMillis();
168
169     if (success) {
170       Message message = Message.obtain(mActivity.mHandler, R.id.decode_succeeded, rawResult);
171       message.arg1 = (int) (end - start);
172       Bundle bundle = new Bundle();
173       bundle.putParcelable(BARCODE_BITMAP, source.renderToBitmap());
174       message.setData(bundle);
175       message.sendToTarget();
176     } else {
177       Message message = Message.obtain(mActivity.mHandler, R.id.decode_failed);
178       message.arg1 = (int) (end - start);
179       message.sendToTarget();
180     }
181   }
182
183 }