[i][b]迅雷看看播放完成后自动关机[/b][/i]
-----------------------------------------
使用说明:
本程序用于通过像素自动检测迅雷播放器播放时间,检测范围大致在全屏时或最大化时,总之窗口底部要在开始菜单上面不超过100像素的屏幕从左算起60%左右的矩形范围内.若播放器完成,播放时间会停止,从而实现关机之目的.
实现方案实在笨拙,请各位不要见笑.
下面是程序代码:
MyFrame
ShutService
XPShutDown
PixelRobot
[color=white]注意: 此程序若不能关闭...请联系作者![/color]
-----------------------------------------
使用说明:
本程序用于通过像素自动检测迅雷播放器播放时间,检测范围大致在全屏时或最大化时,总之窗口底部要在开始菜单上面不超过100像素的屏幕从左算起60%左右的矩形范围内.若播放器完成,播放时间会停止,从而实现关机之目的.
实现方案实在笨拙,请各位不要见笑.
下面是程序代码:
MyFrame
/**
*
*/
package tl.bisoft.java.apps;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Label;
/**
* @author cs
*
*/
public class MyFrame extends Frame {
private static final long serialVersionUID = 1L;
public static final Label LABEL = new Label();
public MyFrame(String name) {
super(name);
}
/**
* @param args
*/
public static void main(String[] args) {
MyFrame f = new MyFrame("");
f.setBackground(new Color(0xffffff01));
f.setForeground(Color.RED);
f.setAlwaysOnTop(true);
f.setUndecorated(true);
f.setBounds(PixelRobot.maxWidth - 100, PixelRobot.maxHeight - 20, 100, 20);
LABEL.setText("关机检测中...");
f.add(LABEL);
f.setVisible(true);
f.startApp(args);
}
public void startApp(String[] args) {
ShutService service = new ShutService();
if (args.length > 0) {
service.exec(args[0]);
} else {
service.exec("-s");
}
}
}
ShutService
package tl.bisoft.java.apps;
/**
* 关机服务
* @author cs
*/
public class ShutService {
/** 关机 */
private static final String SHUTDOWN = "-s";
/** 重启 */
private static final String RESTART = "-r";
/** 注销 */
private static final String LOGOUT = "-l";
/** 取消关机 */
private static final String CANCEL = "-a";
public void exec(String cmd) {
if (null != cmd) {
if (cmd.equals(SHUTDOWN)) {
PixelRobot.shutdown();
} else if (cmd.equals(RESTART)) {
XPShutDown sd = new XPShutDown();
sd.restart();
} else if (cmd.equals(LOGOUT)) {
XPShutDown sd = new XPShutDown();
sd.logout();
} else if (cmd.equals(CANCEL)) {
XPShutDown sd = new XPShutDown();
sd.cancel();
}
}
}
}
XPShutDown
package tl.bisoft.java.apps;
import java.io.IOException;
/**
* XP 系统关机API
* @author cs
*
*/
public class XPShutDown {
private static XPShutDown instance = new XPShutDown();
private static final String SHUTDOWN = "shutdown -s -f -t 60";
private static final String RESTART = "shutdown -r -f -t 0";
private static final String LOGOUT = "shutdown -l -f -t 0";
private static final String CANCEL = "shutdown -a";
public XPShutDown() {
}
public static XPShutDown getInstance() {
return instance;
}
public void exec(String cmd) {
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
System.out.println("执行失败!");
}
}
public void shutdown() {
exec(SHUTDOWN);
}
public void restart() {
exec(RESTART);
}
public void logout() {
exec(LOGOUT);
}
public void cancel() {
exec(CANCEL);
}
}
PixelRobot
package tl.bisoft.java.apps;
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
/**
* 像素处理机器人
* @author cs
*
*/
public class PixelRobot extends Robot implements Runnable {
private static final int STOP = 0;
private static final int RUN = 1;
private static final long DELAY = 5000L;
public static final int maxWidth = Toolkit.getDefaultToolkit()
.getScreenSize().width;
public static final int maxHeight = Toolkit.getDefaultToolkit()
.getScreenSize().height;
private static PixelRobot instance = null;
public Color[][] color1 = new Color[maxWidth][maxHeight];
public Color[][] color2 = new Color[maxWidth][maxHeight];
/** 线程状态 */
private int status = 0;
private PixelRobot() throws AWTException {
super();
}
public static PixelRobot getPixelRobot() {
if (null == instance) {
try {
instance = new PixelRobot();
} catch (AWTException e) {
e.printStackTrace();
}
}
return instance;
}
public static void shutdown() {
PixelRobot pr = PixelRobot.getPixelRobot();
pr.start();
}
/**
* 矩形检测屏幕区域,若约300S内像素RGB值相同时,执行关机操作.
*
* @return 像素RGB值是否相同
*/
public boolean test() {
moveMouse();
fetchScreenColor(color1, 0, 1000 * maxHeight/1080, 1500 * maxWidth / 1920,
maxHeight);
delay(DELAY);
fetchScreenColor(color2, 0, 1000 * maxHeight/1080, 1500 * maxWidth / 1920,
maxHeight);
return same(color1, color2, 0, 1000 * maxHeight/1080, 1500 * maxWidth / 1920,
maxHeight);
}
private void moveMouse() {
this.mouseMove(10, maxHeight - 5);
}
private void caputureScreen(int startX, int startY, int endX, int endY)
{
Rectangle r = null;
if (null == r)
{
r = new Rectangle(startX, startY, endX, endY);
}
this.createScreenCapture(r);
}
private void fetchScreenColor(Color[][] color, int startX, int startY,
int endX, int endY) {
caputureScreen(startX, startY, endX, endY);
int x = startX;
int y = startY;
for (x = startX; x < endX; x++) {
for (y = startY; y < endY; y++) {
color[x][y] = this.getPixelColor(x, y);
}
}
}
private boolean same(Color[][] color1, Color[][] color2, int startX,
int startY, int endX, int endY) {
boolean isSame = true;
int k = startX;
int j = startY;
outter: for (k = startX; k < endX; k++) {
for (j = startY; j < endY; j++) {
if (!color1[k][j].equals(color2[k][j])) {
isSame = false;
break outter;
}
}
}
return isSame;
}
private void delay(long delay) {
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void start() {
setStatus(RUN);
new Thread(this).start();
}
public void run() {
int rgb = 0;
while (getStatus() == RUN) {
if (PixelRobot.getPixelRobot().test()) {
rgb++;
/** 20s * 15 = 60s */
if (rgb >= 15) {
setStatus(STOP);
XPShutDown.getInstance().shutdown();
}
} else {
rgb--;
if (rgb < 0) {
rgb = 0;
}
}
if (rgb >= 12)
{
MyFrame.LABEL.setText("关机倒计时: " + (300 - rgb * 20) + "s.");
}
else
{
MyFrame.LABEL.setText("关机检测中...");
}
}
}
public void setStatus(int status) {
this.status = status;
}
public int getStatus() {
return status;
}
}
[color=white]注意: 此程序若不能关闭...请联系作者![/color]