Did a bunch of renaming, there was no need for the Barcodes prefix.
[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.Date;
32 import java.util.Hashtable;
33 import java.util.Vector;
34
35 /**
36  * This thread does all the heavy lifting of decoding the images.
37  */
38 final class DecodeThread extends Thread {
39
40   public static final String BARCODE_BITMAP = "barcode_bitmap";
41
42   public Handler mHandler;
43   private CaptureActivity mActivity;
44   private MultiFormatReader mMultiFormatReader;
45
46   DecodeThread(CaptureActivity activity, String mode) {
47     mActivity = activity;
48     mMultiFormatReader = new MultiFormatReader();
49
50     // The prefs can't change while the thread is running, so pick them up once here.
51     if (mode == null || mode.length() == 0) {
52       SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
53       boolean decode1D = prefs.getBoolean(PreferencesActivity.KEY_DECODE_1D, true);
54       boolean decodeQR = prefs.getBoolean(PreferencesActivity.KEY_DECODE_QR, true);
55       if (decode1D && decodeQR) {
56         setDecodeAllMode();
57       } else if (decode1D) {
58         setDecode1DMode();
59       } else if (decodeQR) {
60         setDecodeQRMode();
61       }
62     } else {
63       if (mode.equals(Intents.Scan.PRODUCT_MODE)) {
64         setDecodeProductMode();
65       } else if (mode.equals(Intents.Scan.ONE_D_MODE)) {
66         setDecode1DMode();
67       } else if (mode.equals(Intents.Scan.QR_CODE_MODE)) {
68         setDecodeQRMode();
69       } else {
70         setDecodeAllMode();
71       }
72     }
73   }
74
75   @Override
76   public void run() {
77     Looper.prepare();
78     mHandler = new Handler() {
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     hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
116     mMultiFormatReader.setHints(hints);
117   }
118
119   private void setDecodeQRMode() {
120     Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
121     Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
122     vector.addElement(BarcodeFormat.QR_CODE);
123     hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
124     mMultiFormatReader.setHints(hints);
125   }
126
127   private void setDecodeAllMode() {
128     mMultiFormatReader.setHints(null);
129   }
130
131   /**
132    * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
133    * reuse the same reader objects from one decode to the next.
134    *
135    * @param data   The YUV preview frame.
136    * @param width  The width of the preview frame.
137    * @param height The height of the preview frame.
138    */
139   private void decode(byte[] data, int width, int height) {
140     Date startDate = new Date();
141     boolean success;
142     Result rawResult = null;
143     YUVMonochromeBitmapSource source = new YUVMonochromeBitmapSource(data, width, height,
144         CameraManager.get().getFramingRect());
145     try {
146       rawResult = mMultiFormatReader.decodeWithState(source);
147       success = true;
148     } catch (ReaderException e) {
149       success = false;
150     }
151     Date endDate = new Date();
152
153     if (success) {
154       Message message = Message.obtain(mActivity.mHandler, R.id.decode_succeeded, rawResult);
155       message.arg1 = (int) (endDate.getTime() - startDate.getTime());
156       Bundle bundle = new Bundle();
157       bundle.putParcelable(BARCODE_BITMAP, source.renderToBitmap());
158       message.setData(bundle);
159       message.sendToTarget();
160     } else {
161       Message message = Message.obtain(mActivity.mHandler, R.id.decode_failed);
162       message.arg1 = (int) (endDate.getTime() - startDate.getTime());
163       message.sendToTarget();
164     }
165   }
166
167 }