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.ResultPointCallback;
26 import com.google.zxing.client.android.camera.CameraManager;
27 import com.google.zxing.common.HybridBinarizer;
29 import android.content.SharedPreferences;
30 import android.os.Bundle;
31 import android.os.Handler;
32 import android.os.Looper;
33 import android.os.Message;
34 import android.preference.PreferenceManager;
35 import android.util.Log;
37 import java.util.Hashtable;
38 import java.util.Vector;
41 * This thread does all the heavy lifting of decoding the images.
43 * @author dswitkin@google.com (Daniel Switkin)
45 final class DecodeThread extends Thread {
47 private static final String TAG = DecodeThread.class.getSimpleName();
49 public static final String BARCODE_BITMAP = "barcode_bitmap";
51 private Handler handler;
52 private final CaptureActivity activity;
53 private final MultiFormatReader multiFormatReader;
55 DecodeThread(CaptureActivity activity,
56 Vector<BarcodeFormat> decodeFormats,
58 ResultPointCallback resultPointCallback) {
59 this.activity = activity;
60 multiFormatReader = new MultiFormatReader();
61 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
63 // The prefs can't change while the thread is running, so pick them up once here.
64 if (decodeFormats == null || decodeFormats.isEmpty()) {
65 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
66 boolean decode1D = prefs.getBoolean(PreferencesActivity.KEY_DECODE_1D, true);
67 boolean decodeQR = prefs.getBoolean(PreferencesActivity.KEY_DECODE_QR, true);
68 if (decode1D && decodeQR) {
69 hints.put(DecodeHintType.POSSIBLE_FORMATS, CaptureActivity.ALL_FORMATS);
70 } else if (decode1D) {
71 hints.put(DecodeHintType.POSSIBLE_FORMATS, CaptureActivity.ONE_D_FORMATS);
72 } else if (decodeQR) {
73 hints.put(DecodeHintType.POSSIBLE_FORMATS, CaptureActivity.QR_CODE_FORMATS);
76 hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
79 if (characterSet != null) {
80 hints.put(DecodeHintType.CHARACTER_SET, characterSet);
83 hints.put(DecodeHintType.NEED_RESULT_POINT_CALLBACK, resultPointCallback);
85 multiFormatReader.setHints(hints);
88 Handler getHandler() {
95 handler = new Handler() {
97 public void handleMessage(Message message) {
98 switch (message.what) {
100 decode((byte[]) message.obj, message.arg1, message.arg2);
103 Looper.myLooper().quit();
112 * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
113 * reuse the same reader objects from one decode to the next.
115 * @param data The YUV preview frame.
116 * @param width The width of the preview frame.
117 * @param height The height of the preview frame.
119 private void decode(byte[] data, int width, int height) {
120 long start = System.currentTimeMillis();
121 Result rawResult = null;
122 PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(data, width, height);
123 BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
125 rawResult = multiFormatReader.decodeWithState(bitmap);
126 } catch (ReaderException re) {
129 multiFormatReader.reset();
132 if (rawResult != null) {
133 long end = System.currentTimeMillis();
134 Log.v(TAG, "Found barcode (" + (end - start) + " ms):\n" + rawResult.toString());
135 Message message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, rawResult);
136 Bundle bundle = new Bundle();
137 bundle.putParcelable(BARCODE_BITMAP, source.renderCroppedGreyscaleBitmap());
138 message.setData(bundle);
139 message.sendToTarget();
141 Message message = Message.obtain(activity.getHandler(), R.id.decode_failed);
142 message.sendToTarget();