Added some log statement to help track down Acer 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         Log.v(TAG, "Got decode message");
53         decode((byte[]) message.obj, message.arg1, message.arg2);
54         break;
55       case R.id.quit:
56         Looper.myLooper().quit();
57         break;
58     }
59   }
60
61   /**
62    * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
63    * reuse the same reader objects from one decode to the next.
64    *
65    * @param data   The YUV preview frame.
66    * @param width  The width of the preview frame.
67    * @param height The height of the preview frame.
68    */
69   private void decode(byte[] data, int width, int height) {
70     long start = System.currentTimeMillis();
71     Result rawResult = null;
72     PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(data, width, height);
73     BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
74     try {
75       rawResult = multiFormatReader.decodeWithState(bitmap);
76     } catch (ReaderException re) {
77       // continue
78     } finally {
79       multiFormatReader.reset();
80     }
81
82     if (rawResult != null) {
83       long end = System.currentTimeMillis();
84       Log.v(TAG, "Found barcode (" + (end - start) + " ms):\n" + rawResult.toString());
85       Message message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, rawResult);
86       Bundle bundle = new Bundle();
87       bundle.putParcelable(DecodeThread.BARCODE_BITMAP, source.renderCroppedGreyscaleBitmap());
88       message.setData(bundle);
89       Log.v(TAG, "Sending decode succeeded message...");
90       message.sendToTarget();
91     } else {
92       Message message = Message.obtain(activity.getHandler(), R.id.decode_failed);
93       Log.v(TAG, "Sending decode failed message...");
94       message.sendToTarget();
95     }
96   }
97
98 }