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