dce7d14cb08eabe43a3ec8968f1e80c04f0fc0a8
[zxing.git] / android / src / com / google / zxing / client / android / DecodeThread.java
1 /*
2  * Copyright (C) 2008 ZXing authors
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.client.android;
18
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.common.HybridBinarizer;
27
28 import android.content.SharedPreferences;
29 import android.os.Bundle;
30 import android.os.Handler;
31 import android.os.Looper;
32 import android.os.Message;
33 import android.preference.PreferenceManager;
34 import android.util.Log;
35
36 import java.util.Hashtable;
37 import java.util.Vector;
38
39 /**
40  * This thread does all the heavy lifting of decoding the images.
41  *
42  * @author dswitkin@google.com (Daniel Switkin)
43  */
44 final class DecodeThread extends Thread {
45
46   private static final String TAG = DecodeThread.class.getSimpleName();
47
48   public static final String BARCODE_BITMAP = "barcode_bitmap";
49
50   private Handler handler;
51   private final CaptureActivity activity;
52   private final MultiFormatReader multiFormatReader;
53
54   DecodeThread(CaptureActivity activity,
55                Vector<BarcodeFormat> decodeFormats,
56                String characterSet,
57                ResultPointCallback resultPointCallback) {
58     this.activity = activity;
59     multiFormatReader = new MultiFormatReader();
60     Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
61
62     // The prefs can't change while the thread is running, so pick them up once here.
63     if (decodeFormats == null || decodeFormats.isEmpty()) {
64       SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
65       boolean decode1D = prefs.getBoolean(PreferencesActivity.KEY_DECODE_1D, true);
66       boolean decodeQR = prefs.getBoolean(PreferencesActivity.KEY_DECODE_QR, true);
67       if (decode1D && decodeQR) {
68         hints.put(DecodeHintType.POSSIBLE_FORMATS, CaptureActivity.ALL_FORMATS);
69       } else if (decode1D) {
70         hints.put(DecodeHintType.POSSIBLE_FORMATS, CaptureActivity.ONE_D_FORMATS);
71       } else if (decodeQR) {
72         hints.put(DecodeHintType.POSSIBLE_FORMATS, CaptureActivity.QR_CODE_FORMATS);
73       }
74     } else {
75       hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
76     }
77
78     if (characterSet != null) {
79       hints.put(DecodeHintType.CHARACTER_SET, characterSet);
80     }
81
82     hints.put(DecodeHintType.NEED_RESULT_POINT_CALLBACK, resultPointCallback);
83
84     multiFormatReader.setHints(hints);
85   }
86
87   Handler getHandler() {
88     return handler;
89   }
90
91   @Override
92   public void run() {
93     Looper.prepare();
94     handler = new Handler() {
95       @Override
96       public void handleMessage(Message message) {
97         switch (message.what) {
98           case R.id.decode:
99             decode((byte[]) message.obj, message.arg1, message.arg2);
100             break;
101           case R.id.quit:
102             Looper.myLooper().quit();
103             break;
104         }
105       }
106     };
107     Looper.loop();
108   }
109
110   /**
111    * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
112    * reuse the same reader objects from one decode to the next.
113    *
114    * @param data   The YUV preview frame.
115    * @param width  The width of the preview frame.
116    * @param height The height of the preview frame.
117    */
118   private void decode(byte[] data, int width, int height) {
119     long start = System.currentTimeMillis();
120     Result rawResult = null;
121     PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(data, width, height);
122     BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
123     try {
124       rawResult = multiFormatReader.decodeWithState(bitmap);
125     } catch (ReaderException re) {
126       // continue
127     } finally {
128       multiFormatReader.reset();
129     }
130
131     if (rawResult != null) {
132       long end = System.currentTimeMillis();
133       Log.v(TAG, "Found barcode (" + (end - start) + " ms):\n" + rawResult.toString());
134       Message message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, rawResult);
135       Bundle bundle = new Bundle();
136       bundle.putParcelable(BARCODE_BITMAP, source.renderCroppedGreyscaleBitmap());
137       message.setData(bundle);
138       message.sendToTarget();
139     } else {
140       Message message = Message.obtain(activity.getHandler(), R.id.decode_failed);
141       message.sendToTarget();
142     }
143   }
144 }