/*
- * Copyright 2007 Google Inc.
+ * Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
package com.google.zxing.client.j2me;
-import com.google.zxing.MonochromeBitmapSource;
-import com.google.zxing.MultiFormatReader;
-import com.google.zxing.Reader;
-import com.google.zxing.ReaderException;
import com.google.zxing.Result;
-
+import com.google.zxing.client.result.EmailAddressParsedResult;
+import com.google.zxing.client.result.ParsedResult;
+import com.google.zxing.client.result.ParsedResultType;
+import com.google.zxing.client.result.ResultParser;
+import com.google.zxing.client.result.SMSParsedResult;
+import com.google.zxing.client.result.TelParsedResult;
+import com.google.zxing.client.result.ProductParsedResult;
+import com.google.zxing.client.result.URIParsedResult;
+
+import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
-import javax.microedition.lcdui.Graphics;
-import javax.microedition.lcdui.Image;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
/**
* <p>The actual reader application {@link MIDlet}.</p>
*
- * @author Sean Owen (srowen@google.com)
+ * @author Sean Owen
*/
public final class ZXingMIDlet extends MIDlet {
+ private static final int ALERT_TIMEOUT_MS = 5 * 1000;
+
+ private Canvas canvas;
private Player player;
private VideoControl videoControl;
+ Displayable getCanvas() {
+ return canvas;
+ }
+
+ Player getPlayer() {
+ return player;
+ }
+
+ VideoControl getVideoControl() {
+ return videoControl;
+ }
+
+ static MultimediaManager buildMultimediaManager() {
+ return new AdvancedMultimediaManager();
+ // Comment line above / uncomment below to make the basic version
+ // return new DefaultMultimediaManager();
+ }
+
protected void startApp() throws MIDletStateChangeException {
try {
- player = Manager.createPlayer("capture://video");
+ player = createPlayer();
player.realize();
+ MultimediaManager multimediaManager = buildMultimediaManager();
+ multimediaManager.setZoom(player);
+ multimediaManager.setExposure(player);
videoControl = (VideoControl) player.getControl("VideoControl");
- Displayable canvas = new VideoCanvas();
+ canvas = new VideoCanvas(this);
+ canvas.setFullScreenMode(true);
videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
videoControl.setDisplayLocation(0, 0);
videoControl.setDisplaySize(canvas.getWidth(), canvas.getHeight());
videoControl.setVisible(true);
- /*
- FocusControl focusControl = (FocusControl)
- player.getControl("javax.microedition.amms.control.FocusControl");
- if (focusControl != null) {
- if (focusControl.isAutoFocusSupported()) {
- focusControl.setFocus(FocusControl.AUTO);
- }
- if (focusControl.isMacroSupported()) {
- focusControl.setMacro(true);
- }
- } else {
- System.out.println("FocusControl not supported");
- }
- */
- Display.getDisplay(this).setCurrent(canvas);
player.start();
+ Display.getDisplay(this).setCurrent(canvas);
} catch (IOException ioe) {
throw new MIDletStateChangeException(ioe.toString());
} catch (MediaException me) {
}
}
+ private static Player createPlayer() throws IOException, MediaException {
+ // Try a workaround for Nokias, which want to use capture://image in some cases
+ Player player = null;
+ String platform = System.getProperty("microedition.platform");
+ if (platform != null && platform.indexOf("Nokia") >= 0) {
+ try {
+ player = Manager.createPlayer("capture://image");
+ } catch (MediaException me) {
+ // if this fails, just continue with capture://video
+ } catch (Error e) {
+ // Ugly, but, it seems the Nokia N70 throws "java.lang.Error: 136" here
+ // We should still try to continue
+ }
+ }
+ if (player == null) {
+ player = Manager.createPlayer("capture://video");
+ }
+ return player;
+ }
+
protected void pauseApp() {
if (player != null) {
try {
player.stop();
} catch (MediaException me) {
// continue?
- showError(me);
+ showError(me);
}
}
}
protected void destroyApp(boolean unconditional) {
if (player != null) {
+ videoControl = null;
+ try {
+ player.stop();
+ } catch (MediaException me) {
+ // continue
+ }
+ player.deallocate();
player.close();
player = null;
- videoControl = null;
}
}
+ void stop() {
+ destroyApp(false);
+ notifyDestroyed();
+ }
+
// Convenience methods to show dialogs
+ private void showOpenURL(String title, String display, final String uri) {
+ Alert alert = new Alert(title, display, null, AlertType.CONFIRMATION);
+ alert.setTimeout(ALERT_TIMEOUT_MS);
+ Command yes = new Command("Yes", Command.OK, 1);
+ alert.addCommand(yes);
+ Command no = new Command("No", Command.CANCEL, 1);
+ alert.addCommand(no);
+ CommandListener listener = new CommandListener() {
+ public void commandAction(Command command, Displayable displayable) {
+ if (command.getCommandType() == Command.OK) {
+ try {
+ platformRequest(uri);
+ } catch (ConnectionNotFoundException cnfe) {
+ showError(cnfe);
+ } finally {
+ stop();
+ }
+ } else {
+ // cancel
+ Display.getDisplay(ZXingMIDlet.this).setCurrent(getCanvas());
+ }
+ }
+ };
+ alert.setCommandListener(listener);
+ showAlert(alert);
+ }
+
private void showAlert(String title, String text) {
Alert alert = new Alert(title, text, null, AlertType.INFO);
- alert.setTimeout(Alert.FOREVER);
+ alert.setTimeout(ALERT_TIMEOUT_MS);
showAlert(alert);
}
- private void showError(Throwable t) {
- showAlert(new Alert("Error", t.getMessage(), null, AlertType.ERROR));
+ void showError(Throwable t) {
+ String message = t.getMessage();
+ if (message != null && message.length() > 0) {
+ showError(message);
+ } else {
+ showError(t.toString());
+ }
}
- private void showAlert(Alert alert) {
- Display display = Display.getDisplay(this);
- display.setCurrent(alert, display.getCurrent());
+ void showError(String message) {
+ showAlert(new Alert("Error", message, null, AlertType.ERROR));
}
- private class VideoCanvas extends Canvas implements CommandListener {
- private final Command decode = new Command("Decode", Command.SCREEN, 1);
- private final Command exit = new Command("Exit", Command.EXIT, 1);
- private VideoCanvas() {
- addCommand(decode);
- addCommand(exit);
- setCommandListener(this);
- }
- protected void paint(Graphics graphics) {
- // do nothing
- }
- protected void keyPressed(int keyCode) {
- if (FIRE == getGameAction(keyCode)) {
- new SnapshotThread().start();
- }
- }
- public void commandAction(Command command, Displayable displayable) {
- if (command.equals(decode)) {
- new SnapshotThread().start();
- } else if (command.equals(exit)) {
- destroyApp(false);
- notifyDestroyed();
- }
- }
+ private void showAlert(Alert alert) {
+ Display display = Display.getDisplay(this);
+ display.setCurrent(alert, canvas);
}
- // TODO make sure we do not start two threads at once
- private class SnapshotThread extends Thread {
- public void run() {
- try {
- player.stop();
- byte[] snapshot = videoControl.getSnapshot(null);
- Image capturedImage = Image.createImage(snapshot, 0, snapshot.length);
- MonochromeBitmapSource source = new LCDUIImageMonochromeBitmapSource(capturedImage);
- Reader reader = new MultiFormatReader();
- Result result = reader.decode(source);
- showAlert("Barcode detected", result.getText());
- } catch (ReaderException re) {
- showError(re);
- } catch (MediaException me) {
- showError(me);
- } catch (Throwable t) {
- showError(t);
- } finally {
- try {
- player.start();
- } catch (MediaException me) {
- // continue?
- showError(me);
- }
- }
-
+ void handleDecodedText(Result theResult) {
+ ParsedResult result = ResultParser.parseResult(theResult);
+ ParsedResultType type = result.getType();
+ if (type.equals(ParsedResultType.URI)) {
+ String uri = ((URIParsedResult) result).getURI();
+ showOpenURL("Open Web Page?", uri, uri);
+ } else if (type.equals(ParsedResultType.EMAIL_ADDRESS)) {
+ EmailAddressParsedResult emailResult = (EmailAddressParsedResult) result;
+ showOpenURL("Compose E-mail?", emailResult.getEmailAddress(), emailResult.getMailtoURI());
+ } else if (type.equals(ParsedResultType.SMS)) {
+ SMSParsedResult smsResult = (SMSParsedResult) result;
+ showOpenURL("Compose SMS?", smsResult.getNumber(), smsResult.getSMSURI());
+ } else if (type.equals(ParsedResultType.PRODUCT)) {
+ ProductParsedResult productResult = (ProductParsedResult) result;
+ String uri = "http://www.upcdatabase.com/item.asp?upc=" + productResult.getNormalizedProductID();
+ showOpenURL("Look Up Barcode Online?", productResult.getProductID(), uri);
+ } else if (type.equals(ParsedResultType.TEL)) {
+ TelParsedResult telResult = (TelParsedResult) result;
+ showOpenURL("Dial Number?", telResult.getNumber(), telResult.getTelURI());
+ } else {
+ showAlert("Barcode Detected", result.getDisplayResult());
}
}