我们在使用org.apache.batik将svg字符串渲染为图像。为了设置我们获得的图像质量,我们代理org.apache.batik.transcoder.image.PNGTranscoder
,以便在调用createRenderer
时可以为返回的渲染应用一些图像渲染设置。方法createRenderer
是抽象类org.apache.batik.transcoder.image.ImageTranscoder
中的一个受保护的方法,PNGTranscoder是扩展该类的。
完整的代码如下
(set! *warn-on-reflection* true)
(defn- high-quality-png-transcoder ^PNGTranscoder []
(proxy [PNGTranscoder] []
(createRenderer []
(let [add-hint (fn [^RenderingHints hints k v] (.add hints (RenderingHints. k v)))
^ImageRenderer renderer (proxy-super createRenderer) ;; line 104
hints (RenderingHints.
RenderingHints/KEY_ALPHA_INTERPOLATION
RenderingHints/VALUE_ALPHA_INTERPOLATION_QUALITY)]
(doto hints
(add-hint RenderingHints/KEY_ALPHA_INTERPOLATION RenderingHints/VALUE_ALPHA_INTERPOLATION_QUALITY)
(add-hint RenderingHints/KEY_INTERPOLATION RenderingHints/VALUE_INTERPOLATION_BICUBIC)
(add-hint RenderingHints/KEY_ANTIALIASING RenderingHints/VALUE_ANTIALIAS_ON)
(add-hint RenderingHints/KEY_COLOR_RENDERING RenderingHints/VALUE_COLOR_RENDER_QUALITY)
(add-hint RenderingHints/KEY_DITHERING RenderingHints/VALUE_DITHER_DISABLE)
(add-hint RenderingHints/KEY_RENDERING RenderingHints/VALUE_RENDER_QUALITY)
(add-hint RenderingHints/KEY_STROKE_CONTROL RenderingHints/VALUE_STROKE_PURE)
(add-hint RenderingHints/KEY_FRACTIONALMETRICS RenderingHints/VALUE_FRACTIONALMETRICS_ON)
(add-hint RenderingHints/KEY_TEXT_ANTIALIASING RenderingHints/VALUE_TEXT_ANTIALIAS_OFF))
(.setRenderingHints renderer hints)
renderer))))
这会产生警告
可视化警告,metabase/pulse/render/poc.clj:104:37 - 无法解决对字段createRenderer的引用。
Clojure文档给出了“使用typehint this
”作为解决方案。在proxy-super
上方添加^PNGTranscoder this this
并不能解决问题,但它表明代码有更多信息。
可视化警告,metabase/pulse/render/poc.clj:105:37 - 无法解决在org.apache.batik.transcoder.image.PNGTranscoder上对字段createRenderer的引用。
(在包含新的typehinted this绑定的105行中,104行变为105行)