Issue 376: re-set camera params after first auto-focus callback to make it work on...
[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.client.android.camera.CameraManager;
27 import com.google.zxing.common.HybridBinarizer;
28
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;
36
37 import java.util.Hashtable;
38 import java.util.Vector;
39
40 /**
41  * This thread does all the heavy lifting of decoding the images.
42  *
43  * @author dswitkin@google.com (Daniel Switkin)
44  */
45 final class DecodeThread extends Thread {
46
47   private static final String TAG = DecodeThread.class.getSimpleName();
48
49   public static final String BARCODE_BITMAP = "barcode_bitmap";
50
51   private Handler handler;
52   private final CaptureActivity activity;
53   private final MultiFormatReader multiFormatReader;
54
55   DecodeThread(CaptureActivity activity,
56                Vector<BarcodeFormat> decodeFormats,
57                String characterSet,
58                ResultPointCallback resultPointCallback) {
59     this.activity = activity;
60     multiFormatReader = new MultiFormatReader();
61     Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
62
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);
74       }
75     } else {
76       hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
77     }
78
79     if (characterSet != null) {
80       hints.put(DecodeHintType.CHARACTER_SET, characterSet);
81     }
82
83     hints.put(DecodeHintType.NEED_RESULT_POINT_CALLBACK, resultPointCallback);
84
85     multiFormatReader.setHints(hints);
86   }
87
88   Handler getHandler() {
89     return handler;
90   }
91
92   @Override
93   public void run() {
94     Looper.prepare();
95     handler = new Handler() {
96       @Override
97       public void handleMessage(Message message) {
98         switch (message.what) {
99           case R.id.decode:
100             decode((byte[]) message.obj, message.arg1, message.arg2);
101             break;
102           case R.id.quit:
103             Looper.myLooper().quit();
104             break;
105         }
106       }
107     };
108     Looper.loop();
109   }
110
111   /**
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.
114    *
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.
118    */
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));
124     try {
125       rawResult = multiFormatReader.decodeWithState(bitmap);
126     } catch (ReaderException re) {
127       // continue
128     } finally {
129       multiFormatReader.reset();
130     }
131
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();
140     } else {
141       Message message = Message.obtain(activity.getHandler(), R.id.decode_failed);
142       message.sendToTarget();
143     }
144   }
145
146 }