0c137a69edaf075f6db55f24723c3bc7b95d60f5
[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>Implementation suitable for JSR-234 phones which takes advantage of advanced camera
27  * capability.</p>
28  *
29  * @author Sean Owen
30  */
31 final class AdvancedMultimediaManager implements MultimediaManager {
32
33   private static final int NO_ZOOM = 100;
34   private static final int MAX_ZOOM = 200;
35   private static final long FOCUS_TIME_MS = 750L;
36   private static final String DESIRED_METERING = "center-weighted";
37
38   public void setFocus(Controllable player) {
39     FocusControl focusControl = (FocusControl)
40         player.getControl("javax.microedition.amms.control.camera.FocusControl");
41     if (focusControl == null) {
42       focusControl = (FocusControl) player.getControl("FocusControl");
43     }
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) player.getControl("javax.microedition.amms.control.camera.ZoomControl");
66     if (zoomControl == null) {
67       zoomControl = (ZoomControl) player.getControl("ZoomControl");
68     }
69     if (zoomControl != null) {
70       // We zoom in if possible to encourage the viewer to take a snapshot from a greater distance.
71       // This is a crude way of dealing with the fact that many phone cameras will not focus at a
72       // very close range.
73       int maxZoom = zoomControl.getMaxOpticalZoom();
74       if (maxZoom > NO_ZOOM) {
75         zoomControl.setOpticalZoom(maxZoom > MAX_ZOOM ? MAX_ZOOM : maxZoom);
76       } else {
77         int maxDigitalZoom = zoomControl.getMaxDigitalZoom();
78         if (maxDigitalZoom > NO_ZOOM) {
79           zoomControl.setDigitalZoom(maxDigitalZoom > MAX_ZOOM ? MAX_ZOOM : maxDigitalZoom);
80         }
81       }
82     }
83   }
84
85   public void setExposure(Controllable player) {
86     ExposureControl exposureControl =
87         (ExposureControl) player.getControl("javax.microedition.amms.control.camera.ExposureControl");
88     if (exposureControl == null) {
89       exposureControl = (ExposureControl) player.getControl("ExposureControl");
90     }
91     if (exposureControl != null) {
92
93       int[] supportedISOs = exposureControl.getSupportedISOs();
94       if (supportedISOs != null && supportedISOs.length > 0) {
95         int maxISO = Integer.MIN_VALUE;
96         for (int i = 0; i < supportedISOs.length; i++) {
97           if (supportedISOs[i] > maxISO) {
98             maxISO = supportedISOs[i];
99           }
100         }
101         try {
102           exposureControl.setISO(maxISO);
103         } catch (MediaException me) {
104           // continue
105         }
106       }
107
108       String[] supportedMeterings = exposureControl.getSupportedLightMeterings();
109       if (supportedMeterings != null) {
110         for (int i = 0; i < supportedMeterings.length; i++) {
111           if (DESIRED_METERING.equals(supportedMeterings[i])) {
112             exposureControl.setLightMetering(DESIRED_METERING);
113             break;
114           }
115         }
116       }
117
118     }
119   }
120
121 }