2 * Copyright (C) 2008 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.android;
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 com.google.zxing.BarcodeFormat;
26 import com.google.zxing.DecodeHintType;
27 import com.google.zxing.MultiFormatReader;
28 import com.google.zxing.ReaderException;
29 import com.google.zxing.Result;
31 import java.util.Hashtable;
32 import java.util.Vector;
35 * This thread does all the heavy lifting of decoding the images.
37 final class DecodeThread extends Thread {
39 public static final String BARCODE_BITMAP = "barcode_bitmap";
41 public Handler mHandler;
42 private final CaptureActivity mActivity;
43 private final MultiFormatReader mMultiFormatReader;
45 DecodeThread(CaptureActivity activity, String mode) {
47 mMultiFormatReader = new MultiFormatReader();
49 // The prefs can't change while the thread is running, so pick them up once here.
50 if (mode == null || mode.length() == 0) {
51 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
52 boolean decode1D = prefs.getBoolean(PreferencesActivity.KEY_DECODE_1D, true);
53 boolean decodeQR = prefs.getBoolean(PreferencesActivity.KEY_DECODE_QR, true);
54 if (decode1D && decodeQR) {
56 } else if (decode1D) {
58 } else if (decodeQR) {
62 if (mode.equals(Intents.Scan.PRODUCT_MODE)) {
63 setDecodeProductMode();
64 } else if (mode.equals(Intents.Scan.ONE_D_MODE)) {
66 } else if (mode.equals(Intents.Scan.QR_CODE_MODE)) {
77 mHandler = new Handler() {
78 public void handleMessage(Message message) {
79 switch (message.what) {
81 decode((byte[]) message.obj, message.arg1, message.arg2);
84 Looper.myLooper().quit();
92 private void setDecodeProductMode() {
93 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
94 Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
95 vector.addElement(BarcodeFormat.UPC_A);
96 vector.addElement(BarcodeFormat.UPC_E);
97 vector.addElement(BarcodeFormat.EAN_13);
98 vector.addElement(BarcodeFormat.EAN_8);
99 hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
100 mMultiFormatReader.setHints(hints);
103 // TODO: This is fragile in case we add new formats. It would be better to have a new enum
104 // value which represented all 1D formats.
105 private void setDecode1DMode() {
106 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
107 Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
108 vector.addElement(BarcodeFormat.UPC_A);
109 vector.addElement(BarcodeFormat.UPC_E);
110 vector.addElement(BarcodeFormat.EAN_13);
111 vector.addElement(BarcodeFormat.EAN_8);
112 vector.addElement(BarcodeFormat.CODE_39);
113 vector.addElement(BarcodeFormat.CODE_128);
114 hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
115 mMultiFormatReader.setHints(hints);
118 private void setDecodeQRMode() {
119 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
120 Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
121 vector.addElement(BarcodeFormat.QR_CODE);
122 hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
123 mMultiFormatReader.setHints(hints);
127 * Instead of calling setHints(null), which would allow new formats like ITF to sneak in, we
128 * explicitly set which formats are available.
130 private void setDecodeAllMode() {
131 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
132 Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
133 vector.addElement(BarcodeFormat.UPC_A);
134 vector.addElement(BarcodeFormat.UPC_E);
135 vector.addElement(BarcodeFormat.EAN_13);
136 vector.addElement(BarcodeFormat.EAN_8);
137 vector.addElement(BarcodeFormat.CODE_39);
138 vector.addElement(BarcodeFormat.CODE_128);
139 vector.addElement(BarcodeFormat.QR_CODE);
140 hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
141 mMultiFormatReader.setHints(hints);
145 * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
146 * reuse the same reader objects from one decode to the next.
148 * @param data The YUV preview frame.
149 * @param width The width of the preview frame.
150 * @param height The height of the preview frame.
152 private void decode(byte[] data, int width, int height) {
153 long start = System.currentTimeMillis();
155 Result rawResult = null;
156 YUVMonochromeBitmapSource source = new YUVMonochromeBitmapSource(data, width, height,
157 CameraManager.get().getFramingRect());
159 rawResult = mMultiFormatReader.decodeWithState(source);
161 } catch (ReaderException e) {
164 long end = System.currentTimeMillis();
167 Message message = Message.obtain(mActivity.mHandler, R.id.decode_succeeded, rawResult);
168 message.arg1 = (int) (end - start);
169 Bundle bundle = new Bundle();
170 bundle.putParcelable(BARCODE_BITMAP, source.renderToBitmap());
171 message.setData(bundle);
172 message.sendToTarget();
174 Message message = Message.obtain(mActivity.mHandler, R.id.decode_failed);
175 message.arg1 = (int) (end - start);
176 message.sendToTarget();