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.DecodeHintType;
21 import com.google.zxing.ResultPointCallback;
23 import android.content.SharedPreferences;
24 import android.os.Handler;
25 import android.os.Looper;
26 import android.preference.PreferenceManager;
28 import java.util.Hashtable;
29 import java.util.Vector;
30 import java.util.concurrent.CountDownLatch;
33 * This thread does all the heavy lifting of decoding the images.
35 * @author dswitkin@google.com (Daniel Switkin)
37 final class DecodeThread extends Thread {
39 public static final String BARCODE_BITMAP = "barcode_bitmap";
41 private final CaptureActivity activity;
42 private final Hashtable<DecodeHintType, Object> hints;
43 private Handler handler;
44 private final CountDownLatch handlerInitLatch;
46 DecodeThread(CaptureActivity activity,
47 Vector<BarcodeFormat> decodeFormats,
49 ResultPointCallback resultPointCallback) {
51 this.activity = activity;
52 handlerInitLatch = new CountDownLatch(1);
54 hints = new Hashtable<DecodeHintType, Object>(3);
56 // The prefs can't change while the thread is running, so pick them up once here.
57 if (decodeFormats == null || decodeFormats.isEmpty()) {
58 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
59 decodeFormats = new Vector<BarcodeFormat>();
60 if (prefs.getBoolean(PreferencesActivity.KEY_DECODE_1D, true)) {
61 decodeFormats.addAll(DecodeFormatManager.ONE_D_FORMATS);
63 if (prefs.getBoolean(PreferencesActivity.KEY_DECODE_QR, true)) {
64 decodeFormats.addAll(DecodeFormatManager.QR_CODE_FORMATS);
66 if (prefs.getBoolean(PreferencesActivity.KEY_DECODE_DATA_MATRIX, true)) {
67 decodeFormats.addAll(DecodeFormatManager.DATA_MATRIX_FORMATS);
70 hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
72 if (characterSet != null) {
73 hints.put(DecodeHintType.CHARACTER_SET, characterSet);
76 hints.put(DecodeHintType.NEED_RESULT_POINT_CALLBACK, resultPointCallback);
79 Handler getHandler() {
81 handlerInitLatch.await();
82 } catch (InterruptedException ie) {
91 handler = new DecodeHandler(activity, hints);
92 handlerInitLatch.countDown();