2 * Copyright (C) 2010 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.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;
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;
33 import java.util.Hashtable;
35 final class DecodeHandler extends Handler {
37 private static final String TAG = DecodeHandler.class.getSimpleName();
39 private final CaptureActivity activity;
40 private final MultiFormatReader multiFormatReader;
42 DecodeHandler(CaptureActivity activity, Hashtable<DecodeHintType, Object> hints) {
43 multiFormatReader = new MultiFormatReader();
44 multiFormatReader.setHints(hints);
45 this.activity = activity;
49 public void handleMessage(Message message) {
50 switch (message.what) {
52 //Log.d(TAG, "Got decode message");
53 decode((byte[]) message.obj, message.arg1, message.arg2);
56 Looper.myLooper().quit();
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.
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.
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));
75 rawResult = multiFormatReader.decodeWithState(bitmap);
76 } catch (ReaderException re) {
79 multiFormatReader.reset();
82 if (rawResult != null) {
83 long end = System.currentTimeMillis();
84 Log.d(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.d(TAG, "Sending decode succeeded message...");
90 message.sendToTarget();
92 Message message = Message.obtain(activity.getHandler(), R.id.decode_failed);
93 message.sendToTarget();