CameraScreen.java

/*
 * Copyright © 2011 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners.
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.mmsmidlet;

import java.io.IOException;
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;

// The CameraScreen class shows the live view
// of the camera using the MMAPI and gives
// commands to capture the contents of the camera
class CameraScreen extends Canvas implements CommandListener {
    private final MMSMIDlet midlet;
    private final Command exitCommand;
    private Player player = null;
    private Command captureCommand = null;
    private VideoControl videoControl = null;

    CameraScreen(MMSMIDlet midlet) {
        this.midlet = midlet;

        // Builds the user interface
        exitCommand = new Command("Exit", Command.EXIT, 1);
        addCommand(exitCommand);
        captureCommand = new Command("Capture", Command.SCREEN, 1);
        addCommand(captureCommand);
        setCommandListener(this);
    }

    // Paint the canvas' background in black
    public void paint(Graphics g) {
        // black background
        g.setColor(0x0000000);
        g.fillRect(0, 0, getWidth(), getHeight());
    }

    public void commandAction(Command c, Displayable d) {
        if (c == exitCommand) {
            midlet.exitApplication();
        } else if (c == captureCommand) {
            captureImage();
        }
    }

    public void keyPressed(int keyCode) {
        if (getGameAction(keyCode) == FIRE) {
            captureImage();
        }
    }

    // Creates and starts the video player
    synchronized void start() {
        try {
            // Newer phones need to be initialized to image mode
            try {
                player = Manager.createPlayer("capture://image");

                // Older phones don't support this, so we start them
                // in video mode.
            } catch (Exception ce) {
                player = Manager.createPlayer("capture://video");
            }
            player.realize();

            // Get VideoControl for the viewfinder
            videoControl = (VideoControl) player.getControl("VideoControl");
            if (videoControl == null) {
                discardPlayer();
                midlet.showError("Cannot get the video control.\n"
                        + "Capture may not be supported.");
                player = null;
            } else {
                // Set up the viewfinder on the screen.
                videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO,
                        this);
                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);
                player.start();
                videoControl.setVisible(true);
            }
        } catch (IOException ioe) {
            discardPlayer();
            midlet.showError("IOException: " + ioe.getMessage());
        } catch (MediaException me) {
            midlet.showError("MediaException: " + me.getMessage());
        } catch (SecurityException se) {
            midlet.showError("SecurityException: " + se.getMessage());
        }
    }

    // Stops the video player
    synchronized void stop() {
        if (player != null) {
            try {
                videoControl.setVisible(false);
                player.stop();
            } catch (MediaException me) {
                midlet.showError("MediaException: " + me.getMessage());
            }
        }
    }

    // this method will discard the video player
    private void discardPlayer() {
        if (player != null) {
            player.deallocate();
            player.close();
            player = null;
        }
        videoControl = null;
    }

    // captures the image from the video player
    // in a separate thread
    private void captureImage() {
        if (player != null) {
            // Capture image in a new thread.
            new Thread() {
                public void run() {
                try {   //For Devices that support jpg image encoding, mostly Symbian
                        byte[] jpgImage = videoControl.getSnapshot("encoding=image/jpg&width=270&height=360");
                        midlet.imageCaptured(jpgImage);
                        discardPlayer();
                    } catch (MediaException me) {
                        try{
                            //For Devices that do not support jpg image encoding, mostly Series 40
                            byte[] jpgImage = videoControl.getSnapshot(null);
                            midlet.imageCaptured(jpgImage);
                            discardPlayer();
                            }
                            catch(Exception e)
                            {
                                midlet.showError("Exception: "+e.getMessage());
                            }
                    } catch (SecurityException se) {
                        midlet.showError("SecurityException: "
                                + se.getMessage());
                    }
                }
            }.start();
        }
    }
}