⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 menu.java

📁 手机上使用j2me控制相机的例子。需要手机支持jp7
💻 JAVA
字号:
/*
 * Menu.java
 *
 * Created on 2006年9月5日, 上午12:43
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package MyCamera;

import javax.microedition.amms.control.camera.CameraControl;
import javax.microedition.amms.control.camera.ExposureControl;
import javax.microedition.amms.control.camera.FlashControl;
import javax.microedition.amms.control.camera.FocusControl;
import javax.microedition.amms.control.camera.SnapshotControl;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;

/**
 *
 * @author tp
 */
public class Menu
{
    public static final int EXPOSURE_MENU = 1;
    public static final int FLASH_MENU = 2;
    Player player;
    Canvas canvas;
    public int selection;
    /** Creates a new instance of Menu */
    public Menu(Canvas c, Player p)
    {
	canvas = c;
	player = p;
	selection = 0;
    }

    public void showMenu(int num)
    {
	if(num == 3)
	{
	    selection = EXPOSURE_MENU;
	}
	else if(num == 6)
	{
	    ExposureControl ec = (ExposureControl)player.getControl("ExposureControl");
	    String clm = ec.getLightMetering();
	    String[] lm = ec.getSupportedLightMeterings();
	    if(lm[0].compareTo(clm) == 0)   ec.setLightMetering(lm[1]);
	    else    ec.setLightMetering(lm[0]);
	}
	else if(num == 9)
	{
	    selection = FLASH_MENU;
	}
	else if(num == 2)
	{
	    FocusControl foc = (FocusControl)player.getControl("FocusControl");
	    try
	    {
		if(foc.getMacro() == true)	foc.setMacro(false);
		else    foc.setMacro(true);
	    }
	    catch(Exception e)
	    {
		e.printStackTrace();
	    }
	}
    }

    void action(int num)
    {
	if(selection == EXPOSURE_MENU)
	{
	    CameraControl cc = (CameraControl)player.getControl("CameraControl");
	    String[] type = cc.getSupportedExposureModes();
	    if(num < type.length)   cc.setExposureMode(type[num]);
	}
	else if(selection == FLASH_MENU)
	{
	    FlashControl fc = (FlashControl)player.getControl("FlashControl");
	    int[] mfc = fc.getSupportedModes();
	    if(num < mfc.length)    fc.setMode(num+1);
	}
	selection = 0;
    }

    void showMenu(Graphics g)
    {
	if(selection == EXPOSURE_MENU)
	{
	    CameraControl cc = (CameraControl)player.getControl("CameraControl");
	    String[] type = cc.getSupportedExposureModes();
	    int lineHeight = g.getFont().getHeight();
	    for (int i = 0; i < type.length; i++)
	    {
		g.setColor(200,200,200);
		g.fillRect(0, lineHeight*i, canvas.getWidth(), lineHeight);
		g.setColor(0,0,0);
		g.drawString(String.valueOf(i+1) + ":" + type[i], 0, lineHeight*i, 0);
	    }
	}
	else if(selection == FLASH_MENU)
	{
	    FlashControl fc = (FlashControl)player.getControl("FlashControl");
	    int[] type = fc.getSupportedModes();
	    int lineHeight = g.getFont().getHeight();
	    for (int i = 0; i < type.length; i++)
	    {
		g.setColor(200,200,200);
		g.fillRect(0, lineHeight*i, canvas.getWidth(), lineHeight);
		g.setColor(0,0,0);
		g.drawString(String.valueOf(i+1) + ":" + String.valueOf(type[i]), 0, lineHeight*i, 0);
	    }
	}
    }

    void upEC()
    {
	ExposureControl ec = (ExposureControl)player.getControl("ExposureControl");
	int cec = ec.getExposureCompensation();
	int temp = cec % 100;
	if(temp == 30 || temp == -70)	cec += 40;
	else	cec += 30;
	if(cec > 200)	cec = 200;
	try
	{
	    ec.setExposureCompensation(cec);
	}
	catch(Exception e)
	{
	    e.printStackTrace();
	}
    }

    void downEC()
    {
	ExposureControl ec = (ExposureControl)player.getControl("ExposureControl");
	int cec = ec.getExposureCompensation();
	int temp = cec % 100;
	if(temp == 70 || temp == -30)	cec-= 40;
	else	cec -= 30;
	if(cec < -300)	cec = -300;
	try
	{
	    ec.setExposureCompensation(cec);
	}
	catch(Exception e)
	{
	    e.printStackTrace();
	}
    }

    void downISO()
    {
	ExposureControl ec = (ExposureControl)player.getControl("ExposureControl");
	int iso = ec.getISO();
	if(iso == 100)	iso = 0;
	else	iso /= 2;
	try
	{
	    ec.setISO(iso);
	} catch (MediaException ex)
	{
	    ex.printStackTrace();
	}
    }

    void upISO()
    {
	ExposureControl ec = (ExposureControl)player.getControl("ExposureControl");
	int iso = ec.getISO();
	if(iso == 0)	iso = 100;
	else if(iso != 1600)	iso *= 2;
	try
	{
	    ec.setISO(iso);
	} catch (MediaException ex)
	{
	    ex.printStackTrace();
	}
    }

    void focus()
    {
	FocusControl foc = (FocusControl)player.getControl("FocusControl");
	try
	{
		foc.setFocus(foc.AUTO);
		foc.setFocus(foc.AUTO_LOCK);
	} catch (MediaException ex)
	{
		ex.printStackTrace();
	}
    }
    
    void snapshot()
    {
	FocusControl foc = (FocusControl)player.getControl("FocusControl");
	SnapshotControl ssc = (SnapshotControl)player.getControl("SnapshotControl");
	try
	{
		foc.setFocus(foc.AUTO_LOCK);
		ssc.start(9);
	} catch (MediaException ex)
	{
		ex.printStackTrace();
	}
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -