1

我正在 J2ME 中制作应用程序,使用此应用程序用户将能够捕获图像并同时将该图像上传到 Web 服务器,但每当我在诺基亚 C 系列中使用此应用程序时,我无法捕获图像,并且无论何时通过计算机使用此应用程序都可以捕获图像但发送命令不起作用,请查看问题并解决此问题,并指导我需要做什么以使此应用程序对我有用和有用………… …….谢谢阿米特在这里

public class myMidlet extends MIDlet implements CommandListener{
private Display display;
private Form form;
private Command exit, back, capture, camera, send;
private Player player;
private VideoControl videoControl;
private Video video;

int status = 0;
byte localData[];

public myMidlet() {
display = Display.getDisplay(this);
form = new Form("My Form");
    exit = new Command("Exit", Command.EXIT, 0);
    camera = new Command("Camera", Command.SCREEN, 1);
    back = new Command("Back", Command.BACK, 2);
    capture = new Command("Capture", Command.SCREEN, 3);
    send = new Command("Send", Command.OK, 1);
    form.addCommand(camera);
    form.addCommand(exit);
    form.setCommandListener(this);
}

public void startApp() {
    display.setCurrent(form);
}

public void pauseApp() {}

public void destroyApp(boolean unconditional){
    notifyDestroyed();
}

public void commandAction(Command c, Displayable s){
    String label = c.getLabel();
    if (label.equals("Exit")){
        destroyApp(true);
    } else if (label.equals("Camera")) {
        showCamera();
    } else if (label.equals("Back"))
        display.setCurrent(form);
    else if (label.equals("Capture")) {
        video = new Video(this);
        video.start();

        form.addCommand(send);
        form.removeCommand(camera);
    }
    else if( label.equalsIgnoreCase("Send") ){
        try {
            startSendOperation();
        } catch (Exception ex) {
        }

    }

}
    public boolean uploadImage( String uri, byte[] rawImage)throws Exception
{

HttpConnection httpConnection;
OutputStream    out;

// Open connection to the script
httpConnection = (HttpConnection)Connector.open( uri );

// Setup the request as an HTTP POST and encode with form data
httpConnection.setRequestMethod( HttpConnection.POST );
httpConnection.setRequestProperty( "Content-type", "application/
x-www-form-urlencoded" );

// Encode the imagedata with Base64
String encoded = Base64.encode( rawImage ).toString();

// Build the output and encoded string
String    output    = "imgdata=" + encoded;

// Set the content length header
httpConnection.setRequestProperty("Content-Length", Integer.toString
((output.getBytes().length)));

// Open the output stream and publish data
out = httpConnection.openOutputStream();
out.write( output.getBytes() );

// Flush the buffer (might not be necessary?)
out.flush();

// Here you might want to read a response from the POST to make
// sure everything went OK.

// Close everything down
if( out != null )
if( httpConnection != null )
httpConnection.close();

// All good
return true;
}

public void startSendOperation() throws Exception{

    boolean res = uploadImage( "http://www.xxx.com/postFolder?", localData);

}



public void showCamera(){
    try{
        player = Manager.createPlayer("capture://video");
        player.realize();
        videoControl = (VideoControl)player.getControl("VideoControl");
        Canvas canvas = new VideoCanvas(this, videoControl);
        canvas.addCommand(back);
        canvas.addCommand(capture);
        canvas.setCommandListener(this);
        display.setCurrent(canvas);
        player.start();
    } catch (IOException ioe) {} catch (MediaException me) {}
}

    class Video extends Thread {
    myMidlet midlet;
    public Video(myMidlet midlet) {
        this.midlet = midlet;
    }

    public void run() {
        captureVideo();
    }


    public void captureVideo() {
        try {
            byte[] photo = videoControl.getSnapshot(null);
            localData = photo;
            Image image = Image.createImage(photo, 0, photo.length);

            form.append(image);
            display.setCurrent(form);
            player.close();
            player = null;
            videoControl = null;
        } catch (MediaException me) { }
    }
};
}

class VideoCanvas extends Canvas {
private myMidlet midlet;

public VideoCanvas(myMidlet midlet, VideoControl videoControl) {
    int width = getWidth();
    int height = getHeight();
    this.midlet = midlet;

    videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this);
    try {
        videoControl.setDisplayLocation(2, 2);
        videoControl.setDisplaySize(width - 4, height - 4);
    } catch (MediaException me) {}
    videoControl.setVisible(true);
}

public void paint(Graphics g) {
    int width = getWidth();
    int height = getHeight();

    g.setColor(255, 0, 0);
    g.drawRect(0, 0, width - 1, height - 1);
    g.drawRect(1, 1, width - 3, height - 3);
}



}
4

1 回答 1

0

在 ShowCamera 方法中,而不是

Manager.createPlayer("capture://video");

尝试使用

Manager.createPlayer("capture://image");
于 2012-11-24T10:35:13.513 回答