Now, can build "ZXingReaderBasic" which does not require JSR-234
[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.MultiFormatReader;
21 import com.google.zxing.Reader;
22 import com.google.zxing.Result;
23
24 import javax.microedition.lcdui.Image;
25 import javax.microedition.media.MediaException;
26 import javax.microedition.media.Player;
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     Player player = zXingMIDlet.getPlayer();
50     try {
51       AdvancedMultimediaManager.setFocus(player);
52       player.stop();
53       byte[] snapshot = zXingMIDlet.getVideoControl().getSnapshot(null);
54       Image capturedImage = Image.createImage(snapshot, 0, snapshot.length);
55       MonochromeBitmapSource source = new LCDUIImageMonochromeBitmapSource(capturedImage);
56       Reader reader = new MultiFormatReader();
57       Result result = reader.decode(source);
58       zXingMIDlet.handleDecodedText(result.getText());
59     } catch (Throwable t) {
60       zXingMIDlet.showError(t);
61     } finally {
62       try {
63         player.start();
64       } catch (MediaException me) {
65         // continue?
66         zXingMIDlet.showError(me);
67       }
68       currentThread = null;
69     }
70
71   }
72
73 }