"Regular" version now attempts to set desired camera exposure settings
[zxing.git] / javame / src / com / google / zxing / client / j2me / AdvancedMultimediaManager.java
1 /*
2  * Copyright 2007 Google Inc.
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.media.Controllable;
23 import javax.microedition.media.MediaException;
24
25 /**
26  * <p>This odd class encapsulates all access to functionality exposed by JSR-234,
27  * which provides access to things like focus and zoom. Not all phones support this though.
28  * Normally we might handle loading of code like this via reflection but this is
29  * not available to us in Java ME. So, we create two implementations of the same class --
30  * this one, and another found under source root "src-basic". This one actually calls
31  * JSR-234 methods. The other does nothing. The build script creates two build products then
32  * one compiled with this class and one with other, to create both the JSR-234 version
33  * and the "basic" non-JSR-234 version.</p>
34  *
35  * @author Sean Owen (srowen@google.com)
36  */
37 final class AdvancedMultimediaManager {
38
39   private static final int NO_ZOOM = 100;
40   private static final int MAX_ZOOM = 200;
41   private static final long FOCUS_TIME_MS = 750L;
42   private static final String DESIRED_METERING = "center-weighted";
43
44   private AdvancedMultimediaManager() {
45     // do nothing
46   }
47
48   static void setFocus(Controllable player) {
49     FocusControl focusControl = (FocusControl)
50         player.getControl("javax.microedition.amms.control.camera.FocusControl");
51     if (focusControl != null) {
52       try {
53         if (focusControl.isMacroSupported() && !focusControl.getMacro()) {
54           focusControl.setMacro(true);
55         }
56         if (focusControl.isAutoFocusSupported()) {
57           focusControl.setFocus(FocusControl.AUTO);
58           try {
59             Thread.sleep(FOCUS_TIME_MS); // let it focus...
60           } catch (InterruptedException ie) {
61             // continue
62           }
63           focusControl.setFocus(FocusControl.AUTO_LOCK);
64         }
65       } catch (MediaException me) {
66         // continue
67       }
68     }
69   }
70
71   static void setZoom(Controllable player) {
72     ZoomControl zoomControl = (ZoomControl) player.getControl("javax.microedition.amms.control.camera.ZoomControl");
73     if (zoomControl != null) {
74       // We zoom in if possible to encourage the viewer to take a snapshot from a greater distance.
75       // This is a crude way of dealing with the fact that many phone cameras will not focus at a
76       // very close range.
77       int maxZoom = zoomControl.getMaxOpticalZoom();
78       if (maxZoom > NO_ZOOM) {
79         zoomControl.setOpticalZoom(maxZoom > MAX_ZOOM ? MAX_ZOOM : maxZoom);
80       } else {
81         int maxDigitalZoom = zoomControl.getMaxDigitalZoom();
82         if (maxDigitalZoom > NO_ZOOM) {
83           zoomControl.setDigitalZoom(maxDigitalZoom > MAX_ZOOM ? MAX_ZOOM : maxDigitalZoom);
84         }
85       }
86     }
87   }
88
89   static void setExposure(Controllable player) {
90     ExposureControl exposureControl =
91         (ExposureControl) player.getControl("javax.microedition.amms.control.camera.ExposureControl");
92     if (exposureControl != null) {
93
94       int[] supportedISOs = exposureControl.getSupportedISOs();
95       if (supportedISOs != null && supportedISOs.length > 0) {
96         int maxISO = Integer.MIN_VALUE;
97         for (int i = 0; i < supportedISOs.length; i++) {
98           if (supportedISOs[i] > maxISO) {
99             maxISO = supportedISOs[i];
100           }
101         }
102         try {
103           exposureControl.setISO(maxISO);
104         } catch (MediaException me) {
105           // continue
106         }
107       }
108
109       String[] supportedMeterings = exposureControl.getSupportedLightMeterings();
110       if (supportedMeterings != null) {
111         for (int i = 0; i < supportedMeterings.length; i++) {
112           if (DESIRED_METERING.equals(supportedMeterings[i])) {
113             exposureControl.setLightMetering(DESIRED_METERING);
114             break;
115           }
116         }
117       }
118
119     }
120   }
121
122 }