Apparent fix for issue 383 / Moment + Android 2.1 issue
[zxing.git] / android / src / com / google / zxing / client / android / DecodeHandler.java
1 /*
2  * Copyright (C) 2010 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.BinaryBitmap;
20 import com.google.zxing.DecodeHintType;
21 import com.google.zxing.MultiFormatReader;
22 import com.google.zxing.ReaderException;
23 import com.google.zxing.Result;
24 import com.google.zxing.client.android.camera.CameraManager;
25 import com.google.zxing.common.HybridBinarizer;
26
27 import android.os.Bundle;
28 import android.os.Handler;
29 import android.os.Looper;
30 import android.os.Message;
31 import android.util.Log;
32
33 import java.util.Hashtable;
34
35 final class DecodeHandler extends Handler {
36
37   private static final String TAG = DecodeHandler.class.getSimpleName();
38
39   private final CaptureActivity activity;
40   private final MultiFormatReader multiFormatReader;
41
42   DecodeHandler(CaptureActivity activity, Hashtable<DecodeHintType, Object> hints) {
43     multiFormatReader = new MultiFormatReader();
44     multiFormatReader.setHints(hints);
45     this.activity = activity;
46   }
47
48   @Override
49   public void handleMessage(Message message) {
50     switch (message.what) {
51       case R.id.decode:
52         decode((byte[]) message.obj, message.arg1, message.arg2);
53         break;
54       case R.id.quit:
55         Looper.myLooper().quit();
56         break;
57     }
58   }
59
60   /**
61    * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
62    * reuse the same reader objects from one decode to the next.
63    *
64    * @param data   The YUV preview frame.
65    * @param width  The width of the preview frame.
66    * @param height The height of the preview frame.
67    */
68   private void decode(byte[] data, int width, int height) {
69     long start = System.currentTimeMillis();
70     Result rawResult = null;
71     PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(data, width, height);
72     BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
73     try {
74       rawResult = multiFormatReader.decodeWithState(bitmap);
75     } catch (ReaderException re) {
76       // continue
77     } finally {
78       multiFormatReader.reset();
79     }
80
81     if (rawResult != null) {
82       long end = System.currentTimeMillis();
83       Log.v(TAG, "Found barcode (" + (end - start) + " ms):\n" + rawResult.toString());
84       Message message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, rawResult);
85       Bundle bundle = new Bundle();
86       bundle.putParcelable(DecodeThread.BARCODE_BITMAP, source.renderCroppedGreyscaleBitmap());
87       message.setData(bundle);
88       message.sendToTarget();
89     } else {
90       Message message = Message.obtain(activity.getHandler(), R.id.decode_failed);
91       message.sendToTarget();
92     }
93   }
94
95 }