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.Date;
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";
42 public Handler mHandler;
43 private CaptureActivity mActivity;
44 private MultiFormatReader mMultiFormatReader;
46 DecodeThread(CaptureActivity activity, String mode) {
48 mMultiFormatReader = new MultiFormatReader();
50 // The prefs can't change while the thread is running, so pick them up once here.
51 if (mode == null || mode.length() == 0) {
52 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
53 boolean decode1D = prefs.getBoolean(PreferencesActivity.KEY_DECODE_1D, true);
54 boolean decodeQR = prefs.getBoolean(PreferencesActivity.KEY_DECODE_QR, true);
55 if (decode1D && decodeQR) {
57 } else if (decode1D) {
59 } else if (decodeQR) {
63 if (mode.equals(Intents.Scan.PRODUCT_MODE)) {
64 setDecodeProductMode();
65 } else if (mode.equals(Intents.Scan.ONE_D_MODE)) {
67 } else if (mode.equals(Intents.Scan.QR_CODE_MODE)) {
78 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);
104 // TODO: This is fragile in case we add new formats. It would be better to have a new enum
105 // value which represented all 1D formats.
106 private void setDecode1DMode() {
107 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
108 Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
109 vector.addElement(BarcodeFormat.UPC_A);
110 vector.addElement(BarcodeFormat.UPC_E);
111 vector.addElement(BarcodeFormat.EAN_13);
112 vector.addElement(BarcodeFormat.EAN_8);
113 vector.addElement(BarcodeFormat.CODE_39);
114 vector.addElement(BarcodeFormat.CODE_128);
115 hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
116 mMultiFormatReader.setHints(hints);
119 private void setDecodeQRMode() {
120 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
121 Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
122 vector.addElement(BarcodeFormat.QR_CODE);
123 hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
124 mMultiFormatReader.setHints(hints);
127 private void setDecodeAllMode() {
128 mMultiFormatReader.setHints(null);
132 * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
133 * reuse the same reader objects from one decode to the next.
135 * @param data The YUV preview frame.
136 * @param width The width of the preview frame.
137 * @param height The height of the preview frame.
139 private void decode(byte[] data, int width, int height) {
140 Date startDate = new Date();
142 Result rawResult = null;
143 YUVMonochromeBitmapSource source = new YUVMonochromeBitmapSource(data, width, height,
144 CameraManager.get().getFramingRect());
146 rawResult = mMultiFormatReader.decodeWithState(source);
148 } catch (ReaderException e) {
151 Date endDate = new Date();
154 Message message = Message.obtain(mActivity.mHandler, R.id.decode_succeeded, rawResult);
155 message.arg1 = (int) (endDate.getTime() - startDate.getTime());
156 Bundle bundle = new Bundle();
157 bundle.putParcelable(BARCODE_BITMAP, source.renderToBitmap());
158 message.setData(bundle);
159 message.sendToTarget();
161 Message message = Message.obtain(mActivity.mHandler, R.id.decode_failed);
162 message.arg1 = (int) (endDate.getTime() - startDate.getTime());
163 message.sendToTarget();