【问题标题】:Creating composite image from two source png in Groovy在 Groovy 中从两个源 png 创建合成图像
【发布时间】:2011-09-21 18:14:55
【问题描述】:

我见过一些 Groovy 代码,可以让您组合图像和文本,但不能组合图像和图像......

基本上,我需要在地图上的某些坐标处覆盖​​符号:我有一些地图和符号作为 .png 文件。我可以毫无问题地进行坐标计算,所以问题更多的是,给定两个透明的 png,我如何在不丢失透明度的情况下组合它们? (地图和符号可能都需要保持其透明度)。

理想情况下我需要一个函数,比如

combinePngImage(background_src, foreground_src, foreground_x, foreground_y)

这将返回两者的 png 组合,给定前景图像的左上角和右上角坐标。

[背景:我有一些地图和符号作为 .png 文件存储在 FileMaker 的容器字段中,我需要在 FileMaker 解决方案中组合它们。我曾尝试使用 ImageMagick 和命令行来完成它,但突然让我感到震惊的是,这可能是可以使用 ScriptMaster 完成的,它使用 Groovy 创建外部函数。 ]

感谢您的指点!

【问题讨论】:

    标签: image-processing groovy png transparency filemaker


    【解决方案1】:

    好吧,不管它的价值 - 这里有一些拼凑而成的食谱 sn-ps 似乎可以完成这项工作 - 任何(糟糕的)编码风格的 cmet 都非常受欢迎。

    给定一个 ScriptMaster 调用

    combineImage ( backgroundImg ; foregroundImg ; x ; y )
    

    需要的代码是:

    import javax.imageio.ImageIO;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.net.URL;
    
    // get container data
    InputStream bgImgContainer
    
    try{
      bgImgContainer = fmpro.getContainerStream(backgroundImg)
    }catch(e){
      throw new Exception('Could not get data from background image container (make sure container field name is passed as text)')
    }
    
    // test if container field is empty
    if( bgImgContainer == null ) {
      throw new Exception('Background image container field is empty')
    }
    
    bgImgName = fmpro.getContainerFileName(backgroundImg);
    
    InputStream fgImgContainer
    
    try{                                         
      fgImgContainer = fmpro.getContainerStream(foregroundImg)
    }catch(e){
      throw new Exception('Could not get data from foreground image container (make sure container field name is passed as text)')
    }
    
    // test if container field is empty
    if( fgImgContainer == null ) {
      throw new Exception('Foreground image container field is empty')
    }
    
    fgImgName = fmpro.getContainerFileName(foregroundImg);
    
    int xCoord = Integer.parseInt(x);
    int yCoord = Integer.parseInt(y);
    
    // load image from container data
    BufferedImage result = ImageIO.read(bgImgContainer);
    BufferedImage overlay = ImageIO.read(fgImgContainer);
    
    int fgWidth  = overlay.getWidth(null);
    int fgHeight = overlay.getHeight(null);
    
    Graphics2D graphics = result.createGraphics();
    
    // overlay the foreground at given coordinates and actual size
    graphics.drawImage(overlay, xCoord, yCoord, fgWidth, fgHeight, null);
    
    graphics.dispose();
    
    File output = File.createTempFile(bgImgName, ".png");
    ImageIO.write(result, "png", output);
    return output;
    

    【讨论】:

      【解决方案2】:

      您可以尝试使用 Im4java,一个纯 Java ImageMagick API,而不是使用 ImageMagick 命令行。

      【讨论】:

      • 将对此进行调查....但代码需要在 FileMaker 中运行的插件中运行。如果我打算这样做,我希望有一个 Groovy 纯代码路由,而不是依赖外部调用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-11
      • 2011-09-17
      • 1970-01-01
      • 1970-01-01
      • 2019-10-20
      • 2011-08-30
      相关资源
      最近更新 更多