Added simplistic ability to rotate an LCDUI image (once), to help out some devices...
[zxing.git] / javame / src / com / google / zxing / client / j2me / AdvancedMultimediaManager.java
1 /*
2  * Copyright 2007 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.client.j2me;
18
19 import javax.microedition.amms.control.camera.ExposureControl;
20 import javax.microedition.amms.control.camera.FocusControl;
21 import javax.microedition.amms.control.camera.ZoomControl;
22 import javax.microedition.amms.control.camera.FlashControl;
23 import javax.microedition.media.Controllable;
24 import javax.microedition.media.MediaException;
25 import javax.microedition.media.Control;
26
27 /**
28  * <p>Implementation suitable for JSR-234 phones which takes advantage of advanced camera
29  * capability.</p>
30  *
31  * @author Sean Owen
32  */
33 final class AdvancedMultimediaManager implements MultimediaManager {
34
35   private static final int NO_ZOOM = 100;
36   private static final int MAX_ZOOM = 200;
37   private static final long FOCUS_TIME_MS = 750L;
38   private static final String DESIRED_METERING = "center-weighted";
39   private static final int DESIRED_FLASH = FlashControl.AUTO;
40
41   public void setFocus(Controllable player) {
42     FocusControl focusControl =
43         (FocusControl) getControl(player, "javax.microedition.amms.control.camera.FocusControl");
44     if (focusControl != null) {
45       try {
46         if (focusControl.isMacroSupported() && !focusControl.getMacro()) {
47           focusControl.setMacro(true);
48         }
49         if (focusControl.isAutoFocusSupported()) {
50           focusControl.setFocus(FocusControl.AUTO);
51           try {
52             Thread.sleep(FOCUS_TIME_MS); // let it focus...
53           } catch (InterruptedException ie) {
54             // continue
55           }
56           focusControl.setFocus(FocusControl.AUTO_LOCK);
57         }
58       } catch (MediaException me) {
59         // continue
60       }
61     }
62   }
63
64   public void setZoom(Controllable player) {
65     ZoomControl zoomControl = (ZoomControl) getControl(player, "javax.microedition.amms.control.camera.ZoomControl");
66     if (zoomControl != null) {
67       // We zoom in if possible to encourage the viewer to take a snapshot from a greater distance.
68       // This is a crude way of dealing with the fact that many phone cameras will not focus at a
69       // very close range.
70       int maxZoom = zoomControl.getMaxOpticalZoom();
71       if (maxZoom > NO_ZOOM) {
72         zoomControl.setOpticalZoom(maxZoom > MAX_ZOOM ? MAX_ZOOM : maxZoom);
73       } else {
74         int maxDigitalZoom = zoomControl.getMaxDigitalZoom();
75         if (maxDigitalZoom > NO_ZOOM) {
76           zoomControl.setDigitalZoom(maxDigitalZoom > MAX_ZOOM ? MAX_ZOOM : maxDigitalZoom);
77         }
78       }
79     }
80   }
81
82   public void setExposure(Controllable player) {
83     ExposureControl exposureControl =
84         (ExposureControl) getControl(player, "javax.microedition.amms.control.camera.ExposureControl");
85     if (exposureControl != null) {
86
87       int[] supportedISOs = exposureControl.getSupportedISOs();
88       if (supportedISOs != null && supportedISOs.length > 0) {
89         int maxISO = Integer.MIN_VALUE;
90         for (int i = 0; i < supportedISOs.length; i++) {
91           if (supportedISOs[i] > maxISO) {
92             maxISO = supportedISOs[i];
93           }
94         }
95         try {
96           exposureControl.setISO(maxISO);
97         } catch (MediaException me) {
98           // continue
99         }
100       }
101
102       String[] supportedMeterings = exposureControl.getSupportedLightMeterings();
103       if (supportedMeterings != null) {
104         for (int i = 0; i < supportedMeterings.length; i++) {
105           if (DESIRED_METERING.equals(supportedMeterings[i])) {
106             exposureControl.setLightMetering(DESIRED_METERING);
107             break;
108           }
109         }
110       }
111
112     }
113   }
114
115   public void setFlash(Controllable player) {
116     FlashControl flashControl =
117         (FlashControl) getControl(player, "javax.microedition.amms.control.camera.FlashControl");
118     if (flashControl != null) {
119       int[] supportedFlash = flashControl.getSupportedModes();
120       if (supportedFlash != null && supportedFlash.length > 0) {
121         for (int i = 0; i < supportedFlash.length; i++) {
122           if (supportedFlash[i] == DESIRED_FLASH) {
123             try {
124               flashControl.setMode(DESIRED_FLASH);
125             } catch (IllegalArgumentException iae) {
126               // continue
127             }
128             break;
129           }
130         }
131       }
132     }
133   }
134
135   private static Control getControl(Controllable player, String fullName) {
136     Control control = player.getControl(fullName);
137     if (control == null) {
138       String shortName = fullName.substring(fullName.lastIndexOf('.') + 1);
139       control = player.getControl(shortName);
140     }
141     return control;
142   }
143
144 }