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 com.google.zxing.BarcodeFormat;
20 import com.google.zxing.BinaryBitmap;
21 import com.google.zxing.DecodeHintType;
22 import com.google.zxing.MultiFormatReader;
23 import com.google.zxing.ReaderException;
24 import com.google.zxing.Result;
25 import com.google.zxing.common.GlobalHistogramBinarizer;
27 import android.content.SharedPreferences;
28 import android.os.Bundle;
29 import android.os.Handler;
30 import android.os.Looper;
31 import android.os.Message;
32 import android.preference.PreferenceManager;
33 import android.util.Log;
35 import java.util.Hashtable;
36 import java.util.Vector;
39 * This thread does all the heavy lifting of decoding the images.
41 * @author dswitkin@google.com (Daniel Switkin)
43 final class DecodeThread extends Thread {
44 public static final String BARCODE_BITMAP = "barcode_bitmap";
45 private static final String TAG = "DecodeThread";
47 private Handler handler;
48 private final CaptureActivity activity;
49 private final MultiFormatReader multiFormatReader;
51 DecodeThread(CaptureActivity activity, String mode) {
52 this.activity = activity;
53 multiFormatReader = new MultiFormatReader();
55 // The prefs can't change while the thread is running, so pick them up once here.
56 if (mode == null || mode.length() == 0) {
57 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
58 boolean decode1D = prefs.getBoolean(PreferencesActivity.KEY_DECODE_1D, true);
59 boolean decodeQR = prefs.getBoolean(PreferencesActivity.KEY_DECODE_QR, true);
60 if (decode1D && decodeQR) {
62 } else if (decode1D) {
64 } else if (decodeQR) {
68 if (mode.equals(Intents.Scan.PRODUCT_MODE)) {
69 setDecodeProductMode();
70 } else if (mode.equals(Intents.Scan.ONE_D_MODE)) {
72 } else if (mode.equals(Intents.Scan.QR_CODE_MODE)) {
80 Handler getHandler() {
87 handler = new Handler() {
89 public void handleMessage(Message message) {
90 switch (message.what) {
92 decode((byte[]) message.obj, message.arg1, message.arg2);
95 Looper.myLooper().quit();
103 private void setDecodeProductMode() {
104 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
105 Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>(4);
106 vector.addElement(BarcodeFormat.UPC_A);
107 vector.addElement(BarcodeFormat.UPC_E);
108 vector.addElement(BarcodeFormat.EAN_13);
109 vector.addElement(BarcodeFormat.EAN_8);
110 hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
111 multiFormatReader.setHints(hints);
115 * Select the 1D formats we want this client to decode by hand.
117 private void setDecode1DMode() {
118 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
119 Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>(7);
120 vector.addElement(BarcodeFormat.UPC_A);
121 vector.addElement(BarcodeFormat.UPC_E);
122 vector.addElement(BarcodeFormat.EAN_13);
123 vector.addElement(BarcodeFormat.EAN_8);
124 vector.addElement(BarcodeFormat.CODE_39);
125 vector.addElement(BarcodeFormat.CODE_128);
126 vector.addElement(BarcodeFormat.ITF);
127 hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
128 multiFormatReader.setHints(hints);
131 private void setDecodeQRMode() {
132 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
133 Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>(1);
134 vector.addElement(BarcodeFormat.QR_CODE);
135 hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
136 multiFormatReader.setHints(hints);
140 * Instead of calling setHints(null), which would allow new formats to sneak in, we
141 * explicitly set which formats are available.
143 private void setDecodeAllMode() {
144 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
145 Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>(8);
146 vector.addElement(BarcodeFormat.UPC_A);
147 vector.addElement(BarcodeFormat.UPC_E);
148 vector.addElement(BarcodeFormat.EAN_13);
149 vector.addElement(BarcodeFormat.EAN_8);
150 vector.addElement(BarcodeFormat.CODE_39);
151 vector.addElement(BarcodeFormat.CODE_128);
152 vector.addElement(BarcodeFormat.ITF);
153 vector.addElement(BarcodeFormat.QR_CODE);
154 hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
155 multiFormatReader.setHints(hints);
159 * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
160 * reuse the same reader objects from one decode to the next.
162 * @param data The YUV preview frame.
163 * @param width The width of the preview frame.
164 * @param height The height of the preview frame.
166 private void decode(byte[] data, int width, int height) {
167 long start = System.currentTimeMillis();
168 Result rawResult = null;
169 BaseLuminanceSource source = CameraManager.get().buildLuminanceSource(data, width, height);
170 BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
172 rawResult = multiFormatReader.decodeWithState(bitmap);
173 } catch (ReaderException re) {
177 if (rawResult != null) {
178 long end = System.currentTimeMillis();
179 Log.v(TAG, "Found barcode (" + (end - start) + " ms):\n" + rawResult.toString());
180 Message message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, rawResult);
181 Bundle bundle = new Bundle();
182 bundle.putParcelable(BARCODE_BITMAP, source.renderCroppedGreyscaleBitmap());
183 message.setData(bundle);
184 message.sendToTarget();
186 Message message = Message.obtain(activity.getHandler(), R.id.decode_failed);
187 message.sendToTarget();