2 * Copyright 2007 Google Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.j2me;
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;
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>
35 * @author Sean Owen (srowen@google.com)
37 final class AdvancedMultimediaManager {
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";
44 private AdvancedMultimediaManager() {
48 static void setFocus(Controllable player) {
49 FocusControl focusControl = (FocusControl)
50 player.getControl("javax.microedition.amms.control.camera.FocusControl");
51 if (focusControl != null) {
53 if (focusControl.isMacroSupported() && !focusControl.getMacro()) {
54 focusControl.setMacro(true);
56 if (focusControl.isAutoFocusSupported()) {
57 focusControl.setFocus(FocusControl.AUTO);
59 Thread.sleep(FOCUS_TIME_MS); // let it focus...
60 } catch (InterruptedException ie) {
63 focusControl.setFocus(FocusControl.AUTO_LOCK);
65 } catch (MediaException me) {
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
77 int maxZoom = zoomControl.getMaxOpticalZoom();
78 if (maxZoom > NO_ZOOM) {
79 zoomControl.setOpticalZoom(maxZoom > MAX_ZOOM ? MAX_ZOOM : maxZoom);
81 int maxDigitalZoom = zoomControl.getMaxDigitalZoom();
82 if (maxDigitalZoom > NO_ZOOM) {
83 zoomControl.setDigitalZoom(maxDigitalZoom > MAX_ZOOM ? MAX_ZOOM : maxDigitalZoom);
89 static void setExposure(Controllable player) {
90 ExposureControl exposureControl =
91 (ExposureControl) player.getControl("javax.microedition.amms.control.camera.ExposureControl");
92 if (exposureControl != null) {
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];
103 exposureControl.setISO(maxISO);
104 } catch (MediaException me) {
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);