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() {
79 public void handleMessage(Message message) {
80 switch (message.what) {
82 decode((byte[]) message.obj, message.arg1, message.arg2);
85 Looper.myLooper().quit();
93 private void setDecodeProductMode() {
94 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
95 Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
96 vector.addElement(BarcodeFormat.UPC_A);
97 vector.addElement(BarcodeFormat.UPC_E);
98 vector.addElement(BarcodeFormat.EAN_13);
99 vector.addElement(BarcodeFormat.EAN_8);
100 hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
101 mMultiFormatReader.setHints(hints);
105 * Select the 1D formats we want this client to decode by hand.
107 private void setDecode1DMode() {
108 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
109 Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
110 vector.addElement(BarcodeFormat.UPC_A);
111 vector.addElement(BarcodeFormat.UPC_E);
112 vector.addElement(BarcodeFormat.EAN_13);
113 vector.addElement(BarcodeFormat.EAN_8);
114 vector.addElement(BarcodeFormat.CODE_39);
115 vector.addElement(BarcodeFormat.CODE_128);
116 vector.addElement(BarcodeFormat.ITF);
117 hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
118 mMultiFormatReader.setHints(hints);
121 private void setDecodeQRMode() {
122 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
123 Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
124 vector.addElement(BarcodeFormat.QR_CODE);
125 hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
126 mMultiFormatReader.setHints(hints);
130 * Instead of calling setHints(null), which would allow new formats to sneak in, we
131 * explicitly set which formats are available.
133 private void setDecodeAllMode() {
134 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
135 Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
136 vector.addElement(BarcodeFormat.UPC_A);
137 vector.addElement(BarcodeFormat.UPC_E);
138 vector.addElement(BarcodeFormat.EAN_13);
139 vector.addElement(BarcodeFormat.EAN_8);
140 vector.addElement(BarcodeFormat.CODE_39);
141 vector.addElement(BarcodeFormat.CODE_128);
142 vector.addElement(BarcodeFormat.ITF);
143 vector.addElement(BarcodeFormat.QR_CODE);
144 hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
145 mMultiFormatReader.setHints(hints);
149 * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
150 * reuse the same reader objects from one decode to the next.
152 * @param data The YUV preview frame.
153 * @param width The width of the preview frame.
154 * @param height The height of the preview frame.
156 private void decode(byte[] data, int width, int height) {
157 long start = System.currentTimeMillis();
159 Result rawResult = null;
160 YUVMonochromeBitmapSource source = new YUVMonochromeBitmapSource(data, width, height,
161 CameraManager.get().getFramingRect());
163 rawResult = mMultiFormatReader.decodeWithState(source);
165 } catch (ReaderException e) {
168 long end = System.currentTimeMillis();
171 Message message = Message.obtain(mActivity.mHandler, R.id.decode_succeeded, rawResult);
172 message.arg1 = (int) (end - start);
173 Bundle bundle = new Bundle();
174 bundle.putParcelable(BARCODE_BITMAP, source.renderToBitmap());
175 message.setData(bundle);
176 message.sendToTarget();
178 Message message = Message.obtain(mActivity.mHandler, R.id.decode_failed);
179 message.arg1 = (int) (end - start);
180 message.sendToTarget();