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 CameraSurfaceView surfaceView;
36 private CameraManager cameraManager;
37 private Handler handler;
48 WorkerThread(CameraSurfaceView surfaceView, CameraManager cameraManager, Handler handler) {
49 this.surfaceView = surfaceView;
50 this.cameraManager = cameraManager;
51 this.handler = handler;
62 } catch (InterruptedException e) {
66 surfaceView.capturePreviewAndDraw();
68 case STILL_AND_DECODE:
69 Bitmap bitmap = cameraManager.captureStill();
72 MonochromeBitmapSource source = new YUVMonochromeBitmapSource(bitmap);
73 rawResult = new MultiFormatReader().decode(source);
74 } catch (ReaderException e) {
75 Message message = Message.obtain(handler, R.id.decoding_failed_message);
76 message.sendToTarget();
77 state = State.PREVIEW_LOOP;
80 Message message = Message.obtain(handler, R.id.decoding_succeeded_message, rawResult);
81 message.sendToTarget();
90 public void requestPreviewLoop() {
91 state = State.PREVIEW_LOOP;
94 public void requestStillAndDecode() {
95 state = State.STILL_AND_DECODE;
98 public void requestExitAndWait() {
102 } catch (InterruptedException e) {