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