在sikuliX上找到动态图像,用缓冲区创建问题

时间:2018-01-02 10:53:53

标签: java eclipse sikuli

我正在尝试使用此功能找到我之前在空模板中创建的图像,该模板在其上插入接收颜色,内容和字体的文本并返回生成图像的路径:

the template

public String insertTextOnBlanck(String colorLetter,String text,Font font) {
    //path is a private varibable initialized with the constructor
    File blankFile = new File("images/dinamic/"+path);
    BufferedImage image = null;
    String exit_path = null;
    try {
        image = ImageIO.read(blankFile);
        int type = image.getType() == 0? BufferedImage.TYPE_INT_ARGB : image.getType();
        Graphics2D g2 = image.createGraphics();
        FontMetrics metrics = g2.getFontMetrics(font);
        BufferedImage resizeImage = resizeImage(image,type, text,metrics);
        image.flush();
        int w = resizeImage.getWidth();
        int h = resizeImage.getHeight();
        g2 = resizeImage.createGraphics();
        g2.setColor(Color.decode(colorLetter));
        g2.setFont(font);
        // Get the FontMetrics
        int x = (w - metrics.stringWidth(text)) / 2;
        int y = (metrics.getAscent() + (h - (metrics.getAscent() + metrics.getDescent())) / 2);
        g2.setBackground(Color.decode("#d1e8f8"));
        g2.drawString(text, x, y);
        g2.dispose();
        //create image with text
        exit_path = "images/dinamic/changed_"+path;

        File file = new File(exit_path);
        ImageIO.write(resizeImage, "png", file);
        resizeImage.flush();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return exit_path;
}

这是我第一次调用其他功能

    public void  dinamicClick(String path,String input,String fontLetter,String colorLetter, int fontType,int size) throws FindFailed {
    DinamicImg DimImg = new DinamicImg();
    DimImg.setPath(path);
    String modPath = DimImg.insertTextOnBlanck(
            colorLetter,
            input,//Inventario de recurso
            new Font(fontLetter,fontType, size)
            );
    Iterator<Match> myIt = s.findAll(modPath);
    while (myIt.hasNext()) {
        Location loc = myIt.next().getTarget();
        s.click(loc);
    }
    myIt.remove();
    removeFile(modPath);
}

removeFile函数是:

private void removeFile(String toRemove) {
    File file = new File(toRemove);
    if(file.delete()){
        System.out.println(file.getName() + " is deleted!");
    }else{
        System.out.println("Delete operation is failed.");
    }
}

结果:

the result

但是接下来的调用完全不起作用,就在我更改退出路径的名称时,所以我认为是一个缓存问题,但在“insertTextOnBlanck”函数的开头添加ImageIO.setUseCache(false);仍然不起作用。我的想法请帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,使用了库org.sikuli.script.ImagePath,您需要使用ImagePath.reset()重置SikuliX的内部缓存路径。