2 * Copyright (C) 2008 Google Inc.
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.android.barcodes;
19 import android.content.SharedPreferences;
20 import android.os.Handler;
21 import android.os.Looper;
22 import android.os.Message;
23 import android.preference.PreferenceManager;
24 import com.google.zxing.BarcodeFormat;
25 import com.google.zxing.DecodeHintType;
26 import com.google.zxing.MonochromeBitmapSource;
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 Handler mHandler;
41 private BarcodesCaptureActivity mActivity;
42 private MultiFormatReader mMultiFormatReader;
44 DecodeThread(BarcodesCaptureActivity activity, String mode) {
46 mMultiFormatReader = new MultiFormatReader();
48 // The prefs can't change while the thread is running, so pick them up once here.
49 if (mode == null || mode.length() == 0) {
50 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
51 boolean decode1D = prefs.getBoolean(BarcodesPreferenceActivity.KEY_DECODE_1D, true);
52 boolean decodeQR = prefs.getBoolean(BarcodesPreferenceActivity.KEY_DECODE_QR, true);
53 if (decode1D && decodeQR) {
55 } else if (decode1D) {
57 } else if (decodeQR) {
61 if (mode.equals(Intents.Scan.PRODUCT_MODE)) {
62 setDecodeProductMode();
63 } else if (mode.equals(Intents.Scan.ONE_D_MODE)) {
65 } else if (mode.equals(Intents.Scan.QR_CODE_MODE)) {
76 mHandler = new Handler() {
77 public void handleMessage(Message message) {
78 switch (message.what) {
80 decode((byte[]) message.obj, message.arg1, message.arg2);
83 Looper.myLooper().quit();
91 private void setDecodeProductMode() {
92 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
93 Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
94 vector.addElement(BarcodeFormat.UPC_A);
95 vector.addElement(BarcodeFormat.UPC_E);
96 vector.addElement(BarcodeFormat.EAN_13);
97 vector.addElement(BarcodeFormat.EAN_8);
98 hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
99 mMultiFormatReader.setHints(hints);
102 // TODO: This is fragile in case we add new formats. It would be better to have a new enum
103 // value which represented all 1D formats.
104 private void setDecode1DMode() {
105 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
106 Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
107 vector.addElement(BarcodeFormat.UPC_A);
108 vector.addElement(BarcodeFormat.UPC_E);
109 vector.addElement(BarcodeFormat.EAN_13);
110 vector.addElement(BarcodeFormat.EAN_8);
111 vector.addElement(BarcodeFormat.CODE_39);
112 vector.addElement(BarcodeFormat.CODE_128);
113 hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
114 mMultiFormatReader.setHints(hints);
117 private void setDecodeQRMode() {
118 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
119 Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>();
120 vector.addElement(BarcodeFormat.QR_CODE);
121 hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
122 mMultiFormatReader.setHints(hints);
125 private void setDecodeAllMode() {
126 mMultiFormatReader.setHints(null);
130 * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
131 * reuse the same reader objects from one decode to the next.
133 * @param data The YUV preview frame.
134 * @param width The width of the preview frame.
135 * @param height The height of the preview frame.
137 private void decode(byte[] data, int width, int height) {
138 Date startDate = new Date();
140 Result rawResult = null;
142 MonochromeBitmapSource source = new YUVMonochromeBitmapSource(data, width, height,
143 CameraManager.get().getFramingRect());
144 rawResult = mMultiFormatReader.decodeWithState(source);
146 } catch (ReaderException e) {
149 Date endDate = new Date();
152 Message message = Message.obtain(mActivity.mHandler, R.id.decode_succeeded, rawResult);
153 message.arg1 = (int) (endDate.getTime() - startDate.getTime());
154 message.sendToTarget();
156 Message message = Message.obtain(mActivity.mHandler, R.id.decode_failed);
157 message.arg1 = (int) (endDate.getTime() - startDate.getTime());
158 message.sendToTarget();