Improve handling of MultimediaManager to make it a bit easier to make a 'basic' build
[zxing.git] / javame / src / com / google / zxing / client / j2me / SnapshotThread.java
1 /*
2  * Copyright 2007 ZXing authors
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.ReaderException;
23 import com.google.zxing.Result;
24
25 import javax.microedition.lcdui.Image;
26 import javax.microedition.media.MediaException;
27 import javax.microedition.media.Player;
28 import javax.microedition.media.control.VideoControl;
29
30 /**
31  * Thread which does the work of capturing a frame and decoding it.
32  *
33  * @author Sean Owen
34  */
35 final class SnapshotThread implements Runnable {
36
37   private final ZXingMIDlet zXingMIDlet;
38   private final Object waitLock;
39   private volatile boolean done;
40   private final MultimediaManager multimediaManager;
41
42   SnapshotThread(ZXingMIDlet zXingMIDlet) {
43     this.zXingMIDlet = zXingMIDlet;
44     waitLock = new Object();
45     done = false;
46     multimediaManager = ZXingMIDlet.buildMultimediaManager();
47   }
48
49   void continueRun() {
50     synchronized (waitLock) {
51       waitLock.notifyAll();
52     }
53   }
54
55   private void waitForSignal() {
56     synchronized (waitLock) {
57       try {
58         waitLock.wait();
59       } catch (InterruptedException ie) {
60         // continue
61       }
62     }
63   }
64
65   void stop() {
66     done = true;
67     continueRun();
68   }
69
70   public void run() {
71     Player player = zXingMIDlet.getPlayer();
72     do {
73       waitForSignal();
74       try {
75         multimediaManager.setFocus(player);
76         byte[] snapshot = takeSnapshot();
77         Image capturedImage = Image.createImage(snapshot, 0, snapshot.length);
78         MonochromeBitmapSource source = new LCDUIImageMonochromeBitmapSource(capturedImage);
79         Reader reader = new MultiFormatReader();
80         Result result = reader.decode(source);
81         zXingMIDlet.handleDecodedText(result);
82       } catch (ReaderException re) {
83         // Show a friendlier message on a mere failure to read the barcode
84         zXingMIDlet.showError("Sorry, no barcode was found.");
85       } catch (MediaException me) {
86         zXingMIDlet.showError(me);
87       } catch (RuntimeException re) {
88         zXingMIDlet.showError(re);
89       }
90     } while (!done);
91   }
92
93   private byte[] takeSnapshot() throws MediaException {
94     VideoControl videoControl = zXingMIDlet.getVideoControl();
95     byte[] snapshot = null;
96     try {
97       snapshot = videoControl.getSnapshot(null);
98     } catch (MediaException me) {
99     }
100     if (snapshot == null) {
101       // Fall back on JPEG; seems that some cameras default to PNG even
102       // when PNG isn't supported!
103       snapshot = videoControl.getSnapshot("encoding=jpeg");
104       if (snapshot == null) {
105         throw new MediaException("Can't obtain a snapshot");
106       }
107     }
108     return snapshot;
109   }
110
111 }