J2ME 手机拍照程序

package com.zeph.j2me;

import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class CameraControlMIDlet extends MIDlet implements CommandListener {

	private Display display;
	private CameraCanvas cameraCanvas;
	private Form picForm;
	private Command captureCommand;
	private Command exitCommand;
	private Command backCommand;
	private Player player;
	private VideoControl videoControl;

	public CameraControlMIDlet() {
		// TODO Auto-generated constructor stub
		display = Display.getDisplay(this);
		try {
			player = Manager.createPlayer("capture://video");
			player.realize();
			videoControl = (VideoControl) player.getControl("VideoControl");
			cameraCanvas = new CameraCanvas(player, videoControl);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MediaException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		picForm = new Form("图像显示窗口");
		captureCommand = new Command("拍照", Command.OK, 0);
		exitCommand = new Command("退出", Command.EXIT, 0);
		backCommand = new Command("返回摄像头", Command.OK, 0);
		cameraCanvas.addCommand(captureCommand);
		cameraCanvas.addCommand(exitCommand);
		picForm.addCommand(backCommand);
		picForm.setCommandListener(this);
		cameraCanvas.setCommandListener(this);
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	protected void startApp() throws MIDletStateChangeException {
		// TODO Auto-generated method stub
		display.setCurrent(cameraCanvas);
	}

	public void commandAction(Command command, Displayable displayable) {
		// TODO Auto-generated method stub
		if (command == captureCommand) {
			new Snapshot().start();//拍照时需要使用线程启动
		} else if (command == exitCommand) {
			try {
				destroyApp(true);
			} catch (MIDletStateChangeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			notifyDestroyed();
		} else if (command == backCommand) {
			display.setCurrent(cameraCanvas);
		}
	}

	class Snapshot extends Thread {//使用内部类可以很方便的使用VideoControl和Player
		public Image getSnapshot() {
			Image image = null;
			if (player != null) {
				byte[] imageData;
				try {
					imageData = videoControl.getSnapshot(null);
					image = Image.createImage(imageData, 0, imageData.length);
				} catch (MediaException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return image;
		}

		public void run() {
			Image image = getSnapshot();
			picForm.append(image);
			display.setCurrent(picForm);
		}
	}

}

 

package com.zeph.j2me;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;

public class CameraCanvas extends Canvas {

	public CameraCanvas(Player player, VideoControl videoControl) {
		try {
			videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this);
			videoControl.setDisplayLocation(0, 0);
			videoControl.setDisplaySize(getWidth(), getHeight());
			player.start();
			videoControl.setVisible(true);
		} catch (MediaException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected void paint(Graphics g) {
		// TODO Auto-generated method stub
	}

}


 在测试S403rd的系统时,需要把

player = Manager.createPlayer("capture://video");

更改为

player = Manager.createPlayer("capture://image");


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值