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 public 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)) {
83 handler = new Handler() {
85 public void handleMessage(Message message) {
86 switch (message.what) {
88 decode((byte[]) message.obj, message.arg1, message.arg2);
91 Looper.myLooper().quit();
99 private void setDecodeProductMode() {
100 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
101 Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>(4);
102 vector.addElement(BarcodeFormat.UPC_A);
103 vector.addElement(BarcodeFormat.UPC_E);
104 vector.addElement(BarcodeFormat.EAN_13);
105 vector.addElement(BarcodeFormat.EAN_8);
106 hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
107 multiFormatReader.setHints(hints);
111 * Select the 1D formats we want this client to decode by hand.
113 private void setDecode1DMode() {
114 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
115 Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>(7);
116 vector.addElement(BarcodeFormat.UPC_A);
117 vector.addElement(BarcodeFormat.UPC_E);
118 vector.addElement(BarcodeFormat.EAN_13);
119 vector.addElement(BarcodeFormat.EAN_8);
120 vector.addElement(BarcodeFormat.CODE_39);
121 vector.addElement(BarcodeFormat.CODE_128);
122 vector.addElement(BarcodeFormat.ITF);
123 hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
124 multiFormatReader.setHints(hints);
127 private void setDecodeQRMode() {
128 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
129 Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>(1);
130 vector.addElement(BarcodeFormat.QR_CODE);
131 hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
132 multiFormatReader.setHints(hints);
136 * Instead of calling setHints(null), which would allow new formats to sneak in, we
137 * explicitly set which formats are available.
139 private void setDecodeAllMode() {
140 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
141 Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>(8);
142 vector.addElement(BarcodeFormat.UPC_A);
143 vector.addElement(BarcodeFormat.UPC_E);
144 vector.addElement(BarcodeFormat.EAN_13);
145 vector.addElement(BarcodeFormat.EAN_8);
146 vector.addElement(BarcodeFormat.CODE_39);
147 vector.addElement(BarcodeFormat.CODE_128);
148 vector.addElement(BarcodeFormat.ITF);
149 vector.addElement(BarcodeFormat.QR_CODE);
150 hints.put(DecodeHintType.POSSIBLE_FORMATS, vector);
151 multiFormatReader.setHints(hints);
155 * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
156 * reuse the same reader objects from one decode to the next.
158 * @param data The YUV preview frame.
159 * @param width The width of the preview frame.
160 * @param height The height of the preview frame.
162 private void decode(byte[] data, int width, int height) {
163 long start = System.currentTimeMillis();
165 Result rawResult = null;
166 BaseLuminanceSource source = CameraManager.get().buildLuminanceSource(data, width, height);
167 BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
169 rawResult = multiFormatReader.decodeWithState(bitmap);
171 } catch (ReaderException e) {
174 long end = System.currentTimeMillis();
177 Log.v(TAG, "Found barcode (" + (end - start) + " ms):\n" + rawResult.toString());
178 Message message = Message.obtain(activity.handler, R.id.decode_succeeded, rawResult);
179 Bundle bundle = new Bundle();
180 bundle.putParcelable(BARCODE_BITMAP, source.renderCroppedGreyscaleBitmap());
181 message.setData(bundle);
182 message.sendToTarget();
184 Message message = Message.obtain(activity.handler, R.id.decode_failed);
185 message.sendToTarget();