Going back to old approach of using JSR-234 directly, then compiling with different...
[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 (srowen@google.com)
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   AdvancedMultimediaManager() {
39   }
40
41   public void setFocus(Controllable player) {
42     FocusControl focusControl = (FocusControl)
43         player.getControl("javax.microedition.amms.control.camera.FocusControl");
44     if (focusControl == null) {
45       focusControl = (FocusControl) player.getControl("FocusControl");
46     }
47     if (focusControl != null) {
48       try {
49         if (focusControl.isMacroSupported() && !focusControl.getMacro()) {
50           focusControl.setMacro(true);
51         }
52         if (focusControl.isAutoFocusSupported()) {
53           focusControl.setFocus(FocusControl.AUTO);
54           try {
55             Thread.sleep(FOCUS_TIME_MS); // let it focus...
56           } catch (InterruptedException ie) {
57             // continue
58           }
59           focusControl.setFocus(FocusControl.AUTO_LOCK);
60         }
61       } catch (MediaException me) {
62         // continue
63       }
64     }
65   }
66
67   public void setZoom(Controllable player) {
68     ZoomControl zoomControl = (ZoomControl) player.getControl("javax.microedition.amms.control.camera.ZoomControl");
69     if (zoomControl == null) {
70       zoomControl = (ZoomControl) player.getControl("ZoomControl");
71     }
72     if (zoomControl != null) {
73       // We zoom in if possible to encourage the viewer to take a snapshot from a greater distance.
74       // This is a crude way of dealing with the fact that many phone cameras will not focus at a
75       // very close range.
76       int maxZoom = zoomControl.getMaxOpticalZoom();
77       if (maxZoom > NO_ZOOM) {
78         zoomControl.setOpticalZoom(maxZoom > MAX_ZOOM ? MAX_ZOOM : maxZoom);
79       } else {
80         int maxDigitalZoom = zoomControl.getMaxDigitalZoom();
81         if (maxDigitalZoom > NO_ZOOM) {
82           zoomControl.setDigitalZoom(maxDigitalZoom > MAX_ZOOM ? MAX_ZOOM : maxDigitalZoom);
83         }
84       }
85     }
86   }
87
88   public void setExposure(Controllable player) {
89     ExposureControl exposureControl =
90         (ExposureControl) player.getControl("javax.microedition.amms.control.camera.ExposureControl");
91     if (exposureControl == null) {
92       exposureControl = (ExposureControl) player.getControl("ExposureControl");
93     }
94     if (exposureControl != null) {
95
96       int[] supportedISOs = exposureControl.getSupportedISOs();
97       if (supportedISOs != null && supportedISOs.length > 0) {
98         int maxISO = Integer.MIN_VALUE;
99         for (int i = 0; i < supportedISOs.length; i++) {
100           if (supportedISOs[i] > maxISO) {
101             maxISO = supportedISOs[i];
102           }
103         }
104         try {
105           exposureControl.setISO(maxISO);
106         } catch (MediaException me) {
107           // continue
108         }
109       }
110
111       String[] supportedMeterings = exposureControl.getSupportedLightMeterings();
112       if (supportedMeterings != null) {
113         for (int i = 0; i < supportedMeterings.length; i++) {
114           if (DESIRED_METERING.equals(supportedMeterings[i])) {
115             exposureControl.setLightMetering(DESIRED_METERING);
116             break;
117           }
118         }
119       }
120
121     }
122   }
123
124 }