📄 capture.java
字号:
/*
* Movino J2ME Client
* Copyright (C) 2007 Johannes Berg
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.movino;
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.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;
import org.movino.connection.ImagePacket;
import org.movino.connection.MovinoConnection;
public class Capture extends Returnable implements CommandListener, Runnable{
public static final String PROTOCOL="capture";
private static long startTime=System.currentTimeMillis();
private Player videoInputPlayer;
private VideoControl videoControl;
private Command exitCommand;
private Command captureCommand;
private Thread runningThread;
private VideoDisplay videoDisplay;
private boolean isStopped;
private boolean manualCapture=false;
public Capture(){
}
public void show(Display display){
isStopped = false;
videoDisplay = new VideoDisplay();
exitCommand = new Command("Stop", Command.EXIT, 1);
captureCommand = new Command("Capture", Command.SCREEN, 1);
if(!Options.optionAutoCapture) videoDisplay.addCommand(captureCommand);
videoDisplay.addCommand(exitCommand);
videoDisplay.setCommandListener(this);
display.setCurrent(videoDisplay);
runningThread = new Thread(this);
runningThread.start();
startCamera();
}
public void reShow(Display display) {
display.setCurrent(videoDisplay);
}
public void startCamera(){
try {
videoInputPlayer = Manager.createPlayer(PROTOCOL+"://"+Options.optionCamera.getDeviceName());
if(videoInputPlayer==null){
alert("Camera not found");
return;
}
videoInputPlayer.setLoopCount(-1);
videoInputPlayer.realize();
videoInputPlayer.start();
videoControl = (VideoControl)videoInputPlayer.getControl("VideoControl");
videoDisplay.setVideoControl(videoControl);
} catch (IOException e) {
alert("Could not start camera: "+e.getMessage());
return;
} catch (MediaException e) {
alert("Could not start camera: "+e.getMessage());
return;
}
}
public void stopCamera(){
try { videoInputPlayer.stop();
} catch (MediaException e) {}
videoInputPlayer.close();
}
public void commandAction(Command cmd, Displayable arg1) {
if(cmd==exitCommand){
isStopped = true;
//runningThread.interrupt();
Thread.yield(); //give time to stop the running thread(loosely)
stopCamera();
returnBack();
}
else if(cmd==captureCommand){
manualCapture=true;
}
}
public void run() {
while(!isStopped){
if(Options.optionAutoCapture || manualCapture) doCapture();
if(videoDisplay!=null) videoDisplay.repaint();
try {
if(Options.optionAutoCapture){
int delay = Options.optionCaptureDelay;
switch(delay){
case 0:
MovinoConnection mc = MovinoConnection.currentConnection;
if(mc!=null){
while(mc.hasPendingPackets()) Thread.sleep(20);
}
else{
Thread.sleep(20);
}
break;
case 1:
Thread.sleep(5);
break;
case 2:
Thread.sleep(200);
break;
case 3:
Thread.sleep(1000);
break;
default:
Thread.sleep(20);
break;
}
}
else{
Thread.sleep(20); //wait for manual capture
}
} catch (InterruptedException e){}
}
}
public void doCapture(){
manualCapture=false;
try {
if(videoControl!=null){
byte[] img = videoControl.getSnapshot(Options.optionSnapshotFormat.getFormatString());
if(img!=null){
MovinoConnection mc = MovinoConnection.currentConnection;
if(mc!=null){
mc.sendPacket(new ImagePacket((int)(System.currentTimeMillis()-startTime),ImagePacket.IMAGE_TYPE_JPEG,img));
}
}
else{
alert("Could not capture image");
}
}
} catch (MediaException e) {
alert("could not capture image "+e.getMessage());
} catch(SecurityException e){
//ignore...
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -