2 * Copyright (C) 2008 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 android.app.Activity;
20 import android.content.Intent;
21 import android.graphics.Bitmap;
22 import android.os.Bundle;
23 import android.os.Handler;
24 import android.os.Message;
25 import com.google.zxing.Result;
28 * This class handles all the messaging which comprises the state machine for capture.
30 public final class CaptureActivityHandler extends Handler {
32 private final CaptureActivity mActivity;
33 private final DecodeThread mDecodeThread;
42 CaptureActivityHandler(CaptureActivity activity, String decodeMode,
43 boolean beginScanning) {
45 mDecodeThread = new DecodeThread(activity, decodeMode);
46 mDecodeThread.start();
47 mState = State.SUCCESS;
49 // Start ourselves capturing previews and decoding.
50 CameraManager.get().startPreview();
52 restartPreviewAndDecode();
56 public void handleMessage(Message message) {
57 switch (message.what) {
59 // When one auto focus pass finishes, start another. This is the closest thing to
60 // continuous AF. It does seem to hunt a bit, but I'm not sure what else to do.
61 if (mState == State.PREVIEW) {
62 CameraManager.get().requestAutoFocus(this, R.id.auto_focus);
65 case R.id.restart_preview:
66 restartPreviewAndDecode();
68 case R.id.decode_succeeded:
69 mState = State.SUCCESS;
70 Bundle bundle = message.getData();
71 Bitmap barcode = bundle.getParcelable(DecodeThread.BARCODE_BITMAP);
72 int duration = message.arg1;
73 mActivity.handleDecode((Result) message.obj, barcode, duration);
75 case R.id.decode_failed:
76 // We're decoding as fast as possible, so when one decode fails, start another.
77 mState = State.PREVIEW;
78 CameraManager.get().requestPreviewFrame(mDecodeThread.mHandler, R.id.decode);
80 case R.id.return_scan_result:
81 mActivity.setResult(Activity.RESULT_OK, (Intent) message.obj);
87 public void quitSynchronously() {
89 CameraManager.get().stopPreview();
90 Message quit = Message.obtain(mDecodeThread.mHandler, R.id.quit);
94 } catch (InterruptedException e) {
97 // Be absolutely sure we don't send any queued up messages
98 removeMessages(R.id.decode_succeeded);
99 removeMessages(R.id.decode_failed);
102 private void restartPreviewAndDecode() {
103 if (mState == State.SUCCESS) {
104 mState = State.PREVIEW;
105 CameraManager.get().requestPreviewFrame(mDecodeThread.mHandler, R.id.decode);
106 CameraManager.get().requestAutoFocus(this, R.id.auto_focus);
107 mActivity.drawViewfinder();