Search in sources :

Example 1 with CN

use of com.codename1.ui.CN in project CodenameOne by codenameone.

the class GameCanvasImplementation method capturePhoto.

public void capturePhoto(ActionListener response) {
    captureResponse = response;
    final Form current = Display.getInstance().getCurrent();
    final Form cam = new Form();
    cam.setScrollable(false);
    cam.setTransitionInAnimator(CommonTransitions.createEmpty());
    cam.setTransitionOutAnimator(CommonTransitions.createEmpty());
    cam.setLayout(new BorderLayout());
    cam.show();
    String platform = System.getProperty("microedition.platform");
    MMAPIPlayer p = null;
    if (platform != null && platform.indexOf("Nokia") >= 0) {
        try {
            p = MMAPIPlayer.createPlayer("capture://image", null);
        } catch (Throwable e) {
        // Ignore all exceptions for image capture, continue with video capture...
        }
    }
    if (p == null) {
        try {
            p = MMAPIPlayer.createPlayer("capture://video", null);
        } catch (Exception e) {
            // The Nokia 2630 throws this if image/video capture is not supported
            throw new RuntimeException("Image/video capture not supported on this phone");
        }
    }
    final MMAPIPlayer player = p;
    MIDPVideoComponent video = new MIDPVideoComponent(player, canvas);
    video.play();
    video.setVisible(true);
    cam.addCommand(new com.codename1.ui.Command("Cancel") {

        public void actionPerformed(ActionEvent evt) {
            if (player != null) {
                player.cleanup();
            }
            captureResponse.actionPerformed(null);
            current.showBack();
        }
    });
    final ActionListener l = new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            try {
                cam.removeAll();
                VideoControl cnt = (VideoControl) player.nativePlayer.getControl("VideoControl");
                byte[] pic = cnt.getSnapshot("encoding=jpeg&width=" + current.getWidth() + "&height=" + current.getHeight());
                String imagePath = getOutputMediaFile() + ".jpg";
                OutputStream out = null;
                try {
                    if (pic != null) {
                        out = FileSystemStorage.getInstance().openOutputStream(imagePath);
                        out.write(pic);
                    }
                } catch (Throwable ex) {
                    ex.printStackTrace();
                    System.out.println("failed to store picture to " + imagePath);
                } finally {
                    try {
                        if (out != null) {
                            out.close();
                        }
                        player.cleanup();
                    } catch (Throwable ex) {
                        ex.printStackTrace();
                    }
                }
                captureResponse.actionPerformed(new ActionEvent(imagePath));
                current.showBack();
            } catch (Throwable ex) {
                ex.printStackTrace();
                System.out.println("failed to take picture");
                current.showBack();
            }
        }
    };
    cam.addGameKeyListener(Display.GAME_FIRE, l);
    Container cn = new Container(new BorderLayout()) {

        public void pointerReleased(int x, int y) {
            l.actionPerformed(null);
        }
    };
    cn.setFocusable(true);
    cn.addComponent(BorderLayout.CENTER, video);
    cam.addComponent(BorderLayout.CENTER, cn);
    cam.revalidate();
// cam.addPointerReleasedListener(l);
}
Also used : Form(com.codename1.ui.Form) ActionEvent(com.codename1.ui.events.ActionEvent) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(com.codename1.io.BufferedOutputStream) OutputStream(java.io.OutputStream) RecordStoreException(javax.microedition.rms.RecordStoreException) MediaException(javax.microedition.media.MediaException) IOException(java.io.IOException) ConnectionNotFoundException(javax.microedition.io.ConnectionNotFoundException) BorderLayout(com.codename1.ui.layouts.BorderLayout) com.codename1.ui(com.codename1.ui) ActionListener(com.codename1.ui.events.ActionListener) VideoControl(javax.microedition.media.control.VideoControl)

Aggregations

BufferedOutputStream (com.codename1.io.BufferedOutputStream)1 com.codename1.ui (com.codename1.ui)1 Form (com.codename1.ui.Form)1 ActionEvent (com.codename1.ui.events.ActionEvent)1 ActionListener (com.codename1.ui.events.ActionListener)1 BorderLayout (com.codename1.ui.layouts.BorderLayout)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 ConnectionNotFoundException (javax.microedition.io.ConnectionNotFoundException)1 MediaException (javax.microedition.media.MediaException)1 VideoControl (javax.microedition.media.control.VideoControl)1 RecordStoreException (javax.microedition.rms.RecordStoreException)1