X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=javame%2Fsrc%2Fcom%2Fgoogle%2Fzxing%2Fclient%2Fj2me%2FZXingMIDlet.java;h=02ec6779380c78f88e8157882163e9a3d0d1e1b7;hb=1a11f77abad38f073b9ab0f4c1e1a03b38fd3a06;hp=52c860d9bc198f0001d4f9f6de95d8894246afd1;hpb=686539b373a3fac61dcf5f37a350a75416fd6de5;p=zxing.git diff --git a/javame/src/com/google/zxing/client/j2me/ZXingMIDlet.java b/javame/src/com/google/zxing/client/j2me/ZXingMIDlet.java index 52c860d9..02ec6779 100644 --- a/javame/src/com/google/zxing/client/j2me/ZXingMIDlet.java +++ b/javame/src/com/google/zxing/client/j2me/ZXingMIDlet.java @@ -16,12 +16,7 @@ package com.google.zxing.client.j2me; -import com.google.zxing.MonochromeBitmapSource; -import com.google.zxing.MultiFormatReader; -import com.google.zxing.Reader; -import com.google.zxing.ReaderException; -import com.google.zxing.Result; - +import javax.microedition.io.ConnectionNotFoundException; import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; import javax.microedition.lcdui.Canvas; @@ -29,8 +24,6 @@ import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; -import javax.microedition.lcdui.Graphics; -import javax.microedition.lcdui.Image; import javax.microedition.media.Manager; import javax.microedition.media.MediaException; import javax.microedition.media.Player; @@ -40,45 +33,38 @@ import javax.microedition.midlet.MIDletStateChangeException; import java.io.IOException; /** + *

The actual reader application {@link MIDlet}.

+ * * @author Sean Owen (srowen@google.com) */ -public final class ZXingMIDlet extends MIDlet implements CommandListener { - - private static final Command DECODE = new Command("Decode", Command.SCREEN, 1); - private static final Command EXIT = new Command("Exit", Command.EXIT, 1); +public final class ZXingMIDlet extends MIDlet { + private Canvas canvas; private Player player; private VideoControl videoControl; + Player getPlayer() { + return player; + } + + VideoControl getVideoControl() { + return videoControl; + } + protected void startApp() throws MIDletStateChangeException { try { player = Manager.createPlayer("capture://video"); player.realize(); + AdvancedMultimediaManager.setZoom(player); videoControl = (VideoControl) player.getControl("VideoControl"); - Displayable canvas = new VideoCanvas(); + 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"); - } - */ - canvas.addCommand(DECODE); - canvas.addCommand(EXIT); - canvas.setCommandListener(this); - Display.getDisplay(this).setCurrent(canvas); player.start(); + Display.getDisplay(this).setCurrent(canvas); } catch (IOException ioe) { throw new MIDletStateChangeException(ioe.toString()); } catch (MediaException me) { @@ -86,15 +72,6 @@ public final class ZXingMIDlet extends MIDlet implements CommandListener { } } - public void commandAction(Command command, Displayable displayable) { - if (command.equals(DECODE)) { - new SnapshotThread().start(); - } else if (command.equals(EXIT)) { - destroyApp(false); - notifyDestroyed(); - } - } - protected void pauseApp() { if (player != null) { try { @@ -108,59 +85,111 @@ public final class ZXingMIDlet extends MIDlet implements CommandListener { protected void destroyApp(boolean unconditional) { if (player != null) { + videoControl = null; + try { + player.stop(); + } catch (MediaException me) { + // continue + } + player.deallocate(); player.close(); player = null; - videoControl = null; } } + void stop() { + destroyApp(false); + notifyDestroyed(); + } + + // Convenience methods to show dialogs + + private void showOpenURL(final String text) { + Alert alert = new Alert("Open web page?", text, null, AlertType.CONFIRMATION); + alert.setTimeout(Alert.FOREVER); + 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.getCommandType() == Command.OK) { + try { + platformRequest(text); + } catch (ConnectionNotFoundException cnfe) { + showError(cnfe); + } finally { + stop(); + } + } else { + // cancel + Display.getDisplay(ZXingMIDlet.this).setCurrent(canvas); + } + } + }; + alert.setCommandListener(listener); + showAlert(alert); + } + private void showAlert(String title, String text) { Alert alert = new Alert(title, text, null, AlertType.INFO); alert.setTimeout(Alert.FOREVER); showAlert(alert); } - private void showError(Throwable t) { + void showError(Throwable t) { showAlert(new Alert("Error", t.getMessage(), null, AlertType.ERROR)); } private void showAlert(Alert alert) { Display display = Display.getDisplay(this); - display.setCurrent(alert, display.getCurrent()); - } - - private static class VideoCanvas extends Canvas { - protected void paint(Graphics graphics) { - // do nothing - } + display.setCurrent(alert, canvas); } - private class SnapshotThread extends Thread { - public void run() { - try { - player.stop(); - byte[] snapshot = videoControl.getSnapshot(null); - Image capturedImage = Image.createImage(snapshot, 0, snapshot.length); - MonochromeBitmapSource source = new LCDUIImageMonochromeBitmapSource(capturedImage); - Reader reader = new MultiFormatReader(); - Result result = reader.decode(source); - showAlert("Barcode detected", result.getText()); - } catch (ReaderException re) { - showError(re); - } catch (MediaException me) { - showError(me); - } catch (Throwable t) { - showError(t); - } finally { - try { - player.start(); - } catch (MediaException me) { - // continue? - showError(me); + /// TODO this whole bit needs to be merged with the code in core-ext -- this is messy and duplicative + + void handleDecodedText(String text) { + // This is a crude imitation of the code found in module core-ext, which handles the contents + // in a more sophisticated way. It can't be accessed from JavaME just yet because it relies + // 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://")) { + showOpenURL(text); + } else if (text.startsWith("HTTP://") || text.startsWith("HTTPS://")) { + showOpenURL(decapitalizeProtocol(text)); + } else if (text.startsWith("URL:")) { + showOpenURL(decapitalizeProtocol(text.substring(4))); + } else if (text.startsWith("MEBKM:")) { + int urlIndex = text.indexOf("URL:", 6); + if (urlIndex >= 6) { + String url = text.substring( urlIndex + 4); + int semicolon = url.indexOf((int) ';'); + if (semicolon >= 0) { + url = url.substring(0, semicolon); } + showOpenURL(decapitalizeProtocol(url)); + } else { + showAlert("Barcode detected", text); } + } else if (maybeURLWithoutScheme(text)) { + showOpenURL("http://" + text); + } else { + showAlert("Barcode detected", text); + } + } + private static String decapitalizeProtocol(String url) { + int protocolEnd = url.indexOf("://"); + if (protocolEnd >= 0) { + return url.substring(0, protocolEnd).toLowerCase() + url.substring(protocolEnd); + } else { + return url; } } + /** + * Crudely guesses that a string may represent a URL if it has a '.' and no spaces. + */ + private static boolean maybeURLWithoutScheme(String text) { + return text.indexOf((int) '.') >= 0 && text.indexOf((int) ' ') < 0; + } + }