Implemented possible workaround for Nokias that want to use capture://image and may...
[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.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  * @author Sean Owen (srowen@google.com)
32  */
33 final class SnapshotThread extends Thread {
34
35   private static SnapshotThread currentThread;
36
37   private final ZXingMIDlet zXingMIDlet;
38
39   SnapshotThread(ZXingMIDlet zXingMIDlet) {
40     this.zXingMIDlet = zXingMIDlet;
41   }
42
43   static synchronized void startThread(ZXingMIDlet zXingMIDlet) {
44     if (currentThread == null) {
45       currentThread = new SnapshotThread(zXingMIDlet);
46       currentThread.start();
47     }
48   }
49
50   public void run() {
51     Player player = zXingMIDlet.getPlayer();
52     try {
53       AdvancedMultimediaManager.setFocus(player);
54             try {
55               player.stop();
56             } catch (MediaException me) {
57                     // continue
58             }
59             byte[] snapshot = takeSnapshot();
60       Image capturedImage = Image.createImage(snapshot, 0, snapshot.length);
61       MonochromeBitmapSource source = new LCDUIImageMonochromeBitmapSource(capturedImage);
62       Reader reader = new MultiFormatReader();
63       Result result = reader.decode(source);
64       zXingMIDlet.handleDecodedText(result.getText());
65     } catch (ReaderException re) {
66             // Show a friendlier message on a mere failure to read the barcode
67             zXingMIDlet.showError("No barcode was detected in this image. Try again.");
68     } catch (Throwable t) {
69       zXingMIDlet.showError(t);
70     } finally {
71             try {
72               player.start();
73             } catch (MediaException me) {
74                     // continue
75             }
76       currentThread = null;
77     }
78
79   }
80
81         private byte[] takeSnapshot() throws MediaException {
82                 VideoControl videoControl = zXingMIDlet.getVideoControl();
83                 byte[] snapshot = null;
84                 try {
85             snapshot = videoControl.getSnapshot(null);
86                 } catch (MediaException me) {
87                 }
88                 if (snapshot == null) {
89                         // Fall back on JPEG; seems that some cameras default to PNG even
90                         // when PNG isn't supported!
91                         snapshot = videoControl.getSnapshot("encoding=jpeg");
92                         if (snapshot == null) {
93                                 throw new MediaException("Can't obtain a snapshot");
94                         }
95                 }
96                 return snapshot;
97         }
98
99 }