?? btmidlet.java
字號:
package com.j2medev.chapter9;
import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.VideoControl;
import javax.microedition.midlet.MIDlet;
public class BTMIDlet extends MIDlet implements CommandListener{
private Display display = null;
private List menu = null;
private Form form = null;
private Player player = null;
private VideoControl vc = null;
private Thread server = null;
//存儲拍照的圖片數(shù)據(jù)
private byte[] image = null;
private Command captureCommand = new Command("capture",Command.OK,1);
private Command exitCommand = new Command("exit",Command.EXIT,1);
private Command backCommand = new Command("back",Command.BACK,1);
private Command sendCommand = new Command("send by bluetooth",Command.OK,1);
private Command serverCommand = new Command("start server",Command.OK,1);
public static final String[] MENUS = {"receiver picture","send picture"};
public void startApp() {
if(display == null){
display = Display.getDisplay(this);
menu = new List("share",List.IMPLICIT,MENUS,null);
menu.setCommandListener(this);
}
//顯示主菜單
display.setCurrent(menu);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
releasePlayer();
}
private void startPlayer(){
if(player == null){
try {
//創(chuàng)建Player
player = Manager.createPlayer("capture://video");
player.realize();
vc = (VideoControl)player.getControl("VideoControl");
if(vc != null){
//把Item追加到Form上
form.append((Item)vc.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE,null));
}
player.start();
form.addCommand(captureCommand);
} catch (IOException ex) {
ex.printStackTrace();
} catch (MediaException ex) {
ex.printStackTrace();
}
}
}
//非常重要,釋放player
private void releasePlayer(){
if(player != null){
player.close();
player = null;
}
}
public byte[] getImage(){
return image;
}
public static Alert getAlert(String msg,AlertType type,int timeout){
Alert a = new Alert("alert",msg,null,type);
a.setTimeout(timeout);
return a;
}
//服務器啟動后 回調(diào)此函數(shù)
public void btServerReady(){
form.append("device is ready and waiting other bluetooth device to send picture...");
}
//接收到圖片后回調(diào)此函數(shù)
public void imageReceived(byte[] data){
Image img = Image.createImage(data,0,data.length);
form.append(img);
}
public void commandAction(Command command, Displayable displayable) {
if(command.getCommandType() == Command.EXIT){
destroyApp(false);
notifyDestroyed();
}else if(command == List.SELECT_COMMAND){
int index = menu.getSelectedIndex();
if(form == null)
form = new Form("");
form.deleteAll();
if(index == 0){
//啟動服務器端
form.setTitle("receive picture");
BTServer bts = new BTServer(this);
if(server == null)
server = new Thread(bts);
server.start();
}else if(index == 1){
//啟動客戶端,準備拍照
form.setTitle("capture");
startPlayer();
form.addCommand(backCommand);
}
form.setCommandListener(this);
display.setCurrent(form);
}else if(command == captureCommand){
//拍照
new Thread(){
public void run(){
try {
image = vc.getSnapshot(null);
} catch (MediaException ex) {
ex.printStackTrace();
}
//清除以前的拍照屏幕
form.deleteAll();
form.append(Image.createImage(image,0,image.length));
form.addCommand(sendCommand);
form.removeCommand(captureCommand);
//不要忘記釋放Player
releasePlayer();
}
}.start();
}else if(command == sendCommand){
//經(jīng)過藍牙發(fā)送圖片
BTClient client = new BTClient(this);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -