使用j2me将捕获的图像保存到SD卡中

时间:2011-04-22 04:48:40

标签: java java-me

在我的应用程序中,我想捕获图像并将其保存到SD卡。 我可以启动相机并捕捉图像,但我不知道如何将该图像输入到SD卡中。

谢谢。

2 个答案:

答案 0 :(得分:2)

假设您已经拥有了图像的InputStream,您可能需要查看FileConnection API:

http://developers.sun.com/mobility/apis/articles/fileconnection/

根据您的设备和其他变量,您可能需要特殊权限才能执行此操作。

我认为您应该更具体一点:您的目标是什么设备以及您使用的Java ME(CLDC,MIDP)版本或规格是什么?

答案 1 :(得分:1)

import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

import javax.microedition.media.*;
import javax.microedition.media.control.VideoControl;

public class TakePhotoMidlet extends MIDlet
        implements CommandListener {

    private Display display;
    private Form form;
    private Command cmdBack, cmdCapture, cmdCamera;
    private Player player;
    private VideoControl videoControl;
    private Video video;

    public TakePhotoMidlet() {
        form = new Form("Take photo");
        cmdCamera = new Command("Camera", Command.SCREEN, 1);
        form.addCommand(cmdCamera);
        form.setCommandListener(this);
    }

    public void startApp() {
        display = Display.getDisplay(this);
        display.setCurrent(form);
    }

    public void commandAction(Command c, Displayable s) {

        String label = c.getLabel();

        if (label.equals("Camera")) {
            showCamera();
        } else if (label.equals("Back")) {
            display.setCurrent(form);
        } else if (label.equals("Capture")) {
            video = new Video(this);
            video.start();
        }
    }









    public void showCamera() {
        try {
            player = Manager.createPlayer("capture://video");
            player.realize();
            videoControl = (VideoControl) player.getControl("VideoControl");

            Canvas canvas = new VideoCanvas(this, videoControl);

            cmdBack = new Command("Back", Command.BACK, 2);
            cmdCapture = new Command("Capture", Command.SCREEN, 3);
            canvas.addCommand(cmdBack);
            canvas.addCommand(cmdCapture);
            canvas.setCommandListener(this);
            display.setCurrent(canvas);
            player.start();
        } catch (IOException ioe) {
        } catch (MediaException me) {
        }
    }

    public void pauseApp() {
    }

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

    class Video extends Thread {

        TakePhotoMidlet midlet;

        public Video(TakePhotoMidlet midlet) {
            this.midlet = midlet;
        }

        public void run() {
            takePhoto();
        }

        public void takePhoto() {
            try {
                byte[] photo = videoControl.getSnapshot(null);
                saveImage2File(photo);
                Image image = Image.createImage(photo, 0, photo.length);
                form.append(image);
                display.setCurrent(form);
                player.close();
            } catch (MediaException me) {
            }
        }
    };






    void saveImage2File(byte[] photo) {
        // Receive a photo as byte array
        // Save Image to file
        FileConnection fileConn = null;
        DataOutputStream dos = null;

        try {
            fileConn = (FileConnection) Connector.open(
                    "file:///root1/story123.png", Connector.READ_WRITE);
            if (!fileConn.exists()) {
                fileConn.create();
            }
            dos = new DataOutputStream(fileConn.openOutputStream());
            dos.write(photo);
            dos.flush();
            dos.close();
            fileConn.close();

        } catch (IOException ioe) {
            System.out.println("Error!" + ioe);
        }
    }
} // end of VideoCaptureMidlet

class VideoCanvas extends Canvas {

    private TakePhotoMidlet midlet;

    public VideoCanvas(TakePhotoMidlet midlet, VideoControl videoControl) {

        int width = getWidth()/2;
        int height = getHeight()/2;
        this.midlet = midlet;

        videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this);

        try {
            videoControl.setDisplayLocation(2, 2);
            videoControl.setDisplaySize(width , height );

        } catch (MediaException me) {
        }
        videoControl.setVisible(true);
    }

    public void paint(Graphics g) {

        int width = getWidth()/2;
        int height = getHeight()/2;

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