mirror of
https://github.com/zxing/zxing.git
synced 2025-02-02 05:41:08 -08:00
Finally, much better support for auto-focus, other UI fixes. Now requires JSR-234.
git-svn-id: https://zxing.googlecode.com/svn/trunk@49 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
57c5bc3b8e
commit
ff4f86126f
|
@ -22,6 +22,7 @@
|
|||
<pathelement location="${WTK-home}/lib/cldcapi11.jar"/>
|
||||
<pathelement location="${WTK-home}/lib/midpapi20.jar"/>
|
||||
<pathelement location="${WTK-home}/lib/mmapi.jar"/>
|
||||
<pathelement location="${WTK-home}/lib/jsr234.jar"/>
|
||||
<pathelement location="../core/core.jar"/>
|
||||
</classpath>
|
||||
</javac>
|
||||
|
@ -30,7 +31,7 @@
|
|||
|
||||
<mkdir dir="build-j2me"/>
|
||||
<exec executable="${WTK-home}/bin/preverify1.1">
|
||||
<arg line="-classpath ${WTK-home}/lib/cldcapi11.jar:${WTK-home}/lib/midpapi20.jar:${WTK-home}/lib/mmapi.jar:${WTK-home}/lib/satsa-apdu.jar -d build-j2me build"/>
|
||||
<arg line="-classpath ${WTK-home}/lib/cldcapi11.jar:${WTK-home}/lib/midpapi20.jar:${WTK-home}/lib/mmapi.jar:${WTK-home}/lib/jsr234.jar:${WTK-home}/lib/satsa-apdu.jar -d build-j2me build"/>
|
||||
</exec>
|
||||
|
||||
<copy todir="build-j2me">
|
||||
|
|
|
@ -17,13 +17,14 @@
|
|||
package com.google.zxing.client.j2me;
|
||||
|
||||
import com.google.zxing.MonochromeBitmapSource;
|
||||
import com.google.zxing.Reader;
|
||||
import com.google.zxing.MultiFormatReader;
|
||||
import com.google.zxing.Reader;
|
||||
import com.google.zxing.Result;
|
||||
import com.google.zxing.ReaderException;
|
||||
|
||||
import javax.microedition.amms.control.camera.FocusControl;
|
||||
import javax.microedition.lcdui.Image;
|
||||
import javax.microedition.media.MediaException;
|
||||
import javax.microedition.media.Player;
|
||||
|
||||
/**
|
||||
* @author Sean Owen (srowen@google.com)
|
||||
|
@ -46,23 +47,21 @@ final class SnapshotThread extends Thread {
|
|||
}
|
||||
|
||||
public void run() {
|
||||
Player player = zXingMIDlet.getPlayer();
|
||||
try {
|
||||
zXingMIDlet.getPlayer().stop();
|
||||
setFocus(player);
|
||||
player.stop();
|
||||
byte[] snapshot = zXingMIDlet.getVideoControl().getSnapshot(null);
|
||||
Image capturedImage = Image.createImage(snapshot, 0, snapshot.length);
|
||||
MonochromeBitmapSource source = new LCDUIImageMonochromeBitmapSource(capturedImage);
|
||||
Reader reader = new MultiFormatReader();
|
||||
Result result = reader.decode(source);
|
||||
zXingMIDlet.handleDecodedText(result.getText());
|
||||
} catch (ReaderException re) {
|
||||
zXingMIDlet.showError(re);
|
||||
} catch (MediaException me) {
|
||||
zXingMIDlet.showError(me);
|
||||
} catch (Throwable t) {
|
||||
zXingMIDlet.showError(t);
|
||||
} finally {
|
||||
try {
|
||||
zXingMIDlet.getPlayer().start();
|
||||
player.start();
|
||||
} catch (MediaException me) {
|
||||
// continue?
|
||||
zXingMIDlet.showError(me);
|
||||
|
@ -71,4 +70,20 @@ final class SnapshotThread extends Thread {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
private static void setFocus(Player player) throws MediaException, InterruptedException {
|
||||
FocusControl focusControl = (FocusControl)
|
||||
player.getControl("javax.microedition.amms.control.camera.FocusControl");
|
||||
if (focusControl != null) {
|
||||
if (focusControl.isMacroSupported() && !focusControl.getMacro()) {
|
||||
focusControl.setMacro(true);
|
||||
}
|
||||
if (focusControl.isAutoFocusSupported()) {
|
||||
focusControl.setFocus(FocusControl.AUTO);
|
||||
Thread.sleep(1500L); // let it focus...
|
||||
focusControl.setFocus(FocusControl.AUTO_LOCK);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -27,14 +27,12 @@ import javax.microedition.lcdui.Graphics;
|
|||
*/
|
||||
final class VideoCanvas extends Canvas implements CommandListener {
|
||||
|
||||
private static final Command decode = new Command("Decode", Command.SCREEN, 1);
|
||||
private static final Command exit = new Command("Exit", Command.EXIT, 1);
|
||||
|
||||
private final ZXingMIDlet zXingMIDlet;
|
||||
|
||||
VideoCanvas(ZXingMIDlet zXingMIDlet) {
|
||||
this.zXingMIDlet = zXingMIDlet;
|
||||
addCommand(decode);
|
||||
addCommand(exit);
|
||||
setCommandListener(this);
|
||||
}
|
||||
|
@ -51,9 +49,7 @@ final class VideoCanvas extends Canvas implements CommandListener {
|
|||
}
|
||||
|
||||
public void commandAction(Command command, Displayable displayable) {
|
||||
if (command.equals(decode)) {
|
||||
SnapshotThread.startThread(zXingMIDlet);
|
||||
} else if (command.equals(exit)) {
|
||||
if (command.getCommandType() == Command.EXIT || command.getCommandType() == Command.STOP) {
|
||||
zXingMIDlet.stop();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,6 +30,7 @@ import javax.microedition.media.Player;
|
|||
import javax.microedition.media.control.VideoControl;
|
||||
import javax.microedition.midlet.MIDlet;
|
||||
import javax.microedition.midlet.MIDletStateChangeException;
|
||||
import javax.microedition.amms.control.camera.ZoomControl;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
|
@ -39,6 +40,9 @@ import java.io.IOException;
|
|||
*/
|
||||
public final class ZXingMIDlet extends MIDlet {
|
||||
|
||||
private static final int MAX_ZOOM = 200;
|
||||
|
||||
private Canvas canvas;
|
||||
private Player player;
|
||||
private VideoControl videoControl;
|
||||
|
||||
|
@ -54,29 +58,16 @@ public final class ZXingMIDlet extends MIDlet {
|
|||
try {
|
||||
player = Manager.createPlayer("capture://video");
|
||||
player.realize();
|
||||
setZoom(player);
|
||||
videoControl = (VideoControl) player.getControl("VideoControl");
|
||||
Canvas canvas = new VideoCanvas(this);
|
||||
canvas = new VideoCanvas(this);
|
||||
canvas.setFullScreenMode(true);
|
||||
videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
|
||||
videoControl.setDisplayLocation(0, 0);
|
||||
videoControl.setDisplaySize(canvas.getWidth(), canvas.getHeight());
|
||||
videoControl.setVisible(true);
|
||||
/*
|
||||
FocusControl focusControl = (FocusControl)
|
||||
player.getControl("javax.microedition.amms.control.FocusControl");
|
||||
if (focusControl != null) {
|
||||
if (focusControl.isAutoFocusSupported()) {
|
||||
focusControl.setFocus(FocusControl.AUTO);
|
||||
}
|
||||
if (focusControl.isMacroSupported()) {
|
||||
focusControl.setMacro(true);
|
||||
}
|
||||
} else {
|
||||
System.out.println("FocusControl not supported");
|
||||
}
|
||||
*/
|
||||
Display.getDisplay(this).setCurrent(canvas);
|
||||
player.start();
|
||||
Display.getDisplay(this).setCurrent(canvas);
|
||||
} catch (IOException ioe) {
|
||||
throw new MIDletStateChangeException(ioe.toString());
|
||||
} catch (MediaException me) {
|
||||
|
@ -84,6 +75,22 @@ public final class ZXingMIDlet extends MIDlet {
|
|||
}
|
||||
}
|
||||
|
||||
private static void setZoom(Player player) {
|
||||
// zoom up to 2x if possible
|
||||
ZoomControl zoomControl = (ZoomControl) player.getControl("javax.microedition.amms.control.camera.ZoomControl");
|
||||
if (zoomControl != null) {
|
||||
int maxZoom = zoomControl.getMaxOpticalZoom();
|
||||
if (maxZoom > 100) {
|
||||
zoomControl.setOpticalZoom(maxZoom > MAX_ZOOM ? MAX_ZOOM : maxZoom);
|
||||
} else {
|
||||
int maxDigitalZoom = zoomControl.getMaxDigitalZoom();
|
||||
if (maxDigitalZoom > 100) {
|
||||
zoomControl.setDigitalZoom(maxDigitalZoom > MAX_ZOOM ? MAX_ZOOM : maxDigitalZoom);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void pauseApp() {
|
||||
if (player != null) {
|
||||
try {
|
||||
|
@ -117,23 +124,23 @@ public final class ZXingMIDlet extends MIDlet {
|
|||
// Convenience methods to show dialogs
|
||||
|
||||
void showYesNo(String title, final String text) {
|
||||
Alert alert = new Alert(title, text, null, AlertType.INFO);
|
||||
Alert alert = new Alert(title, text, null, AlertType.CONFIRMATION);
|
||||
alert.setTimeout(Alert.FOREVER);
|
||||
final Command yes = new Command("Yes", Command.OK, 0);
|
||||
final Command no = new Command("No", Command.CANCEL, 0);
|
||||
alert.addCommand(yes);
|
||||
alert.addCommand(no);
|
||||
final Command cancel = new Command("Cancel", Command.CANCEL, 1);
|
||||
alert.addCommand(cancel);
|
||||
CommandListener listener = new CommandListener() {
|
||||
public void commandAction(Command command, Displayable displayable) {
|
||||
if (command.equals(yes)) {
|
||||
if (command.getCommandType() == Command.OK) {
|
||||
try {
|
||||
if (platformRequest(text)) {
|
||||
// Successfully opened URL; exit
|
||||
stop();
|
||||
}
|
||||
platformRequest(text);
|
||||
} catch (ConnectionNotFoundException cnfe) {
|
||||
showError(cnfe);
|
||||
} finally {
|
||||
stop();
|
||||
}
|
||||
} else {
|
||||
// cancel
|
||||
Display.getDisplay(ZXingMIDlet.this).setCurrent(canvas);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -153,7 +160,7 @@ public final class ZXingMIDlet extends MIDlet {
|
|||
|
||||
private void showAlert(Alert alert) {
|
||||
Display display = Display.getDisplay(this);
|
||||
display.setCurrent(alert, display.getCurrent());
|
||||
display.setCurrent(alert, canvas);
|
||||
}
|
||||
|
||||
void handleDecodedText(String text) {
|
||||
|
@ -162,7 +169,7 @@ public final class ZXingMIDlet extends MIDlet {
|
|||
// on URL parsing routines in java.net. This should be somehow worked around: TODO
|
||||
// For now, detect URLs in a simple way, and treat everything else as text
|
||||
if (text.startsWith("http://") || text.startsWith("https://") || maybeURLWithoutScheme(text)) {
|
||||
showYesNo("Open URL?", text);
|
||||
showYesNo("Open web page?", text);
|
||||
} else {
|
||||
showAlert("Barcode detected", text);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue