Biiig standardization of whitespace. 2 space indents now, no tabs.
[zxing.git] / android / src / com / google / zxing / client / android / WorkerThread.java
1 /*
2  * Copyright (C) 2008 Google Inc.
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 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;
26
27 /**
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.
30  *
31  * @author dswitkin@google.com (Daniel Switkin)
32  */
33 final class WorkerThread extends Thread {
34
35   private CameraSurfaceView surfaceView;
36   private CameraManager cameraManager;
37   private Handler handler;
38
39   private enum State {
40     IDLE,
41     PREVIEW_LOOP,
42     STILL_AND_DECODE,
43     DONE
44   }
45
46   private State state;
47
48   WorkerThread(CameraSurfaceView surfaceView, CameraManager cameraManager, Handler handler) {
49     this.surfaceView = surfaceView;
50     this.cameraManager = cameraManager;
51     this.handler = handler;
52     state = State.IDLE;
53   }
54
55   @Override
56   public void run() {
57     while (true) {
58       switch (state) {
59         case IDLE:
60           try {
61             sleep(50);
62           } catch (InterruptedException e) {
63           }
64           break;
65         case PREVIEW_LOOP:
66           surfaceView.capturePreviewAndDraw();
67           break;
68         case STILL_AND_DECODE:
69           Bitmap bitmap = cameraManager.captureStill();
70           Result rawResult;
71           try {
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;
78             break;
79           }
80           Message message = Message.obtain(handler, R.id.decoding_succeeded_message, rawResult);
81           message.sendToTarget();
82           state = State.IDLE;
83           break;
84         case DONE:
85           return;
86       }
87     }
88   }
89
90   public void requestPreviewLoop() {
91     state = State.PREVIEW_LOOP;
92   }
93
94   public void requestStillAndDecode() {
95     state = State.STILL_AND_DECODE;
96   }
97
98   public void requestExitAndWait() {
99     state = State.DONE;
100     try {
101       join();
102     } catch (InterruptedException e) {
103     }
104   }
105
106 }