Various improvements to decode speed and efficiency of J2ME client
[zxing.git] / javame / src / com / google / zxing / client / j2me / AdvancedMultimediaManager.java
1 /*
2  * Copyright 2007 Google Inc.
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.FocusControl;
20 import javax.microedition.amms.control.camera.ZoomControl;
21 import javax.microedition.media.Controllable;
22 import javax.microedition.media.MediaException;
23
24 /**
25  * <p>This odd class encapsulates all access to functionality exposed by JSR-234,
26  * which provides access to things like focus and zoom. Not all phones support this though.
27  * Normally we might handle loading of code like this via reflection but this is
28  * not available to us in Java ME. So, we create two implementations of the same class --
29  * this one, and another found under source root "src-basic". This one actually calls
30  * JSR-234 methods. The other does nothing. The build script creates two build products then
31  * one compiled with this class and one with other, to create both the JSR-234 version
32  * and the "basic" non-JSR-234 version.</p>
33  *
34  * @author Sean Owen (srowen@google.com)
35  */
36 final class AdvancedMultimediaManager {
37
38   private static final int NO_ZOOM = 100;
39   private static final int MAX_ZOOM = 200;
40   private static final long FOCUS_TIME_MS = 750L;
41
42   private AdvancedMultimediaManager() {
43     // do nothing
44   }
45
46   static void setFocus(Controllable player) throws MediaException {
47     FocusControl focusControl = (FocusControl)
48         player.getControl("javax.microedition.amms.control.camera.FocusControl");
49     if (focusControl != null) {
50       if (focusControl.isMacroSupported() && !focusControl.getMacro()) {
51         focusControl.setMacro(true);
52       }
53       if (focusControl.isAutoFocusSupported()) {
54         focusControl.setFocus(FocusControl.AUTO);
55         try {
56           Thread.sleep(FOCUS_TIME_MS); // let it focus...
57         } catch (InterruptedException ie) {
58           // continue
59         }
60         try {
61           focusControl.setFocus(FocusControl.AUTO_LOCK);
62         } catch (MediaException me) {
63           // continue; some phones like the SE K850 don't support this?
64         }
65       }
66     }
67   }
68
69   static void setZoom(Controllable player) {
70     ZoomControl zoomControl = (ZoomControl) player.getControl("javax.microedition.amms.control.camera.ZoomControl");
71     if (zoomControl != null) {
72       // We zoom in if possible to encourage the viewer to take a snapshot from a greater distance.
73       // This is a crude way of dealing with the fact that many phone cameras will not focus at a
74       // very close range.
75       int maxZoom = zoomControl.getMaxOpticalZoom();
76       if (maxZoom > NO_ZOOM) {
77         zoomControl.setOpticalZoom(maxZoom > MAX_ZOOM ? MAX_ZOOM : maxZoom);
78       } else {
79         int maxDigitalZoom = zoomControl.getMaxDigitalZoom();
80         if (maxDigitalZoom > NO_ZOOM) {
81           zoomControl.setDigitalZoom(maxDigitalZoom > MAX_ZOOM ? MAX_ZOOM : maxDigitalZoom);
82         }
83       }
84     }
85   }
86
87 }