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