Broke out classes, tried to add "open URL" support. Still some issues.
[zxing.git] / javame / src / com / google / zxing / client / j2me / SnapshotThread.java
1 /*
2  * Copyright 2007 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.j2me;
18
19 import com.google.zxing.MonochromeBitmapSource;
20 import com.google.zxing.Reader;
21 import com.google.zxing.MultiFormatReader;
22 import com.google.zxing.Result;
23 import com.google.zxing.ReaderException;
24
25 import javax.microedition.lcdui.Image;
26 import javax.microedition.media.MediaException;
27
28 /**
29  * @author Sean Owen (srowen@google.com)
30  */
31 final class SnapshotThread extends Thread {
32
33   private static SnapshotThread currentThread;
34
35   private final ZXingMIDlet zXingMIDlet;
36
37   SnapshotThread(ZXingMIDlet zXingMIDlet) {
38     this.zXingMIDlet = zXingMIDlet;
39   }
40
41   static synchronized void startThread(ZXingMIDlet zXingMIDlet) {
42     if (currentThread == null) {
43       currentThread = new SnapshotThread(zXingMIDlet);
44       currentThread.start();
45     }
46   }
47
48   public void run() {
49     try {
50       zXingMIDlet.getPlayer().stop();
51       byte[] snapshot = zXingMIDlet.getVideoControl().getSnapshot(null);
52       Image capturedImage = Image.createImage(snapshot, 0, snapshot.length);
53       MonochromeBitmapSource source = new LCDUIImageMonochromeBitmapSource(capturedImage);
54       Reader reader = new MultiFormatReader();
55       Result result = reader.decode(source);
56       zXingMIDlet.handleDecodedText(result.getText());
57     } catch (ReaderException re) {
58       zXingMIDlet.showError(re);
59     } catch (MediaException me) {
60       zXingMIDlet.showError(me);
61     } catch (Throwable t) {
62       zXingMIDlet.showError(t);
63     } finally {
64       try {
65         zXingMIDlet.getPlayer().start();
66       } catch (MediaException me) {
67         // continue?
68         zXingMIDlet.showError(me);
69       }
70       currentThread = null;
71     }
72
73   }
74 }