groovy import org.xhtmlrenderer.simple.Graphics2DRenderer我如何在groovy中添加它?

时间:2017-12-24 04:03:19

标签: java groovy

IM尝试在groovy 2.4中执行此代码,但似乎该类不存在

我不知道如何在groovy中安装它

标题组织和org.w3c不存在

请帮我解决这个问题

这是代码

import java.awt.Dimension
import java.awt.Graphics2D
import java.awt.RenderingHints
import java.awt.image.BufferedImage
import javax.imageio.ImageIO
import org.w3c.dom.Document
import org.w3c.tidy.Tidy
import org.xhtmlrenderer.simple.Graphics2DRenderer

def makeThumbnail(address) {
  // Size for the renderer
    def WIDTH = 1280
    def HEIGHT = 800

    // Setup Tidy
    def tidy = new Tidy()
    tidy.with {
        setQuiet(true)
        setXHTML(true)
        setHideComments(true)
        setInputEncoding("UTF-8")
        setOutputEncoding("UTF-8")
        setShowErrors(0)
        setShowWarnings(false)
    }

    def url = new URL(address)
    def doc = tidy.parseDOM(new ByteArrayInputStream(url.text.getBytes("UTF-8")), null)
    def os = new FileOutputStream("/Users/ascotti/${url.getHost()}.png")

    def buf = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB)

    def graphics = buf.createGraphics()
    def renderer = new Graphics2DRenderer()

    renderer.with {
        setDocument(doc, address)
        layout(graphics, new Dimension(WIDTH, HEIGHT))
        render(graphics)
        graphics.dispose()
        ImageIO.write(buf, "png", os)
    }
}

makeThumbnail("http://www.128bitstudios.com/")
makeThumbnail("http://en.wikipedia.org/")
makeThumbnail("http://misplaced-packets.net/")
makeThumbnail("http://stackoverflow.com/")

我如何安装

import org.w3c.dom.Document
import org.w3c.tidy.Tidy
import org.xhtmlrenderer.simple.Graphics2DRenderer

我安装

 grape install "org.w3c" "dom" "2.3.0-jaxb-1.0.6"
  grape install "org.xhtmlrenderer" "core-renderer" "R8pre2"
  grape install "net.sf.jtidy" "jtidy" "r938"

但在grooyv中出现相同的错误 在groovy 请帮帮我

1 个答案:

答案 0 :(得分:0)

在脚本的开头使用@Grab groovy注释。

像这样你的脚本运行正常。

结果很奇怪 - 但这是另一个问题)

@Grab(group='org.xhtmlrenderer', module='core-renderer', version='R8')
@Grab(group='net.sf.jtidy', module='jtidy', version='r938')

import java.awt.Dimension
import java.awt.Graphics2D
import java.awt.RenderingHints
import java.awt.image.BufferedImage
import javax.imageio.ImageIO
import org.w3c.dom.Document
import org.w3c.tidy.Tidy
import org.xhtmlrenderer.simple.Graphics2DRenderer

def makeThumbnail(address) {
  // Size for the renderer
    def WIDTH = 1280
    def HEIGHT = 800

    // Setup Tidy
    def tidy = new Tidy()
    tidy.with {
        setQuiet(true)
        setXHTML(true)
        setHideComments(true)
        setInputEncoding("UTF-8")
        setOutputEncoding("UTF-8")
        setShowErrors(0)
        setShowWarnings(false)
    }

    def url = new URL(address)
    def doc = tidy.parseDOM(new ByteArrayInputStream(url.text.getBytes("UTF-8")), null)
    def os = new FileOutputStream("${System.env.USERPROFILE}/${url.getHost()}.png")

    def buf = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB)

    def graphics = buf.createGraphics()
    def renderer = new Graphics2DRenderer()

    renderer.with {
        setDocument(doc, address)
        layout(graphics, new Dimension(WIDTH, HEIGHT))
        render(graphics)
        graphics.dispose()
        ImageIO.write(buf, "png", os)
    }
}

makeThumbnail("http://en.wikipedia.org/")