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 android.util.Log;
26 import com.google.zxing.BarcodeFormat;
27 import com.google.zxing.DecodeHintType;
28 import com.google.zxing.MultiFormatReader;
29 import com.google.zxing.ReaderException;
30 import com.google.zxing.Result;
32 import java.util.Hashtable;
33 import java.util.Vector;
36 * This thread does all the heavy lifting of decoding the images.
38 final class DecodeThread extends Thread {
40 public static final String BARCODE_BITMAP = "barcode_bitmap";
41 private static final String TAG = "DecodeThread";
43 public Handler mHandler;
44 private final CaptureActivity mActivity;
45 private final MultiFormatReader mMultiFormatReader;
47 DecodeThread(CaptureActivity activity, String mode) {
49 mMultiFormatReader = new MultiFormatReader();
51 // The prefs can't change while the thread is running, so pick them up once here.
52 if (mode == null || mode.length() == 0) {
53 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
54 boolean decode1D = prefs.getBoolean(PreferencesActivity.KEY_DECODE_1D, true);
55 boolean decodeQR = prefs.getBoolean(PreferencesActivity.KEY_DECODE_QR, true);
56 if (decode1D && decodeQR) {
58 } else if (decode1D) {
60 } else if (decodeQR) {
64 if (mode.equals(Intents.Scan.PRODUCT_MODE)) {
65 setDecodeProductMode();
66 } else if (mode.equals(Intents.Scan.ONE_D_MODE)) {
68 } else if (mode.equals(Intents.Scan.QR_CODE_MODE)) {
79 mHandler = new Handler() {
81 public void handleMessage(Message message) {
82 switch (message.what) {
84 decode((byte[]) message.obj, message.arg1, message.arg2);
87 Looper.myLooper().quit();
95 private void setDecodeProductMode() {
96 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
97 Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
98 vector.addElement(BarcodeFormat.UPC_A);
99 vector.addElement(BarcodeFormat.UPC_E);
100 vector.addElement(BarcodeFormat.EAN_13);
101 vector.addElement(BarcodeFormat.EAN_8);
102 hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
103 mMultiFormatReader.setHints(hints);
107 * Select the 1D formats we want this client to decode by hand.
109 private void setDecode1DMode() {
110 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
111 Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
112 vector.addElement(BarcodeFormat.UPC_A);
113 vector.addElement(BarcodeFormat.UPC_E);
114 vector.addElement(BarcodeFormat.EAN_13);
115 vector.addElement(BarcodeFormat.EAN_8);
116 vector.addElement(BarcodeFormat.CODE_39);
117 vector.addElement(BarcodeFormat.CODE_128);
118 vector.addElement(BarcodeFormat.ITF);
119 hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
120 mMultiFormatReader.setHints(hints);
123 private void setDecodeQRMode() {
124 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
125 Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
126 vector.addElement(BarcodeFormat.QR_CODE);
127 hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
128 mMultiFormatReader.setHints(hints);
132 * Instead of calling setHints(null), which would allow new formats to sneak in, we
133 * explicitly set which formats are available.
135 private void setDecodeAllMode() {
136 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
137 Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
138 vector.addElement(BarcodeFormat.UPC_A);
139 vector.addElement(BarcodeFormat.UPC_E);
140 vector.addElement(BarcodeFormat.EAN_13);
141 vector.addElement(BarcodeFormat.EAN_8);
142 vector.addElement(BarcodeFormat.CODE_39);
143 vector.addElement(BarcodeFormat.CODE_128);
144 vector.addElement(BarcodeFormat.ITF);
145 vector.addElement(BarcodeFormat.QR_CODE);
146 hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
147 mMultiFormatReader.setHints(hints);
151 * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
152 * reuse the same reader objects from one decode to the next.
154 * @param data The YUV preview frame.
155 * @param width The width of the preview frame.
156 * @param height The height of the preview frame.
158 private void decode(byte[] data, int width, int height) {
159 long start = System.currentTimeMillis();
161 Result rawResult = null;
162 YUVMonochromeBitmapSource source = new YUVMonochromeBitmapSource(data, width, height,
163 CameraManager.get().getFramingRect());
165 rawResult = mMultiFormatReader.decodeWithState(source);
167 } catch (ReaderException e) {
170 long end = System.currentTimeMillis();
173 Log.v(TAG, "Found barcode (" + (end - start) + " ms):\n" + rawResult.toString());
174 Message message = Message.obtain(mActivity.mHandler, R.id.decode_succeeded, rawResult);
175 Bundle bundle = new Bundle();
176 bundle.putParcelable(BARCODE_BITMAP, source.renderToBitmap());
177 message.setData(bundle);
178 message.sendToTarget();
180 Message message = Message.obtain(mActivity.mHandler, R.id.decode_failed);
181 message.sendToTarget();