2 * Copyright (C) 2008 Google Inc.
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.graphics.Bitmap;
20 import android.os.Handler;
21 import android.os.Message;
22 import com.google.zxing.MonochromeBitmapSource;
23 import com.google.zxing.MultiFormatReader;
24 import com.google.zxing.ReaderException;
25 import com.google.zxing.Result;
28 * This thread does all the heavy lifting, both during preview and for the final capture and
29 * decoding. That leaves the main thread free to handle UI tasks.
31 * @author dswitkin@google.com (Daniel Switkin)
33 final class WorkerThread extends Thread {
35 private final CameraSurfaceView surfaceView;
36 private final CameraManager cameraManager;
37 private final Handler handler;
38 private final Object idleLock;
48 WorkerThread(CameraSurfaceView surfaceView, CameraManager cameraManager, Handler handler) {
49 this.surfaceView = surfaceView;
50 this.cameraManager = cameraManager;
51 this.handler = handler;
52 this.idleLock = new Object();
64 surfaceView.capturePreviewAndDraw();
66 case STILL_AND_DECODE:
67 Bitmap bitmap = cameraManager.captureStill();
70 MonochromeBitmapSource source = new RGBMonochromeBitmapSource(bitmap);
71 rawResult = new MultiFormatReader().decode(source);
72 } catch (ReaderException e) {
73 Message message = Message.obtain(handler, R.id.decoding_failed_message);
74 message.sendToTarget();
75 state = State.PREVIEW_LOOP;
78 Message message = Message.obtain(handler, R.id.decoding_succeeded_message, rawResult);
79 message.sendToTarget();
88 public void requestPreviewLoop() {
89 state = State.PREVIEW_LOOP;
93 public void requestStillAndDecode() {
94 state = State.STILL_AND_DECODE;
98 public void requestExitAndWait() {
103 } catch (InterruptedException e) {
107 private void idle() {
109 synchronized (idleLock) {
112 } catch (InterruptedException ie) {
117 private void wakeFromIdle() {
118 synchronized (idleLock) {
119 idleLock.notifyAll();