我决定在android中创建自己的Java的Robot类来进行屏幕捕获..我已经编写了我自己的机器人类的源代码,但问题在这里,代码中的以下行是抛出编译错误..saying“ ComponentFactory类型中的方法createRobot(Robot,GraphicsDevice)不适用于参数(Robot,GraphicsDevice)”
peer =((ComponentFactory)toolkit).createRobot(this,screen);
任何人都可以建议我解决方案是什么......
感谢..
答案 0 :(得分:0)
这是我的完整源代码
package com.example.awt;
import java.awt.AWTException; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.Rectangle; import java.awt.peer。; import java.awt.image。; import java.awt.event。*; import java.lang.reflect.InvocationTargetException;
// import com.sun.media.sound.Toolkit;
import sun.awt.ComponentFactory; // import sun.awt.SunToolkit; import sun.security.util.SecurityConstants; import java.awt.Toolkit;
公共类机器人{
private static final int MAX_DELAY = 60000;
private RobotPeer peer;
private boolean isAutoWaitForIdle = false;
private int autoDelay = 0;
private static final int LEGAL_BUTTON_MASK =
InputEvent.BUTTON1_MASK|
InputEvent.BUTTON2_MASK|
InputEvent.BUTTON3_MASK;
private DirectColorModel screenCapCM = null;
public Robot() throws AWTException {
if (GraphicsEnvironment.isHeadless()) {
throw new AWTException ("headless environment");
}
init(GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice());
}
private void init(GraphicsDevice screen) throws AWTException {
checkRobotAllowed();
Toolkit toolkit = Toolkit.getDefaultToolkit();
if (toolkit instanceof ComponentFactory) {
peer = ((ComponentFactory)toolkit).createRobot(this, screen);
}
}
public Robot(GraphicsDevice screen) throws AWTException {
checkIsScreenDevice(screen);
init(screen);
}
private void checkRobotAllowed() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPermission(SecurityConstants.CREATE_ROBOT_PERMISSION);
}
}
private void checkIsScreenDevice(GraphicsDevice device) {
if (device == null || device.getType() != GraphicsDevice.TYPE_RASTER_SCREEN) {
throw new IllegalArgumentException ("not a valid screen device");
}
}
public synchronized BufferedImage createScreenCapture(Rectangle screenRect) {
checkScreenCaptureAllowed();
checkValidRect(screenRect);
BufferedImage image;
DataBufferInt buffer;
WritableRaster raster;
if (screenCapCM == null) {
/*
284 * Fix for 4285201
285 * Create a DirectColorModel equivalent to the default RGB ColorModel,
286 * except with no Alpha component.
*/
screenCapCM = new DirectColorModel(24,
/* red mask */ 0x00FF0000,
/* green mask */ 0x0000FF00,
/* blue mask */ 0x000000FF);
}
int pixels[];
int[] bandmasks = new int[3];
pixels = peer.getRGBPixels(screenRect);
buffer = new DataBufferInt(pixels, pixels.length);
bandmasks[0] = screenCapCM.getRedMask();
bandmasks[1] = screenCapCM.getGreenMask();
bandmasks[2] = screenCapCM.getBlueMask();
raster = Raster.createPackedRaster(buffer, screenRect.width, screenRect.height, screenRect.width, bandmasks, null);
image = new BufferedImage(screenCapCM, raster, false, null);
return image;
}
private static void checkValidRect(Rectangle rect) {
if (rect.width <= 0 || rect.height <= 0) {
throw new IllegalArgumentException ("Rectangle width and height must be > 0");
}
}
private static void checkScreenCaptureAllowed() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPermission(
SecurityConstants.READ_DISPLAY_PIXELS_PERMISSION);
}
}
}