TestCameraControl.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 javax.microedition.lcdui.*;
import javax.microedition.media.MediaException;

public class TestCameraControl extends TestControl
        implements ItemStateListener {

    public TestCameraControl(CameraMIDlet cm) {
        super(cm);
        imgw = 120;
        imgh = 160;
        super.optionsForm.setTitle("Camera Control Options");
        createCameraForm(super.midlet.cameracontrol);
        if (isSupported()) {
            CreateOptionsForm();
        }
    }

    private void CreateOptionsForm() {
        shutterCG = new ChoiceGroup("Shutter Feedback", 1);
        exposureCG = new ChoiceGroup("Exposure: ", 1);
        stillResCG = new ChoiceGroup("Still Resolution", 1);
        videoResCG = new ChoiceGroup("Video Resolution", 1);
        shutterCG.append("On", null);
        shutterCG.append("Off", null);
        if (super.cameraControl.isShutterFeedbackEnabled()) {
            shutterCG.setSelectedIndex(0, true);
            shutterCG.setSelectedIndex(1, false);
        }
        else {
            shutterCG.setSelectedIndex(1, true);
            shutterCG.setSelectedIndex(0, false);
        }
        String expModes[] = super.cameraControl.getSupportedExposureModes();
        if (expModes.length == 0) {
            exposureCG.append("Not supported", null);
            exposureCG.setFont(0, Font.getFont(0, 0, 8));
        }
        else {
            for (int i = 0; i < expModes.length; i++) {
                exposureCG.append(expModes[i], null);
                exposureCG.setFont(i, Font.getFont(0, 0, 8));
            }

        }
        int stillRes[] = super.cameraControl.getSupportedStillResolutions();
        if (stillRes.length == 0) {
            stillResCG.append("Not supported", null);
            stillResCG.setFont(0, Font.getFont(0, 0, 8));
        }
        else {
            for (int i = 0; i < stillRes.length; i += 2) {
                stillResCG.append(String.valueOf(stillRes[i]) + "x" + String.valueOf(stillRes[i + 1]), null);
            }

        }
        int vidRes[] = super.cameraControl.getSupportedVideoResolutions();
        if (vidRes.length == 0) {
            videoResCG.append("Not supported", null);
            videoResCG.setFont(0, Font.getFont(0, 0, 8));
        }
        else {
            for (int i = 0; i < vidRes.length; i += 2) {
                videoResCG.append(String.valueOf(vidRes[i]) + "x" + String.valueOf(vidRes[i + 1]), null);
            }

        }
        for (int i = 0; i < stillResCG.size(); i++) {
            stillResCG.setFont(i, Font.getFont(0, 0, 8));
        }

        for (int i = 0; i < videoResCG.size(); i++) {
            videoResCG.setFont(i, Font.getFont(0, 0, 8));
        }

        for (int i = 0; i < shutterCG.size(); i++) {
            shutterCG.setFont(i, Font.getFont(0, 0, 8));
        }

        super.optionsForm.setItemStateListener(this);
        super.optionsForm.append(shutterCG);
        super.optionsForm.append(exposureCG);
        super.optionsForm.append(stillResCG);
        super.optionsForm.append(videoResCG);
        super.optionsForm.addCommand(super.cmdBack);
        super.optionsForm.setCommandListener(this);
    }

    public void SaveOptions() {
        try {
            super.cameraControl.enableShutterFeedback(shutterCG.getSelectedIndex() == 0);
            super.cameraControl.setExposureMode(exposureCG.getString(exposureCG.getSelectedIndex()));
            if (!stillResCG.getString(stillResCG.getSelectedIndex()).equals("Not supported")) {
                super.cameraControl.setStillResolution(stillResCG.getSelectedIndex());
            }
            if (!videoResCG.getString(videoResCG.getSelectedIndex()).equals("Not supported")) {
                super.cameraControl.setVideoResolution(videoResCG.getSelectedIndex());
            }
        }
        catch (MediaException e) {
            super.midlet.showAlert("Media Exception", "Unable to save settings.", null);
            CameraMIDlet.getDisplay().setCurrent(super.canvas);
        }
    }

    public boolean captureImage() {
        try {
            super.imageData = super.videoControl.getSnapshot("encoding=jpeg&width=" + imgw + "&height=" + imgh);
            super.capturedImage = Image.createImage(super.imageData, 0, super.imageData.length);
        }
        catch (MediaException exc) {
            super.midlet.showAlert("Error", exc.getMessage(), super.canvas);
            return false;
        }
        catch (SecurityException secExc) {
            super.midlet.showAlert("Security error", secExc.getMessage(), super.canvas);
            return false;
        }
        catch (NullPointerException exc) {
            super.midlet.showAlert("Null Pointer Error", "Null Pointer - videoControl not working ... ", super.canvas);
            return false;
        }
        catch (Exception e) {
            super.midlet.showAlert("Error", e.getMessage(), super.canvas);
            return false;
        }
        return true;
    }

    public void itemStateChanged(Item item) {
        if (item != shutterCG) {
            if (item == exposureCG) {
                super.cameraControl.setExposureMode(super.cameraControl.getSupportedExposureModes()[exposureCG.getSelectedIndex()]);
                super.midlet.showAlert("Exposure changed", "New exposure mode: " + super.cameraControl.getExposureMode(), super.optionsForm);
            }
            else if (item == stillResCG) {
                if (!stillResCG.getString(stillResCG.getSelectedIndex()).equals("Not supported")) {
                    super.cameraControl.setStillResolution(stillResCG.getSelectedIndex() / 2);
                    String resStr = stillResCG.getString(stillResCG.getSelectedIndex());
                    imgh = Integer.parseInt(resStr.substring(0, resStr.indexOf("x")));
                    imgw = Integer.parseInt(resStr.substring(resStr.indexOf("x") + 1));
                    super.midlet.showAlert("Still Resolution changed", "New resolution: " + imgh + "x" + imgw, super.optionsForm);
                }
            }
            else if (item == videoResCG && !videoResCG.getString(videoResCG.getSelectedIndex()).equals("Not supported")) {
                super.cameraControl.setVideoResolution(videoResCG.getSelectedIndex());
            }
        }
    }
    ChoiceGroup exposureCG;
    ChoiceGroup stillResCG;
    ChoiceGroup videoResCG;
    ChoiceGroup shutterCG;
    int imgw;
    int imgh;
}