Increase maximum desired zoom to 2.5x
[zxing.git] / javame / src / com / google / zxing / client / j2me / ZXingMIDlet.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.io.ConnectionNotFoundException;
20 import javax.microedition.lcdui.Alert;
21 import javax.microedition.lcdui.AlertType;
22 import javax.microedition.lcdui.Canvas;
23 import javax.microedition.lcdui.Command;
24 import javax.microedition.lcdui.CommandListener;
25 import javax.microedition.lcdui.Display;
26 import javax.microedition.lcdui.Displayable;
27 import javax.microedition.media.Manager;
28 import javax.microedition.media.MediaException;
29 import javax.microedition.media.Player;
30 import javax.microedition.media.control.VideoControl;
31 import javax.microedition.midlet.MIDlet;
32 import javax.microedition.midlet.MIDletStateChangeException;
33 import javax.microedition.amms.control.camera.ZoomControl;
34 import java.io.IOException;
35
36 /**
37  * <p>The actual reader application {@link MIDlet}.</p>
38  *
39  * @author Sean Owen (srowen@google.com)
40  */
41 public final class ZXingMIDlet extends MIDlet {
42
43   private static final int NO_ZOOM = 100;
44   private static final int MAX_ZOOM = 250;
45
46   private Canvas canvas;
47   private Player player;
48   private VideoControl videoControl;
49
50   Player getPlayer() {
51     return player;
52   }
53
54   VideoControl getVideoControl() {
55     return videoControl;
56   }
57
58   protected void startApp() throws MIDletStateChangeException {
59     try {
60       player = Manager.createPlayer("capture://video");
61       player.realize();
62       setZoom(player);
63       videoControl = (VideoControl) player.getControl("VideoControl");
64       canvas = new VideoCanvas(this);
65       canvas.setFullScreenMode(true);
66       videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
67       videoControl.setDisplayLocation(0, 0);
68       videoControl.setDisplaySize(canvas.getWidth(), canvas.getHeight());
69       videoControl.setVisible(true);
70       player.start();
71       Display.getDisplay(this).setCurrent(canvas);
72     } catch (IOException ioe) {
73       throw new MIDletStateChangeException(ioe.toString());
74     } catch (MediaException me) {
75       throw new MIDletStateChangeException(me.toString());
76     }
77   }
78
79   private static void setZoom(Player player) {
80     ZoomControl zoomControl = (ZoomControl) player.getControl("javax.microedition.amms.control.camera.ZoomControl");
81     if (zoomControl != null) {
82       // We zoom in if possible to encourage the viewer to take a snapshot from a greater distance.
83       // This is a crude way of dealing with the fact that many phone cameras will not focus at a
84       // very close range.
85       int maxZoom = zoomControl.getMaxOpticalZoom();
86       if (maxZoom > NO_ZOOM) {
87         zoomControl.setOpticalZoom(maxZoom > MAX_ZOOM ? MAX_ZOOM : maxZoom);
88       } else {
89         int maxDigitalZoom = zoomControl.getMaxDigitalZoom();
90         if (maxDigitalZoom > NO_ZOOM) {
91           zoomControl.setDigitalZoom(maxDigitalZoom > MAX_ZOOM ? MAX_ZOOM : maxDigitalZoom);
92         }
93       }
94     }
95   }
96
97   protected void pauseApp() {
98     if (player != null) {
99       try {
100         player.stop();
101       } catch (MediaException me) {
102         // continue?
103         showError(me);        
104       }
105     }
106   }
107
108   protected void destroyApp(boolean unconditional) {
109     if (player != null) {
110       videoControl = null;      
111       try {
112         player.stop();
113       } catch (MediaException me) {
114         // continue
115       }
116       player.deallocate();
117       player.close();
118       player = null;
119     }
120   }
121
122   void stop() {
123     destroyApp(false);
124     notifyDestroyed();
125   }
126
127   // Convenience methods to show dialogs
128
129   void showYesNo(String title, final String text) {
130     Alert alert = new Alert(title, text, null, AlertType.CONFIRMATION);
131     alert.setTimeout(Alert.FOREVER);
132     final Command cancel = new Command("Cancel", Command.CANCEL, 1);
133     alert.addCommand(cancel);
134     CommandListener listener = new CommandListener() {
135       public void commandAction(Command command, Displayable displayable) {
136         if (command.getCommandType() == Command.OK) {
137           try {
138             platformRequest(text);
139           } catch (ConnectionNotFoundException cnfe) {
140             showError(cnfe);
141           } finally {
142             stop();
143           }
144         } else {
145           // cancel
146           Display.getDisplay(ZXingMIDlet.this).setCurrent(canvas);
147         }
148       }
149     };
150     alert.setCommandListener(listener);
151     showAlert(alert);
152   }
153
154   void showAlert(String title, String text) {
155     Alert alert = new Alert(title, text, null, AlertType.INFO);
156     alert.setTimeout(Alert.FOREVER);
157     showAlert(alert);
158   }
159
160   void showError(Throwable t) {
161     showAlert(new Alert("Error", t.getMessage(), null, AlertType.ERROR));
162   }
163
164   private void showAlert(Alert alert) {
165     Display display = Display.getDisplay(this);
166     display.setCurrent(alert, canvas);
167   }
168
169   void handleDecodedText(String text) {
170     // This is a crude imitation of the code found in module core-ext, which handles the contents
171     // in a more sophisticated way. It can't be accessed from JavaME just yet because it relies
172     // on URL parsing routines in java.net. This should be somehow worked around: TODO
173     // For now, detect URLs in a simple way, and treat everything else as text
174     if (text.startsWith("http://") || text.startsWith("https://") || maybeURLWithoutScheme(text)) {
175       showYesNo("Open web page?", text);
176     } else {
177       showAlert("Barcode detected", text);
178     }
179   }
180
181   private static boolean maybeURLWithoutScheme(String text) {
182     return text.indexOf((int) '.') >= 0 && text.indexOf((int) ' ') < 0;
183   }
184
185 }