Show CameraCanvas.java syntax highlighted
/*
* Copyright (c) 2005
* Helsinki Institute of Physics
* see LICENSE file for details
*
* CameraCanvas.java
* Created on Nov 11, 2003
*/
package fi.hip.gb.client;
import java.io.IOException;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;
import fi.hip.gb.bluetooth.util.Properties;
/**
* Takes pictures with MMAPI supported cameras.
* When picture is taken, it is added to the
* pictures form.
*
* @author Juho Karppinen
*/
public class CameraCanvas extends Canvas implements CommandListener {
/** the player object */
private Player mPlayer;
/** the video controller */
private VideoControl mVideoControl;
/**
* Creates new camera canvas with live picture on the screen.
*/
public CameraCanvas() {
addCommand(new Command("Capture", Command.SCREEN, 1));
addCommand(new Command("Back", Command.BACK, 1));
setCommandListener(this);
String supports = System.getProperty("supports.video.capture");
if (supports != null && supports.equals("true")) {
showCamera();
}
}
public void commandAction(Command c, Displayable disp) {
if ((c.getCommandType() == Command.BACK)) {
MIDui.back(true);
} else {
capture();
}
}
public void keyPressed(int keyCode) {
int action = getGameAction(keyCode);
if (action == FIRE)
capture();
}
public void paint(Graphics g) {
int width = getWidth();
int height = getHeight();
if(mPlayer == null) {
g.setColor( 255, 255, 255 );
g.fillRect( 0, 0, this.getWidth(), this.getHeight() );
g.setColor( 0, 0, 0 );
g.drawString( "Cannot use this device to take pictures.",
0,
0,
Graphics.BASELINE | Graphics.LEFT );
}
// Draw a green border around the VideoControl.
g.setColor(0x00ff00);
g.drawRect(0, 0, width - 1, height - 1);
g.drawRect(1, 1, width - 3, height - 3);
}
private void showCamera() {
int width = getWidth();
int height = getHeight();
try {
mPlayer = Manager.createPlayer("capture://video");
mPlayer.realize();
mVideoControl = (VideoControl) mPlayer.getControl("VideoControl");
mVideoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this);
try {
mVideoControl.setDisplayLocation(2, 2);
mVideoControl.setDisplaySize(width - 4, height - 4);
} catch (MediaException me) {
try {
mVideoControl.setDisplayFullScreen(false);
} catch (MediaException me2) {
System.out.println(me2.getMessage());
}
}
mVideoControl.setVisible(true);
mPlayer.start();
} catch (IOException ioe) {
MIDui.showAlert("IO Exception", ioe.getMessage(), AlertType.ERROR);
} catch (MediaException me) {
MIDui.showAlert("Media Exception", me.getMessage(), AlertType.ERROR);
}
}
public void capture() {
try {
// get the image.
byte[] rawImage = mVideoControl.getSnapshot("encoding=jpeg&width=160&height=120");
// shut down the player.
mPlayer.close();
mPlayer = null;
mVideoControl = null;
Properties flags = new Properties();
flags.put("encoding", "jpeg");
flags.put("width", "160");
flags.put("height", "120");
/*
LiteResult image = new LiteResult("newimage.jpeg",
"",
rawImage.length,
flags,
rawImage);
MIDui.savePicture(image);
MIDui.showPicture(image);
*/
} catch (MediaException me) {
MIDui.showException("Cannot take picture", me);
}
}
}
See more files for this project here