Issue 141 -- add flash control for JSR 234 phones
authorsrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Tue, 3 Feb 2009 10:15:23 +0000 (10:15 +0000)
committersrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Tue, 3 Feb 2009 10:15:23 +0000 (10:15 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@839 59b500cc-1b3d-0410-9834-0bbf25fbcc57

javame/src/com/google/zxing/client/j2me/AdvancedMultimediaManager.java
javame/src/com/google/zxing/client/j2me/DefaultMultimediaManager.java
javame/src/com/google/zxing/client/j2me/MultimediaManager.java
javame/src/com/google/zxing/client/j2me/ZXingMIDlet.java

index 0c137a6..9573e8b 100644 (file)
@@ -19,8 +19,10 @@ package com.google.zxing.client.j2me;
 import javax.microedition.amms.control.camera.ExposureControl;
 import javax.microedition.amms.control.camera.FocusControl;
 import javax.microedition.amms.control.camera.ZoomControl;
+import javax.microedition.amms.control.camera.FlashControl;
 import javax.microedition.media.Controllable;
 import javax.microedition.media.MediaException;
+import javax.microedition.media.Control;
 
 /**
  * <p>Implementation suitable for JSR-234 phones which takes advantage of advanced camera
@@ -34,13 +36,11 @@ final class AdvancedMultimediaManager implements MultimediaManager {
   private static final int MAX_ZOOM = 200;
   private static final long FOCUS_TIME_MS = 750L;
   private static final String DESIRED_METERING = "center-weighted";
+  private static final int DESIRED_FLASH = FlashControl.AUTO;
 
   public void setFocus(Controllable player) {
-    FocusControl focusControl = (FocusControl)
-        player.getControl("javax.microedition.amms.control.camera.FocusControl");
-    if (focusControl == null) {
-      focusControl = (FocusControl) player.getControl("FocusControl");
-    }
+    FocusControl focusControl =
+        (FocusControl) getControl(player, "javax.microedition.amms.control.camera.FocusControl");
     if (focusControl != null) {
       try {
         if (focusControl.isMacroSupported() && !focusControl.getMacro()) {
@@ -62,10 +62,7 @@ final class AdvancedMultimediaManager implements MultimediaManager {
   }
 
   public void setZoom(Controllable player) {
-    ZoomControl zoomControl = (ZoomControl) player.getControl("javax.microedition.amms.control.camera.ZoomControl");
-    if (zoomControl == null) {
-      zoomControl = (ZoomControl) player.getControl("ZoomControl");
-    }
+    ZoomControl zoomControl = (ZoomControl) getControl(player, "javax.microedition.amms.control.camera.ZoomControl");
     if (zoomControl != null) {
       // We zoom in if possible to encourage the viewer to take a snapshot from a greater distance.
       // This is a crude way of dealing with the fact that many phone cameras will not focus at a
@@ -84,10 +81,7 @@ final class AdvancedMultimediaManager implements MultimediaManager {
 
   public void setExposure(Controllable player) {
     ExposureControl exposureControl =
-        (ExposureControl) player.getControl("javax.microedition.amms.control.camera.ExposureControl");
-    if (exposureControl == null) {
-      exposureControl = (ExposureControl) player.getControl("ExposureControl");
-    }
+        (ExposureControl) getControl(player, "javax.microedition.amms.control.camera.ExposureControl");
     if (exposureControl != null) {
 
       int[] supportedISOs = exposureControl.getSupportedISOs();
@@ -118,4 +112,33 @@ final class AdvancedMultimediaManager implements MultimediaManager {
     }
   }
 
+  public void setFlash(Controllable player) {
+    FlashControl flashControl =
+        (FlashControl) getControl(player, "javax.microedition.amms.control.camera.FlashControl");
+    if (flashControl != null) {
+      int[] supportedFlash = flashControl.getSupportedModes();
+      if (supportedFlash != null && supportedFlash.length > 0) {
+        for (int i = 0; i < supportedFlash.length; i++) {
+          if (supportedFlash[i] == DESIRED_FLASH) {
+            try {
+              flashControl.setMode(DESIRED_FLASH);
+            } catch (IllegalArgumentException iae) {
+              // continue
+            }
+            break;
+          }
+        }
+      }
+    }
+  }
+
+  private static Control getControl(Controllable player, String fullName) {
+    Control control = player.getControl(fullName);
+    if (control == null) {
+      String shortName = fullName.substring(fullName.lastIndexOf('.') + 1);
+      control = player.getControl(shortName);
+    }
+    return control;
+  }
+
 }
\ No newline at end of file
index ff00103..e33f3f0 100644 (file)
@@ -34,4 +34,7 @@ final class DefaultMultimediaManager implements MultimediaManager {
   public void setExposure(Controllable player) {
   }
 
+  public void setFlash(Controllable player) {
+  }
+
 }
\ No newline at end of file
index 37ab365..5a99d44 100644 (file)
@@ -34,4 +34,6 @@ interface MultimediaManager {
 
   void setExposure(Controllable player);
 
+  void setFlash(Controllable player);
+
 }
\ No newline at end of file
index 9fc09a9..8335a53 100644 (file)
@@ -94,6 +94,7 @@ public final class ZXingMIDlet extends MIDlet {
       MultimediaManager multimediaManager = buildMultimediaManager();\r
       multimediaManager.setZoom(player);\r
       multimediaManager.setExposure(player);\r
+      multimediaManager.setFlash(player);\r
       videoControl = (VideoControl) player.getControl("VideoControl");\r
       canvas = new VideoCanvas(this);\r
       canvas.setFullScreenMode(true);\r