Implemented possible workaround for Nokias that want to use capture://image and may have trouble with default image encoding in getSnapshot()

git-svn-id: https://zxing.googlecode.com/svn/trunk@157 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2008-01-30 18:58:25 +00:00
parent af603abfff
commit 64cebdfdd7
2 changed files with 48 additions and 9 deletions

View file

@ -25,6 +25,7 @@ import com.google.zxing.Result;
import javax.microedition.lcdui.Image;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;
/**
* @author Sean Owen (srowen@google.com)
@ -50,8 +51,12 @@ final class SnapshotThread extends Thread {
Player player = zXingMIDlet.getPlayer();
try {
AdvancedMultimediaManager.setFocus(player);
player.stop();
byte[] snapshot = zXingMIDlet.getVideoControl().getSnapshot(null);
try {
player.stop();
} catch (MediaException me) {
// continue
}
byte[] snapshot = takeSnapshot();
Image capturedImage = Image.createImage(snapshot, 0, snapshot.length);
MonochromeBitmapSource source = new LCDUIImageMonochromeBitmapSource(capturedImage);
Reader reader = new MultiFormatReader();
@ -63,15 +68,32 @@ final class SnapshotThread extends Thread {
} catch (Throwable t) {
zXingMIDlet.showError(t);
} finally {
try {
player.start();
} catch (MediaException me) {
// continue?
zXingMIDlet.showError(me);
}
try {
player.start();
} catch (MediaException me) {
// continue
}
currentThread = null;
}
}
private byte[] takeSnapshot() throws MediaException {
VideoControl videoControl = zXingMIDlet.getVideoControl();
byte[] snapshot = null;
try {
snapshot = videoControl.getSnapshot(null);
} catch (MediaException me) {
}
if (snapshot == null) {
// Fall back on JPEG; seems that some cameras default to PNG even
// when PNG isn't supported!
snapshot = videoControl.getSnapshot("encoding=jpeg");
if (snapshot == null) {
throw new MediaException("Can't obtain a snapshot");
}
}
return snapshot;
}
}

View file

@ -60,7 +60,7 @@ public final class ZXingMIDlet extends MIDlet {
protected void startApp() throws MIDletStateChangeException {
try {
player = Manager.createPlayer("capture://video");
player = createPlayer();
player.realize();
AdvancedMultimediaManager.setZoom(player);
videoControl = (VideoControl) player.getControl("VideoControl");
@ -79,6 +79,23 @@ public final class ZXingMIDlet extends MIDlet {
}
}
private static Player createPlayer() throws IOException, MediaException {
// Try a workaround for Nokias, which want to use capture://image in some cases
Player player = null;
String platform = System.getProperty("microedition.platform");
if (platform != null && platform.indexOf("Nokia") >= 0) {
try {
player = Manager.createPlayer("capture://image");
} catch (MediaException me) {
// if this fails, just continue with capture://video
}
}
if (player == null) {
player = Manager.createPlayer("capture://video");
}
return player;
}
protected void pauseApp() {
if (player != null) {
try {