📄 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;
//存储拍照的图片数据
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 {
//创建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;
}
//服务器启动后 回调此函数
public void btServerReady(){
form.append("device is ready and waiting other bluetooth device to send picture...");
}
//接收到图片后回调此函数
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){
//经过蓝牙发送图片
BTClient client = new BTClient(this);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -