01cd15708e4e79f9fbd3baf9ad1d937949008eea
[zxing.git] / android / src / com / android / barcodes / DecodeThread.java
1 /*
2  * Copyright (C) 2008 Google Inc.
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.android.barcodes;
18
19 import android.content.SharedPreferences;
20 import android.os.Handler;
21 import android.os.Looper;
22 import android.os.Message;
23 import android.preference.PreferenceManager;
24 import com.google.zxing.BarcodeFormat;
25 import com.google.zxing.DecodeHintType;
26 import com.google.zxing.MonochromeBitmapSource;
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 Handler mHandler;
41     private BarcodesCaptureActivity mActivity;
42     private MultiFormatReader mMultiFormatReader;
43
44     DecodeThread(BarcodesCaptureActivity activity, String mode) {
45         mActivity = activity;
46         mMultiFormatReader = new MultiFormatReader();
47
48         // The prefs can't change while the thread is running, so pick them up once here.
49         if (mode == null || mode.length() == 0) {
50             SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
51             boolean decode1D = prefs.getBoolean(BarcodesPreferenceActivity.KEY_DECODE_1D, true);
52             boolean decodeQR = prefs.getBoolean(BarcodesPreferenceActivity.KEY_DECODE_QR, true);
53             if (decode1D && decodeQR) {
54                 setDecodeAllMode();
55             } else if (decode1D) {
56                 setDecode1DMode();
57             } else if (decodeQR) {
58                 setDecodeQRMode();
59             }
60         } else {
61             if (mode.equals(Intents.Scan.PRODUCT_MODE)) {
62                 setDecodeProductMode();
63             } else if (mode.equals(Intents.Scan.ONE_D_MODE)) {
64                 setDecode1DMode();
65             } else if (mode.equals(Intents.Scan.QR_CODE_MODE)) {
66                 setDecodeQRMode();
67             } else {
68                 setDecodeAllMode();
69             }
70         }
71     }
72
73     @Override
74     public void run() {
75         Looper.prepare();
76         mHandler = new Handler() {
77             public void handleMessage(Message message) {
78                 switch (message.what) {
79                     case R.id.decode:
80                         decode((byte[]) message.obj, message.arg1, message.arg2);
81                         break;
82                     case R.id.quit:
83                         Looper.myLooper().quit();
84                         break;
85                 }
86             }
87         };
88         Looper.loop();
89     }
90
91     private void setDecodeProductMode() {
92         Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
93         Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
94         vector.addElement(BarcodeFormat.UPC_A);
95         vector.addElement(BarcodeFormat.UPC_E);
96         vector.addElement(BarcodeFormat.EAN_13);
97         vector.addElement(BarcodeFormat.EAN_8);
98         hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
99         mMultiFormatReader.setHints(hints);
100     }
101
102     // TODO: This is fragile in case we add new formats. It would be better to have a new enum
103     // value which represented all 1D formats.
104     private void setDecode1DMode() {
105         Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
106         Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
107         vector.addElement(BarcodeFormat.UPC_A);
108         vector.addElement(BarcodeFormat.UPC_E);
109         vector.addElement(BarcodeFormat.EAN_13);
110         vector.addElement(BarcodeFormat.EAN_8);
111         vector.addElement(BarcodeFormat.CODE_39);
112         vector.addElement(BarcodeFormat.CODE_128);
113         hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
114         mMultiFormatReader.setHints(hints);
115     }
116
117     private void setDecodeQRMode() {
118         Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
119         Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
120         vector.addElement(BarcodeFormat.QR_CODE);
121         hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
122         mMultiFormatReader.setHints(hints);
123     }
124
125     private void setDecodeAllMode() {
126         mMultiFormatReader.setHints(null);
127     }
128
129     /**
130      * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
131      * reuse the same reader objects from one decode to the next.
132      *
133      * @param data   The YUV preview frame.
134      * @param width  The width of the preview frame.
135      * @param height The height of the preview frame.
136      */
137     private void decode(byte[] data, int width, int height) {
138         Date startDate = new Date();
139         boolean success;
140         Result rawResult = null;
141         try {
142             MonochromeBitmapSource source = new YUVMonochromeBitmapSource(data, width, height,
143                     CameraManager.get().getFramingRect());
144             rawResult = mMultiFormatReader.decodeWithState(source);
145             success = true;
146         } catch (ReaderException e) {
147             success = false;
148         }
149         Date endDate = new Date();
150
151         if (success) {
152             Message message = Message.obtain(mActivity.mHandler, R.id.decode_succeeded, rawResult);
153             message.arg1 = (int) (endDate.getTime() - startDate.getTime());
154             message.sendToTarget();
155         } else {
156             Message message = Message.obtain(mActivity.mHandler, R.id.decode_failed);
157             message.arg1 = (int) (endDate.getTime() - startDate.getTime());
158             message.sendToTarget();
159         }
160     }
161
162 }