Implemented Paul's solution to the basic/regular build problem -- a sort of pseudo...
[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.media.Controllable;
23 import javax.microedition.media.MediaException;
24
25 /**
26  * <p>See {@link DefaultMultimediaManager} documentation for details.</p>
27  *
28  * <p>This class should never be directly imported or reference in the code.</p>
29  *
30  * @author Sean Owen (srowen@google.com)
31  */
32 final class AdvancedMultimediaManager implements MultimediaManager {
33
34   private static final int NO_ZOOM = 100;
35   private static final int MAX_ZOOM = 200;
36   private static final long FOCUS_TIME_MS = 750L;
37   private static final String DESIRED_METERING = "center-weighted";
38
39   public void setFocus(Controllable player) {
40     FocusControl focusControl = (FocusControl)
41         player.getControl("javax.microedition.amms.control.camera.FocusControl");
42     if (focusControl != null) {
43       try {
44         if (focusControl.isMacroSupported() && !focusControl.getMacro()) {
45           focusControl.setMacro(true);
46         }
47         if (focusControl.isAutoFocusSupported()) {
48           focusControl.setFocus(FocusControl.AUTO);
49           try {
50             Thread.sleep(FOCUS_TIME_MS); // let it focus...
51           } catch (InterruptedException ie) {
52             // continue
53           }
54           focusControl.setFocus(FocusControl.AUTO_LOCK);
55         }
56       } catch (MediaException me) {
57         // continue
58       }
59     }
60   }
61
62   public void setZoom(Controllable player) {
63     ZoomControl zoomControl = (ZoomControl) player.getControl("javax.microedition.amms.control.camera.ZoomControl");
64     if (zoomControl != null) {
65       // We zoom in if possible to encourage the viewer to take a snapshot from a greater distance.
66       // This is a crude way of dealing with the fact that many phone cameras will not focus at a
67       // very close range.
68       int maxZoom = zoomControl.getMaxOpticalZoom();
69       if (maxZoom > NO_ZOOM) {
70         zoomControl.setOpticalZoom(maxZoom > MAX_ZOOM ? MAX_ZOOM : maxZoom);
71       } else {
72         int maxDigitalZoom = zoomControl.getMaxDigitalZoom();
73         if (maxDigitalZoom > NO_ZOOM) {
74           zoomControl.setDigitalZoom(maxDigitalZoom > MAX_ZOOM ? MAX_ZOOM : maxDigitalZoom);
75         }
76       }
77     }
78   }
79
80   public void setExposure(Controllable player) {
81     ExposureControl exposureControl =
82         (ExposureControl) player.getControl("javax.microedition.amms.control.camera.ExposureControl");
83     if (exposureControl != null) {
84
85       int[] supportedISOs = exposureControl.getSupportedISOs();
86       if (supportedISOs != null && supportedISOs.length > 0) {
87         int maxISO = Integer.MIN_VALUE;
88         for (int i = 0; i < supportedISOs.length; i++) {
89           if (supportedISOs[i] > maxISO) {
90             maxISO = supportedISOs[i];
91           }
92         }
93         try {
94           exposureControl.setISO(maxISO);
95         } catch (MediaException me) {
96           // continue
97         }
98       }
99
100       String[] supportedMeterings = exposureControl.getSupportedLightMeterings();
101       if (supportedMeterings != null) {
102         for (int i = 0; i < supportedMeterings.length; i++) {
103           if (DESIRED_METERING.equals(supportedMeterings[i])) {
104             exposureControl.setLightMetering(DESIRED_METERING);
105             break;
106           }
107         }
108       }
109
110     }
111   }
112
113 }