Now does something with UPC codes. :)
[zxing.git] / javame / src / com / google / zxing / client / j2me / ZXingMIDlet.java
index ba375f8..bba7271 100644 (file)
 
 package com.google.zxing.client.j2me;
 
+import com.google.zxing.client.result.BookmarkDoCoMoResult;
+import com.google.zxing.client.result.EmailAddressResult;
+import com.google.zxing.client.result.EmailDoCoMoResult;
+import com.google.zxing.client.result.ParsedReaderResult;
+import com.google.zxing.client.result.ParsedReaderResultType;
+import com.google.zxing.client.result.UPCParsedResult;
+import com.google.zxing.client.result.URIParsedResult;
+
 import javax.microedition.io.ConnectionNotFoundException;
 import javax.microedition.lcdui.Alert;
 import javax.microedition.lcdui.AlertType;
@@ -30,7 +38,6 @@ 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;
 
 /**
@@ -40,8 +47,6 @@ 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;
@@ -56,9 +61,9 @@ public final class ZXingMIDlet extends MIDlet {
 
   protected void startApp() throws MIDletStateChangeException {
     try {
-      player = Manager.createPlayer("capture://video");
+      player = createPlayer();
       player.realize();
-      setZoom(player);
+      AdvancedMultimediaManager.setZoom(player);
       videoControl = (VideoControl) player.getControl("VideoControl");
       canvas = new VideoCanvas(this);
       canvas.setFullScreenMode(true);
@@ -75,21 +80,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);
-        }
-      }
-    }
-  }
+       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) {
@@ -123,16 +129,18 @@ 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.CONFIRMATION);
+  private void showOpenURL(String title, final String display, final String uri) {
+    Alert alert = new Alert(title, display, null, AlertType.CONFIRMATION);
     alert.setTimeout(Alert.FOREVER);
-    final Command cancel = new Command("Cancel", Command.CANCEL, 1);
-    alert.addCommand(cancel);
+         Command yes = new Command("Yes", Command.OK, 1);
+    alert.addCommand(yes);
+    Command no = new Command("No", Command.CANCEL, 1);
+    alert.addCommand(no);
     CommandListener listener = new CommandListener() {
       public void commandAction(Command command, Displayable displayable) {
         if (command.getCommandType() == Command.OK) {
           try {
-            platformRequest(text);
+            platformRequest(uri);
           } catch (ConnectionNotFoundException cnfe) {
             showError(cnfe);
           } finally {
@@ -148,35 +156,47 @@ public final class ZXingMIDlet extends MIDlet {
     showAlert(alert);
   }
 
-  void showAlert(String title, String text) {
+  private void showAlert(String title, String text) {
     Alert alert = new Alert(title, text, null, AlertType.INFO);
     alert.setTimeout(Alert.FOREVER);
     showAlert(alert);
   }
 
   void showError(Throwable t) {
-    showAlert(new Alert("Error", t.getMessage(), null, AlertType.ERROR));
+         showError(t.getMessage());
   }
 
+       void showError(String message) {
+               showAlert(new Alert("Error", message, null, AlertType.ERROR));
+       }
+
   private void showAlert(Alert alert) {
     Display display = Display.getDisplay(this);
     display.setCurrent(alert, canvas);
   }
 
   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://") || maybeURLWithoutScheme(text)) {
-      showYesNo("Open web page?", text);
+    ParsedReaderResult result = ParsedReaderResult.parseReaderResult(text);
+    ParsedReaderResultType type = result.getType();
+    if (type.equals(ParsedReaderResultType.URI)) {
+      String uri = ((URIParsedResult) result).getURI();
+      showOpenURL("Open web page?", uri, uri);
+    } else if (type.equals(ParsedReaderResultType.BOOKMARK)) {
+      String uri = ((BookmarkDoCoMoResult) result).getURI();
+      showOpenURL("Open web page?", uri, uri);
+    } else if (type.equals(ParsedReaderResultType.EMAIL)) {
+      String email = ((EmailDoCoMoResult) result).getTo();
+      showOpenURL("Compose e-mail?", email, "mailto:" + email);
+    } else if (type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
+      String email = ((EmailAddressResult) result).getEmailAddress();
+      showOpenURL("Compose e-mail?", email, "mailto:" + email);
+    } else if (type.equals(ParsedReaderResultType.UPC)) {
+           String upc = ((UPCParsedResult) result).getUPC();
+           String uri = "http://www.upcdatabase.com/item.asp?upc=" + upc;
+           showOpenURL("Look up UPC?", upc, uri);
     } else {
-      showAlert("Barcode detected", text);
+      showAlert("Barcode detected", result.getDisplayResult());
     }
   }
 
-  private static boolean maybeURLWithoutScheme(String text) {
-    return text.indexOf((int) '.') >= 0 && text.indexOf((int) ' ') < 0;
-  }
-
 }