Commit Simon's changes for Issue 134

git-svn-id: https://zxing.googlecode.com/svn/trunk@838 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2009-02-01 11:44:56 +00:00
parent db61519508
commit 270edc5785
3 changed files with 450 additions and 316 deletions

View file

@ -0,0 +1,96 @@
/*
* Copyright 2009 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.client.j2me;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
/**
* The Menu form simply adds Command Listener functionality
* to the standard List User Interface component.
*
* @author Simon Flannery (simon.flannery@gmail.com)
*/
final class Menu extends List implements CommandListener {
private final ZXingMIDlet zXingMIDlet;
private Command cancelCommand;
private Command barcodeCommand;
/**
* Creates a new Search List and initialises all components.
*
* @param parent The Parent ZXing MIDlet.
* @param title The title of the List.
* @param item The caption of the item action Command.
*/
Menu(ZXingMIDlet parent, String title, String item) {
super(title, IMPLICIT); // Set the title of the form
zXingMIDlet = parent;
// Build the UI components
cancelCommand = new Command("Cancel", Command.CANCEL, 0);
barcodeCommand = new Command(item, Command.ITEM, 0);
addCommand(cancelCommand);
addCommand(barcodeCommand);
setCommandListener(this);
}
/**
* A convenience method for getting the selected option item.
*
* @return The selected option represented as a String. If no option is
* selected, then the empty string is returned.
*/
public String getSelectedString() {
String result = "";
if (getSelectedIndex() != -1) {
result = getString(getSelectedIndex());
}
return result;
}
/**
* A convenience method for removing all items from the list.
* While the size of the list does not equal zero, the first item of the list is deleted.
*/
public void clear() {
while (size() != 0) { // Delete the first-most element until there is no first-most element
delete(0);
}
}
/**
* CommandListener Required Implementation for capturing soft key presses.
* This is where all call back methods (of the MIDlet) are serviced.
*
* @param command The command requiring attention.
* @param displayable The Display.
*/
public void commandAction(Command command, Displayable displayable) {
if (command == cancelCommand) { /* Detecting the soft key press. */
Display.getDisplay(zXingMIDlet).setCurrent(zXingMIDlet.getCanvas());
} else if (command == barcodeCommand || command == SELECT_COMMAND) {
if (getSelectedIndex() != -1) {
zXingMIDlet.itemRequest();
}
}
}
}

View file

@ -1,66 +1,71 @@
/* /*
* Copyright 2007 ZXing authors * Copyright 2007 ZXing authors
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.google.zxing.client.j2me; package com.google.zxing.client.j2me;
import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command; import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Graphics;
/** /**
* The main {@link Canvas} onto which the camera's field of view is painted. * The main {@link Canvas} onto which the camera's field of view is painted.
* This class manages decoding via {@link SnapshotThread}. * This class manages decoding via {@link SnapshotThread}.
* *
* @author Sean Owen * @author Sean Owen
*/ * @author Simon Flannery
final class VideoCanvas extends Canvas implements CommandListener { */
final class VideoCanvas extends Canvas implements CommandListener {
private static final Command exit = new Command("Exit", Command.EXIT, 1);
private static final Command exit = new Command("Exit", Command.EXIT, 1);
private final ZXingMIDlet zXingMIDlet; private static final Command history = new Command("History", Command.ITEM, 0);
private final SnapshotThread snapshotThread;
private final ZXingMIDlet zXingMIDlet;
VideoCanvas(ZXingMIDlet zXingMIDlet) { private final SnapshotThread snapshotThread;
this.zXingMIDlet = zXingMIDlet;
addCommand(exit); VideoCanvas(ZXingMIDlet zXingMIDlet) {
setCommandListener(this); this.zXingMIDlet = zXingMIDlet;
snapshotThread = new SnapshotThread(zXingMIDlet); addCommand(exit);
new Thread(snapshotThread).start(); addCommand(history);
} setCommandListener(this);
snapshotThread = new SnapshotThread(zXingMIDlet);
protected void paint(Graphics graphics) { new Thread(snapshotThread).start();
// do nothing }
}
protected void paint(Graphics graphics) {
protected void keyPressed(int keyCode) { // do nothing
// Any valid game key will trigger a capture }
if (getGameAction(keyCode) != 0) {
snapshotThread.continueRun(); protected void keyPressed(int keyCode) {
} else { // Any valid game key will trigger a capture
super.keyPressed(keyCode); if (getGameAction(keyCode) != 0) {
} snapshotThread.continueRun();
} } else {
super.keyPressed(keyCode);
public void commandAction(Command command, Displayable displayable) { }
int type = command.getCommandType(); }
if (type == Command.EXIT || type == Command.STOP || type == Command.BACK || type == Command.CANCEL) {
snapshotThread.stop(); public void commandAction(Command command, Displayable displayable) {
zXingMIDlet.stop(); int type = command.getCommandType();
} if (command == history) {
} zXingMIDlet.historyRequest();
} } else if (type == Command.EXIT || type == Command.STOP || type == Command.BACK || type == Command.CANCEL) {
snapshotThread.stop();
zXingMIDlet.stop();
}
}
}

View file

@ -1,250 +1,283 @@
/* /*
* Copyright 2007 ZXing authors * Copyright 2007 ZXing authors
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.google.zxing.client.j2me; package com.google.zxing.client.j2me;
import com.google.zxing.Result; import com.google.zxing.Result;
import com.google.zxing.client.result.EmailAddressParsedResult; import com.google.zxing.client.result.EmailAddressParsedResult;
import com.google.zxing.client.result.ParsedResult; import com.google.zxing.client.result.ParsedResult;
import com.google.zxing.client.result.ParsedResultType; import com.google.zxing.client.result.ParsedResultType;
import com.google.zxing.client.result.ResultParser; import com.google.zxing.client.result.ResultParser;
import com.google.zxing.client.result.SMSParsedResult; import com.google.zxing.client.result.SMSParsedResult;
import com.google.zxing.client.result.TelParsedResult; import com.google.zxing.client.result.TelParsedResult;
import com.google.zxing.client.result.ProductParsedResult; import com.google.zxing.client.result.ProductParsedResult;
import com.google.zxing.client.result.URIParsedResult; import com.google.zxing.client.result.URIParsedResult;
import javax.microedition.lcdui.Image; import javax.microedition.lcdui.Image;
import javax.microedition.io.ConnectionNotFoundException; import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType; import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command; import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Displayable;
import javax.microedition.media.Manager; import javax.microedition.media.Manager;
import javax.microedition.media.MediaException; import javax.microedition.media.MediaException;
import javax.microedition.media.Player; import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl; import javax.microedition.media.control.VideoControl;
import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException; import javax.microedition.midlet.MIDletStateChangeException;
import java.io.IOException; import java.io.IOException;
import java.util.Vector;
/**
* <p>The actual reader application {@link MIDlet}.</p> /**
* * <p>The actual reader application {@link MIDlet}.</p>
* @author Sean Owen *
*/ * @author Sean Owen
public final class ZXingMIDlet extends MIDlet { * @author Simon Flannery
*/
private static final int ALERT_TIMEOUT_MS = 5 * 1000; public final class ZXingMIDlet extends MIDlet {
private Canvas canvas; private static final int ALERT_TIMEOUT_MS = 5 * 1000;
private Player player;
private VideoControl videoControl; private Canvas canvas;
private Alert confirmation; private Player player;
private Alert alert; private VideoControl videoControl;
private Alert confirmation;
Displayable getCanvas() { private Alert alert;
return canvas; private Menu history;
} private Vector resultHistory;
Player getPlayer() { Displayable getCanvas() {
return player; return canvas;
} }
VideoControl getVideoControl() { Player getPlayer() {
return videoControl; return player;
} }
static MultimediaManager buildMultimediaManager() { VideoControl getVideoControl() {
return new AdvancedMultimediaManager(); return videoControl;
// Comment line above / uncomment below to make the basic version }
// return new DefaultMultimediaManager();
} static MultimediaManager buildMultimediaManager() {
return new AdvancedMultimediaManager();
protected void startApp() throws MIDletStateChangeException { // Comment line above / uncomment below to make the basic version
try { // return new DefaultMultimediaManager();
Image image = Image.createImage("/res/zxing-icon.png"); }
SplashThread splash = new SplashThread(this, 2000, image);
Display.getDisplay(this).setCurrent(splash); protected void startApp() throws MIDletStateChangeException {
player = createPlayer(); try {
player.realize(); Image image = Image.createImage("/res/zxing-icon.png");
MultimediaManager multimediaManager = buildMultimediaManager(); Displayable splash = new SplashThread(this, 2000, image);
multimediaManager.setZoom(player); Display.getDisplay(this).setCurrent(splash);
multimediaManager.setExposure(player);
videoControl = (VideoControl) player.getControl("VideoControl"); resultHistory = new Vector(5);
canvas = new VideoCanvas(this); history = new Menu(this, "Scan History", "Use");
canvas.setFullScreenMode(true);
videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas); player = createPlayer();
videoControl.setDisplayLocation(0, 0); player.realize();
videoControl.setDisplaySize(canvas.getWidth(), canvas.getHeight()); MultimediaManager multimediaManager = buildMultimediaManager();
} catch (IOException ioe) { multimediaManager.setZoom(player);
throw new MIDletStateChangeException(ioe.toString()); multimediaManager.setExposure(player);
} catch (MediaException me) { videoControl = (VideoControl) player.getControl("VideoControl");
throw new MIDletStateChangeException(me.toString()); canvas = new VideoCanvas(this);
} canvas.setFullScreenMode(true);
videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
// Set up one confirmation and alert object to re-use videoControl.setDisplayLocation(0, 0);
confirmation = new Alert(null); videoControl.setDisplaySize(canvas.getWidth(), canvas.getHeight());
confirmation.setType(AlertType.CONFIRMATION); } catch (IOException ioe) {
confirmation.setTimeout(ALERT_TIMEOUT_MS); throw new MIDletStateChangeException(ioe.toString());
Command yes = new Command("Yes", Command.OK, 1); } catch (MediaException me) {
confirmation.addCommand(yes); throw new MIDletStateChangeException(me.toString());
Command no = new Command("No", Command.CANCEL, 1); }
confirmation.addCommand(no);
alert = new Alert(null); // Set up one confirmation and alert object to re-use
alert.setTimeout(ALERT_TIMEOUT_MS); confirmation = new Alert(null);
} confirmation.setType(AlertType.CONFIRMATION);
confirmation.setTimeout(ALERT_TIMEOUT_MS);
void splashDone() { Command yes = new Command("Yes", Command.OK, 1);
try { confirmation.addCommand(yes);
videoControl.setVisible(true); Command no = new Command("No", Command.CANCEL, 1);
player.start(); confirmation.addCommand(no);
} catch (MediaException me) { alert = new Alert(null);
// continue alert.setTimeout(ALERT_TIMEOUT_MS);
} }
Display.getDisplay(this).setCurrent(canvas);
} void splashDone() {
try {
private static Player createPlayer() throws IOException, MediaException { videoControl.setVisible(true);
// Try a workaround for Nokias, which want to use capture://image in some cases player.start();
Player player = null; } catch (MediaException me) {
String platform = System.getProperty("microedition.platform"); showError(me);
if (platform != null && platform.indexOf("Nokia") >= 0) { }
try { Display.getDisplay(this).setCurrent(canvas);
player = Manager.createPlayer("capture://image"); }
} catch (MediaException me) {
// if this fails, just continue with capture://video private static Player createPlayer() throws IOException, MediaException {
} catch (Error e) { // Try a workaround for Nokias, which want to use capture://image in some cases
// Ugly, but, it seems the Nokia N70 throws "java.lang.Error: 136" here Player player = null;
// We should still try to continue String platform = System.getProperty("microedition.platform");
} if (platform != null && platform.indexOf("Nokia") >= 0) {
} try {
if (player == null) { player = Manager.createPlayer("capture://image");
player = Manager.createPlayer("capture://video"); } catch (MediaException me) {
} // if this fails, just continue with capture://video
return player; } catch (Error e) {
} // Ugly, but, it seems the Nokia N70 throws "java.lang.Error: 136" here
// We should still try to continue
protected void pauseApp() { }
if (player != null) { }
try { if (player == null) {
player.stop(); player = Manager.createPlayer("capture://video");
} catch (MediaException me) { }
// continue? return player;
showError(me); }
}
} protected void pauseApp() {
} if (player != null) {
try {
protected void destroyApp(boolean unconditional) { player.stop();
if (player != null) { } catch (MediaException me) {
videoControl = null; // continue?
try { showError(me);
player.stop(); }
} catch (MediaException me) { }
// continue }
}
player.deallocate(); protected void destroyApp(boolean unconditional) {
player.close(); if (player != null) {
player = null; videoControl = null;
} try {
} player.stop();
} catch (MediaException me) {
void stop() { // continue
destroyApp(false); }
notifyDestroyed(); player.deallocate();
} player.close();
player = null;
// Convenience methods to show dialogs }
}
private void showOpenURL(String title, String display, final String uri) {
confirmation.setTitle(title); void stop() {
confirmation.setString(display); destroyApp(false);
CommandListener listener = new CommandListener() { notifyDestroyed();
public void commandAction(Command command, Displayable displayable) { }
if (command.getCommandType() == Command.OK) {
try { void historyRequest() {
platformRequest(uri); Display.getDisplay(this).setCurrent(history);
} catch (ConnectionNotFoundException cnfe) { }
showError(cnfe);
} finally { // Convenience methods to show dialogs
stop();
} private void showOpenURL(String title, String display, final String uri) {
} else { confirmation.setTitle(title);
// cancel confirmation.setString(display);
Display.getDisplay(ZXingMIDlet.this).setCurrent(getCanvas()); CommandListener listener = new CommandListener() {
} public void commandAction(Command command, Displayable displayable) {
} if (command.getCommandType() == Command.OK) {
}; try {
confirmation.setCommandListener(listener); platformRequest(uri);
showAlert(confirmation); } catch (ConnectionNotFoundException cnfe) {
} showError(cnfe);
} finally {
private void showAlert(String title, String text) { stop();
alert.setTitle(title); }
alert.setString(text); } else {
alert.setType(AlertType.INFO); // cancel
showAlert(alert); Display.getDisplay(ZXingMIDlet.this).setCurrent(getCanvas());
} }
}
void showError(Throwable t) { };
String message = t.getMessage(); confirmation.setCommandListener(listener);
if (message != null && message.length() > 0) { showAlert(confirmation);
showError(message); }
} else {
showError(t.toString()); private void showAlert(String title, String text) {
} alert.setTitle(title);
} alert.setString(text);
alert.setType(AlertType.INFO);
void showError(String message) { showAlert(alert);
alert.setTitle("Error"); }
alert.setString(message);
alert.setType(AlertType.ERROR); void showError(Throwable t) {
showAlert(alert); String message = t.getMessage();
} if (message != null && message.length() > 0) {
showError(message);
private void showAlert(Alert alert) { } else {
Display display = Display.getDisplay(this); showError(t.toString());
display.setCurrent(alert, canvas); }
} }
void handleDecodedText(Result theResult) { void showError(String message) {
ParsedResult result = ResultParser.parseResult(theResult); alert.setTitle("Error");
ParsedResultType type = result.getType(); alert.setString(message);
if (type.equals(ParsedResultType.URI)) { alert.setType(AlertType.ERROR);
String uri = ((URIParsedResult) result).getURI(); showAlert(alert);
showOpenURL("Open Web Page?", uri, uri); }
} else if (type.equals(ParsedResultType.EMAIL_ADDRESS)) {
EmailAddressParsedResult emailResult = (EmailAddressParsedResult) result; private void showAlert(Alert alert) {
showOpenURL("Compose E-mail?", emailResult.getEmailAddress(), emailResult.getMailtoURI()); Display display = Display.getDisplay(this);
} else if (type.equals(ParsedResultType.SMS)) { display.setCurrent(alert, canvas);
SMSParsedResult smsResult = (SMSParsedResult) result; }
showOpenURL("Compose SMS?", smsResult.getNumber(), smsResult.getSMSURI());
} else if (type.equals(ParsedResultType.PRODUCT)) { void barcodeAction(ParsedResult result) {
ProductParsedResult productResult = (ProductParsedResult) result; ParsedResultType type = result.getType();
String uri = "http://www.upcdatabase.com/item.asp?upc=" + productResult.getNormalizedProductID(); if (type.equals(ParsedResultType.URI)) {
showOpenURL("Look Up Barcode Online?", productResult.getProductID(), uri); String uri = ((URIParsedResult) result).getURI();
} else if (type.equals(ParsedResultType.TEL)) { showOpenURL("Open Web Page?", uri, uri);
TelParsedResult telResult = (TelParsedResult) result; } else if (type.equals(ParsedResultType.EMAIL_ADDRESS)) {
showOpenURL("Dial Number?", telResult.getNumber(), telResult.getTelURI()); EmailAddressParsedResult emailResult = (EmailAddressParsedResult) result;
} else { showOpenURL("Compose E-mail?", emailResult.getEmailAddress(), emailResult.getMailtoURI());
showAlert("Barcode Detected", result.getDisplayResult()); } 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());
}
}
void itemRequest() {
ParsedResult result = (ParsedResult) resultHistory.elementAt(history.getSelectedIndex());
barcodeAction(result);
}
void handleDecodedText(Result theResult) {
ParsedResult result = ResultParser.parseResult(theResult);
String resultString = result.toString();
int i = 0;
while (i < resultHistory.size()) {
if (resultString.equals(resultHistory.elementAt(i).toString())) {
break;
}
i++;
}
if (i == resultHistory.size()) {
resultHistory.addElement(result);
history.append(result.getDisplayResult(), null);
}
barcodeAction(result);
}
}