2 * Copyright 2007 Google Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.j2me;
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;
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;
31 * Thread which does the work of capturing a frame and decoding it.
33 * @author Sean Owen (srowen@google.com)
35 final class SnapshotThread implements Runnable {
37 private final ZXingMIDlet zXingMIDlet;
38 private final Object waitLock;
41 SnapshotThread(ZXingMIDlet zXingMIDlet) {
42 this.zXingMIDlet = zXingMIDlet;
43 waitLock = new Object();
48 synchronized (waitLock) {
53 private void waitForSignal() {
54 synchronized (waitLock) {
57 } catch (InterruptedException ie) {
69 Player player = zXingMIDlet.getPlayer();
73 AdvancedMultimediaManager.setFocus(player);
74 byte[] snapshot = takeSnapshot();
75 Image capturedImage = Image.createImage(snapshot, 0, snapshot.length);
76 MonochromeBitmapSource source = new LCDUIImageMonochromeBitmapSource(capturedImage);
77 Reader reader = new MultiFormatReader();
78 Result result = reader.decode(source);
79 zXingMIDlet.handleDecodedText(result);
80 } catch (ReaderException re) {
81 // Show a friendlier message on a mere failure to read the barcode
82 zXingMIDlet.showError("Sorry, no barcode was found.");
83 } catch (MediaException me) {
84 zXingMIDlet.showError(me);
85 } catch (RuntimeException re) {
86 zXingMIDlet.showError(re);
91 private byte[] takeSnapshot() throws MediaException {
92 VideoControl videoControl = zXingMIDlet.getVideoControl();
93 byte[] snapshot = null;
95 snapshot = videoControl.getSnapshot(null);
96 } catch (MediaException me) {
98 if (snapshot == null) {
99 // Fall back on JPEG; seems that some cameras default to PNG even
100 // when PNG isn't supported!
101 snapshot = videoControl.getSnapshot("encoding=jpeg");
102 if (snapshot == null) {
103 throw new MediaException("Can't obtain a snapshot");