TestAllFeatures.java

/**
 * Copyright (c) 2012 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.ammscamera;

import java.io.IOException;
import javax.microedition.amms.control.camera.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.VideoControl;

public class TestAllFeatures
        implements CommandListener {

    public TestAllFeatures(CameraMIDlet cm) {
        exposureControl = null;
        cameraControl = null;
        flashControl = null;
        focusControl = null;
        snapControl = null;
        zoomControl = null;
        midlet = cm;
        cmdCapture = new Command("Capture", 4, 0);
        cmdOptions = new Command("Options", 8, 0);
        cmdBack = new Command("Back", 2, 1);
        cmdZoomIn = new Command("Zoom In", 8, 0);
        cmdZoomOut = new Command("Zoom Out", 8, 0);
        cmdSel = new Command("Select", 4, 0);
        optionList = new List("Change: ", 3);
        cameraForm = new Form("Testing All Features");
        createCameraForm();
    }

    public void initOptions() {
        flashForm = new Form("Flash Settings");
        snapForm = new Form("Snapshot Settings");
        zoomForm = new Form("Zoom Settings");
        optionList.append("Disable Shutter Feedback", null);
        optionList.append("Enable Macro", null);
        optionList.append("Camera Mode ->", null);
        optionList.append("Resolution ->", null);
        optionList.append("Exposure Settings ->", null);
        optionList.append("Flash Mode -> ", null);
        optionList.append("Snapshot Settings ->", null);
        optionList.append("Zoom Settings ->", null);
        for (int i = 0; i < optionList.size(); i++) {
            optionList.setFont(i, Font.getFont(0, 0, 8));
        }

        optionList.addCommand(cmdBack);
        optionList.setSelectCommand(cmdSel);
        optionList.setCommandListener(this);
    }

    public void createCameraForm() {
        cameraForm.addCommand(cmdOptions);
        cameraForm.addCommand(cmdCapture);
        cameraForm.addCommand(cmdBack);
        cameraForm.addCommand(cmdZoomIn);
        cameraForm.addCommand(cmdZoomOut);
        cameraForm.setCommandListener(this);
        try {
            player = Manager.createPlayer(midlet.getPlayerType());
            player.realize();
            player.prefetch();
            videoControl = (VideoControl) player.getControl("VideoControl");
            if (videoControl == null) {
                cameraForm.append("videoControl is null");
            }
            cameraForm.append((Item) videoControl.initDisplayMode(0, null));
            videoControl.setDisplayFullScreen(true);
            cameraControl = (CameraControl) player.getControl("javax.microedition.amms.control.camera.CameraControl");
            exposureControl = (ExposureControl) player.getControl("javax.microedition.amms.control.camera.ExposureControl");
            flashControl = (FlashControl) player.getControl("javax.microedition.amms.control.camera.FlashControl");
            focusControl = (FocusControl) player.getControl("javax.microedition.amms.control.camera.FocusControl");
            snapControl = (SnapshotControl) player.getControl("javax.microedition.amms.control.camera.SnapshotControl");
            zoomControl = (ZoomControl) player.getControl("javax.microedition.amms.control.camera.ZoomControl");
            if (player.getState() == 300) {
                player.start();
            }
        }
        catch (IOException e) {
            midlet.showAlert("Error", "IO Exception\n" + e.getMessage(), cameraForm);
        }
        catch (MediaException e) {
            midlet.showAlert("Error", "Media Exception\n" + e.getMessage(), cameraForm);
        }
        catch (NullPointerException e) {
            midlet.showAlert("Error", "Null Pointer Exception: Player or VideoControl+\n" + e.getMessage(), cameraForm);
        }
    }

    public void initCameraMode() {
        cameraModeForm = new Form("Camera Mode");
        String expModes[] = cameraControl.getSupportedExposureModes();
        cameraModeCG = new ChoiceGroup("Camera Modes:", 1);
        if (expModes.length == 0) {
            cameraModeCG.append("Not supported", null);
            cameraModeCG.setFont(0, Font.getFont(0, 0, 8));
        }
        else {
            for (int i = 0; i < expModes.length; i++) {
                cameraModeCG.append(expModes[i], null);
                cameraModeCG.setFont(i, Font.getFont(0, 0, 8));
            }

        }
        cameraModeForm.append(cameraModeCG);
        cameraModeForm.addCommand(cmdSel);
        cameraModeForm.addCommand(cmdBack);
        cameraModeForm.setCommandListener(this);
    }

    public void initResOptions() {
        resForm = new Form("Still Resolutions");
        resCG = new ChoiceGroup("Choose resolution:", 1);
        int stillRes[] = cameraControl.getSupportedStillResolutions();
        if (stillRes.length == 0) {
            resCG.append("Not supported", null);
            resCG.setFont(0, Font.getFont(0, 0, 8));
        }
        else {
            for (int i = 0; i < stillRes.length; i += 2) {
                resCG.append(String.valueOf(stillRes[i]) + "x" + String.valueOf(stillRes[i + 1]), null);
            }

        }
        resForm.append(resCG);
        resForm.addCommand(cmdBack);
        resForm.addCommand(cmdSel);
        resForm.setCommandListener(this);
    }

    public void initExpsoureOptions() {
        expSettingsForm = new Form("Exposure Settings");
        expSettingsForm.addCommand(cmdBack);
        expSettingsForm.addCommand(cmdSel);
        expSettingsForm.setCommandListener(this);
    }

    public boolean captureImage() {
        try {
            imageData = videoControl.getSnapshot(null);
            capturedImage = Image.createImage(imageData, 0, imageData.length);
        }
        catch (MediaException exc) {
            midlet.showAlert("Error", exc.getMessage(), cameraForm);
            return false;
        }
        catch (SecurityException secExc) {
            midlet.showAlert("Security error", secExc.getMessage(), cameraForm);
            return false;
        }
        catch (NullPointerException exc) {
            midlet.showAlert("Null Pointer Error", "Null Pointer - videoControl not working ... ", cameraForm);
            return false;
        }
        catch (Exception e) {
            midlet.showAlert("Error", e.getMessage(), cameraForm);
            return false;
        }
        return true;
    }

    public void showCapturedImage() {
        createImageForm();
        if (capturedImage != null) {
            imageForm.append(capturedImage);
        }
        imageForm.setTitle("Captured image");
        CameraMIDlet.getDisplay().setCurrent(imageForm);
    }

    public void createImageForm() {
        imageForm = new Form("Captured image");
        cmdSave = new Command("Save", 8, 0);
        imageForm.addCommand(cmdBack);
        imageForm.addCommand(cmdSave);
        imageForm.setCommandListener(this);
    }

    public void zoomIn() {
        if (zoomControl != null) {
            if (-1001 > zoomControl.getMaxDigitalZoom()) {
                midlet.showAlert("Zoom Control", "Already zoomed in max amount", cameraForm);
            }
            else {
                zoomControl.setDigitalZoom(-1001);
            }
        }
        else {
            midlet.showAlert("Zoom Control", "Zoom Not Supported.", cameraForm);
        }
    }

    public void zoomOut() {
        if (zoomControl != null) {
            midlet.showAlert("Zoom Control", "Already zoomed out max amount", cameraForm);
        }
        else {
            midlet.showAlert("Zoom Control", "Zoom Not Supported.", cameraForm);
        }
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmdCapture) {
            if (captureImage()) {
                showCapturedImage();
            }
        }
        else if (c == cmdOptions) {
            CameraMIDlet.getDisplay().setCurrent(optionList);
        }
        else if (c == cmdZoomIn) {
            zoomIn();
        }
        else if (c == cmdZoomOut) {
            zoomOut();
        }
        else if (c == cmdSel && d == optionList) {
            if (!optionList.getString(optionList.getSelectedIndex()).equals("Disable Shutter Feedback")) {
                if (optionList.getString(optionList.getSelectedIndex()).equals("Enable Macro")) {
                    if (focusControl != null) {
                        if (focusControl.isMacroSupported()) {
                            try {
                                focusControl.setMacro(true);
                                optionList.set(optionList.getSelectedIndex(), "Disable Macro", null);
                            }
                            catch (MediaException e) {
                                midlet.showAlert("Focus Control", "Macro focus not supported.", optionList);
                            }
                        }
                        else {
                            midlet.showAlert("Focus Control", "Macro focus not supported.", optionList);
                        }
                    }
                }
                else if (optionList.getString(optionList.getSelectedIndex()).equals("Disable Macro")) {
                    if (focusControl != null) {
                        if (focusControl.isMacroSupported()) {
                            try {
                                focusControl.setMacro(false);
                                optionList.set(optionList.getSelectedIndex(), "Enable Macro", null);
                            }
                            catch (MediaException e) {
                                midlet.showAlert("Focus Control", "Macro focus not supported.", optionList);
                            }
                        }
                        else {
                            midlet.showAlert("Focus Control", "Macro focus not supported.", optionList);
                        }
                    }
                }
                else if (optionList.getString(optionList.getSelectedIndex()).equals("Camera Mode ->")) {
                    if (cameraControl != null) {
                        initCameraMode();
                        CameraMIDlet.getDisplay().setCurrent(cameraModeForm);
                    }
                    else {
                        midlet.showAlert("Camera Control", "Camera Control not supported.", optionList);
                    }
                }
                else if (optionList.getString(optionList.getSelectedIndex()).equals("Resolution ->")) {
                    if (cameraControl != null) {
                        initResOptions();
                        CameraMIDlet.getDisplay().setCurrent(resForm);
                    }
                    else {
                        midlet.showAlert("Camera Control", "Camera Control not supported.", optionList);
                    }
                }
                else if (optionList.getString(optionList.getSelectedIndex()).equals("Exposure Settings ->")) {
                    if (exposureControl != null) {
                        initExpsoureOptions();
                        CameraMIDlet.getDisplay().setCurrent(expSettingsForm);
                    }
                    else {
                        midlet.showAlert("Exposure Control", "Exposure Control not supported.", optionList);
                    }
                }
                else if (!optionList.getString(optionList.getSelectedIndex()).equals("Flash Mode -> ") && !optionList.getString(optionList.getSelectedIndex()).equals("Snapshot Settings ->")) {
                    optionList.getString(optionList.getSelectedIndex()).equals("Zoom Settings ->");
                }
            }
        }
        else if (c == cmdBack && d != cameraForm) {
            CameraMIDlet.getDisplay().setCurrent(cameraForm);
        }
        else if (c == cmdBack) {
            CameraMIDlet.getDisplay().setCurrent(midlet.openMenu);
        }
        else if (c == cmdSel) {
            if (d == cameraModeForm) {
                cameraControl.setExposureMode(cameraModeCG.getString(cameraModeCG.getSelectedIndex()));
            }
            else if (d == resForm) {
                cameraControl.setStillResolution(resCG.getSelectedIndex());
            }
        }
    }
    boolean supported;
    CameraMIDlet midlet;
    Player player;
    VideoControl videoControl;
    Command cmdCapture;
    Command cmdOptions;
    Command cmdBack;
    Command cmdSave;
    Command cmdSel;
    Command cmdZoomIn;
    Command cmdZoomOut;
    Form cameraForm;
    Form imageForm;
    Form cameraModeForm;
    Form resForm;
    Form expSettingsForm;
    Form flashForm;
    Form snapForm;
    Form zoomForm;
    ChoiceGroup cameraModeCG;
    ChoiceGroup resCG;
    ChoiceGroup flashCG;
    ChoiceGroup focusCG;
    ChoiceGroup digZoomCG;
    ChoiceGroup optZoomCG;
    List optionList;
    byte imageData[];
    Image capturedImage;
    ExposureControl exposureControl;
    CameraControl cameraControl;
    FlashControl flashControl;
    FocusControl focusControl;
    SnapshotControl snapControl;
    ZoomControl zoomControl;
}