Através das classes abaixo estou tentando capturar uma imagem da câmera e salva-la no aparelho.
DisplayCanvas.java
import java.io.IOException;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.*;
class DisplayCanvas
extends Canvas
implements CommandListener
{
private final CameraMIDlet midlet;
private Image image = null;
DisplayCanvas(CameraMIDlet midlet)
{
this.midlet = midlet;
addCommand(new Command("Back", Command.BACK, 1));
setCommandListener(this);
}
public void paint(Graphics g)
{
g.setColor(0x0000FFFF); // cyan
g.fillRect(0, 0, getWidth(), getHeight());
if (image != null)
{
g.drawImage(image,
getWidth()/2,
getHeight()/2,
Graphics.VCENTER | Graphics.HCENTER);
g.setColor(0x00000000);
}
}
void setImage(byte[] pngImage) throws IOException
{
image = Image.createImage(pngImage, 0, pngImage.length);
FileConnection fcon = (FileConnection)Connector.open("file:///store/home/user/pictures/image01.png", Connector.READ_WRITE);
if(!fcon.exists()){
fcon.create();
}
OutputStream out = fcon.openOutputStream();
image = Image.createImage(pngImage, 0, pngImage.length);
if(image == null)
{
System.out.println(" image null");
}
else
{
out.write(pngImage);
out.flush();
out.close();
}
fcon.close();
}
public void commandAction(Command c, Displayable d)
{
midlet.displayCanvasBack();
}
}
CameraCanvas.java
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import java.io.IOException;
class CameraCanvas extends Canvas
implements CommandListener
{
private final CameraMIDlet midlet;
private final Command exitCommand;
private Command captureCommand = null;
private Player player = null;
private VideoControl videoControl = null;
private boolean active = false;
// Two strings for displaying error messages
private String message1 = null;
private String message2 = null;
CameraCanvas(CameraMIDlet midlet)
{
this.midlet = midlet;
exitCommand = new Command("Exit", Command.EXIT, 1);
addCommand(exitCommand);
setCommandListener(this);
try
{
player = Manager.createPlayer("capture://video");
player.realize();
player.prefetch();
// Grab the video control and set it to the current display.
videoControl = (VideoControl)(player.getControl("VideoControl"));
if (videoControl == null)
{
discardPlayer();
message1 = "Unsupported:";
message2 = "Can't get video control";
}
else
{
videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this);
// centre video, letting it be clipped if it's too big
int canvasWidth = getWidth();
int canvasHeight = getHeight();
int displayWidth = videoControl.getDisplayWidth();
int displayHeight = videoControl.getDisplayHeight();
int x = (canvasWidth - displayWidth) / 2;
int y = (canvasHeight - displayHeight) / 2;
videoControl.setDisplayLocation(x, y);
captureCommand = new Command("Capture", Command.SCREEN, 1);
addCommand(captureCommand);
}
}
catch (IOException ioe)
{
discardPlayer();
message1 = "IOException:";
message2 = ioe.getMessage();
}
catch (MediaException me)
{
discardPlayer();
message1 = "MediaException:";
message2 = me.getMessage();
}
catch (SecurityException se)
{
discardPlayer();
message1 = "SecurityException";
message2 = se.getMessage();
}
}
// Called in case of exception to make sure invalid players are closed
private void discardPlayer()
{
if (player != null)
{
player.close();
player = null;
}
videoControl = null;
}
public void paint(Graphics g)
{
g.setColor(0x00FFFF00); // yellow
g.fillRect(0, 0, getWidth(), getHeight());
if (message1 != null)
{
g.setColor(0x00000000); // black
g.drawString(message1, 1, 1, Graphics.TOP | Graphics.LEFT);
//g.drawString(path, 1, 1, Graphics.TOP | Graphics.LEFT);
g.drawString(message2, 1, 1 + g.getFont().getHeight(),
Graphics.TOP | Graphics.LEFT);
}
}
synchronized void start()
{
if ((player != null) && !active)
{
try
{
player.start();
videoControl.setVisible(true);
}
catch (MediaException me)
{
message1 = "Media exception:";
message2 = me.getMessage();
}
catch (SecurityException se)
{
message1 = "SecurityException";
message2 = se.getMessage();
}
active = true;
}
}
synchronized void stop()
{
if ((player != null) && active)
{
try
{
videoControl.setVisible(false);
player.stop();
}
catch (MediaException me)
{
message1 = "MediaException:";
message2 = me.getMessage();
}
active = false;
}
}
public void commandAction(Command c, Displayable d)
{
if (c == exitCommand)
{
midlet.cameraCanvasExit();
}
else if (c == captureCommand)
{
Thread t = new Thread(){
public void run(){
takeSnapshot();
}
};
t.start();
}
}
public void keyPressed(int keyCode)
{
if (keyCode == 120)
midlet.exitMIDlet();
if (keyCode == 115){
Thread t = new Thread(){
public void run(){
takeSnapshot();
}
};
t.start();
}
}
private void takeSnapshot()
{
if (player != null)
{
try
{
byte[] pngImage = videoControl.getSnapshot(null);
try {
midlet.cameraCanvasCaptured(pngImage);
} catch (IOException ex) {
ex.printStackTrace();
}
}
catch (MediaException me)
{
message1 = "MediaException:";
message2 = me.getMessage();
}
}
}
}
Quando testo no Emulador BlackBerry 8350, está funcionando perfeito.
Quando rodo no aparelho, nada acontece.
Alguém poderia me dizer o que está errado ou o que está faltando?
Valews.