Pre-RSS-14 changes. Necessary code changes, but not the decoder. Committing this...
[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 com.google.zxing.BarcodeFormat;
20 import com.google.zxing.BinaryBitmap;
21 import com.google.zxing.DecodeHintType;
22 import com.google.zxing.MultiFormatReader;
23 import com.google.zxing.ReaderException;
24 import com.google.zxing.Result;
25 import com.google.zxing.ResultPointCallback;
26 import com.google.zxing.common.HybridBinarizer;
27
28 import android.content.SharedPreferences;
29 import android.os.Bundle;
30 import android.os.Handler;
31 import android.os.Looper;
32 import android.os.Message;
33 import android.preference.PreferenceManager;
34 import android.util.Log;
35
36 import java.util.Hashtable;
37 import java.util.Vector;
38
39 /**
40  * This thread does all the heavy lifting of decoding the images.
41  *
42  * @author dswitkin@google.com (Daniel Switkin)
43  */
44 final class DecodeThread extends Thread {
45
46   public static final String BARCODE_BITMAP = "barcode_bitmap";
47   private static final String TAG = "DecodeThread";
48
49   private Handler handler;
50   private final CaptureActivity activity;
51   private final MultiFormatReader multiFormatReader;
52   private final ResultPointCallback resultPointCallback;
53
54   DecodeThread(CaptureActivity activity, String mode, ResultPointCallback resultPointCallback) {
55     this.activity = activity;
56     multiFormatReader = new MultiFormatReader();
57     this.resultPointCallback = resultPointCallback;
58
59     // The prefs can't change while the thread is running, so pick them up once here.
60     if (mode == null || mode.length() == 0) {
61       SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
62       boolean decode1D = prefs.getBoolean(PreferencesActivity.KEY_DECODE_1D, true);
63       boolean decodeQR = prefs.getBoolean(PreferencesActivity.KEY_DECODE_QR, true);
64       if (decode1D && decodeQR) {
65         setDecodeAllMode();
66       } else if (decode1D) {
67         setDecode1DMode();
68       } else if (decodeQR) {
69         setDecodeQRMode();
70       }
71     } else {
72       if (mode.equals(Intents.Scan.PRODUCT_MODE)) {
73         setDecodeProductMode();
74       } else if (mode.equals(Intents.Scan.ONE_D_MODE)) {
75         setDecode1DMode();
76       } else if (mode.equals(Intents.Scan.QR_CODE_MODE)) {
77         setDecodeQRMode();
78       } else {
79         setDecodeAllMode();
80       }
81     }
82   }
83
84   Handler getHandler() {
85     return handler;
86   }
87
88   @Override
89   public void run() {
90     Looper.prepare();
91     handler = new Handler() {
92       @Override
93       public void handleMessage(Message message) {
94         switch (message.what) {
95           case R.id.decode:
96             decode((byte[]) message.obj, message.arg1, message.arg2);
97             break;
98           case R.id.quit:
99             Looper.myLooper().quit();
100             break;
101         }
102       }
103     };
104     Looper.loop();
105   }
106
107   private void setDecodeProductMode() {
108     doSetDecodeMode(BarcodeFormat.UPC_A,
109                     BarcodeFormat.UPC_E,
110                     BarcodeFormat.EAN_13,
111                     BarcodeFormat.EAN_8,
112                     BarcodeFormat.RSS14);
113   }
114
115   /**
116    * Select the 1D formats we want this client to decode by hand.
117    */
118   private void setDecode1DMode() {
119     doSetDecodeMode(BarcodeFormat.UPC_A,
120                     BarcodeFormat.UPC_E,
121                     BarcodeFormat.EAN_13,
122                     BarcodeFormat.EAN_8,
123                     BarcodeFormat.CODE_39,
124                     BarcodeFormat.CODE_128,
125                     BarcodeFormat.ITF,
126                     BarcodeFormat.RSS14);
127   }
128
129   private void setDecodeQRMode() {
130     doSetDecodeMode(BarcodeFormat.QR_CODE);
131   }
132
133   /**
134    * Instead of calling setHints(null), which would allow new formats to sneak in, we
135    * explicitly set which formats are available.
136    */
137   private void setDecodeAllMode() {
138     doSetDecodeMode(BarcodeFormat.UPC_A,
139                     BarcodeFormat.UPC_E,
140                     BarcodeFormat.EAN_13,
141                     BarcodeFormat.EAN_8,
142                     BarcodeFormat.CODE_39,
143                     BarcodeFormat.CODE_128,
144                     BarcodeFormat.ITF,
145                     BarcodeFormat.RSS14,
146                     BarcodeFormat.QR_CODE);
147   }
148
149   private void doSetDecodeMode(BarcodeFormat... formats) {
150     Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
151     Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>(formats.length);
152     for (BarcodeFormat format : formats) {
153       vector.addElement(format);
154     }
155     hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
156     hints.put(DecodeHintType.NEED_RESULT_POINT_CALLBACK, resultPointCallback);
157     multiFormatReader.setHints(hints);
158   }
159
160   /**
161    * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
162    * reuse the same reader objects from one decode to the next.
163    *
164    * @param data   The YUV preview frame.
165    * @param width  The width of the preview frame.
166    * @param height The height of the preview frame.
167    */
168   private void decode(byte[] data, int width, int height) {
169     long start = System.currentTimeMillis();
170     Result rawResult = null;
171     PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(data, width, height);
172     BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
173     try {
174       rawResult = multiFormatReader.decodeWithState(bitmap);
175     } catch (ReaderException re) {
176       // continue
177     } finally {
178       multiFormatReader.reset();
179     }
180
181     if (rawResult != null) {
182       long end = System.currentTimeMillis();
183       Log.v(TAG, "Found barcode (" + (end - start) + " ms):\n" + rawResult.toString());
184       Message message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, rawResult);
185       Bundle bundle = new Bundle();
186       bundle.putParcelable(BARCODE_BITMAP, source.renderCroppedGreyscaleBitmap());
187       message.setData(bundle);
188       message.sendToTarget();
189     } else {
190       Message message = Message.obtain(activity.getHandler(), R.id.decode_failed);
191       message.sendToTarget();
192     }
193   }
194 }