2 * Copyright 2007 ZXing authors
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.amms.control.camera.FlashControl;
23 import javax.microedition.media.Controllable;
24 import javax.microedition.media.MediaException;
25 import javax.microedition.media.Control;
28 * <p>Implementation suitable for JSR-234 phones which takes advantage of advanced camera
33 final class AdvancedMultimediaManager implements MultimediaManager {
35 private static final int NO_ZOOM = 100;
36 private static final int MAX_ZOOM = 200;
37 private static final long FOCUS_TIME_MS = 750L;
38 private static final String DESIRED_METERING = "center-weighted";
39 private static final int DESIRED_FLASH = FlashControl.AUTO;
41 public void setFocus(Controllable player) {
42 FocusControl focusControl =
43 (FocusControl) getControl(player, "javax.microedition.amms.control.camera.FocusControl");
44 if (focusControl != null) {
46 if (focusControl.isMacroSupported() && !focusControl.getMacro()) {
47 focusControl.setMacro(true);
49 if (focusControl.isAutoFocusSupported()) {
50 focusControl.setFocus(FocusControl.AUTO);
52 Thread.sleep(FOCUS_TIME_MS); // let it focus...
53 } catch (InterruptedException ie) {
56 focusControl.setFocus(FocusControl.AUTO_LOCK);
58 } catch (MediaException me) {
64 public void setZoom(Controllable player) {
65 ZoomControl zoomControl = (ZoomControl) getControl(player, "javax.microedition.amms.control.camera.ZoomControl");
66 if (zoomControl != null) {
67 // We zoom in if possible to encourage the viewer to take a snapshot from a greater distance.
68 // This is a crude way of dealing with the fact that many phone cameras will not focus at a
70 int maxZoom = zoomControl.getMaxOpticalZoom();
71 if (maxZoom > NO_ZOOM) {
72 zoomControl.setOpticalZoom(maxZoom > MAX_ZOOM ? MAX_ZOOM : maxZoom);
74 int maxDigitalZoom = zoomControl.getMaxDigitalZoom();
75 if (maxDigitalZoom > NO_ZOOM) {
76 zoomControl.setDigitalZoom(maxDigitalZoom > MAX_ZOOM ? MAX_ZOOM : maxDigitalZoom);
82 public void setExposure(Controllable player) {
83 ExposureControl exposureControl =
84 (ExposureControl) getControl(player, "javax.microedition.amms.control.camera.ExposureControl");
85 if (exposureControl != null) {
87 int[] supportedISOs = exposureControl.getSupportedISOs();
88 if (supportedISOs != null && supportedISOs.length > 0) {
89 int maxISO = Integer.MIN_VALUE;
90 for (int i = 0; i < supportedISOs.length; i++) {
91 if (supportedISOs[i] > maxISO) {
92 maxISO = supportedISOs[i];
96 exposureControl.setISO(maxISO);
97 } catch (MediaException me) {
102 String[] supportedMeterings = exposureControl.getSupportedLightMeterings();
103 if (supportedMeterings != null) {
104 for (int i = 0; i < supportedMeterings.length; i++) {
105 if (DESIRED_METERING.equals(supportedMeterings[i])) {
106 exposureControl.setLightMetering(DESIRED_METERING);
115 public void setFlash(Controllable player) {
116 FlashControl flashControl =
117 (FlashControl) getControl(player, "javax.microedition.amms.control.camera.FlashControl");
118 if (flashControl != null) {
119 int[] supportedFlash = flashControl.getSupportedModes();
120 if (supportedFlash != null && supportedFlash.length > 0) {
121 for (int i = 0; i < supportedFlash.length; i++) {
122 if (supportedFlash[i] == DESIRED_FLASH) {
124 flashControl.setMode(DESIRED_FLASH);
125 } catch (IllegalArgumentException iae) {
135 private static Control getControl(Controllable player, String fullName) {
136 Control control = player.getControl(fullName);
137 if (control == null) {
138 String shortName = fullName.substring(fullName.lastIndexOf('.') + 1);
139 control = player.getControl(shortName);