如何将GRAL图表导出为JPG格式?

3

我正在尝试使用以下方法将示例GRAL饼图导出为jpg:

private byte[] getJpg() throws IOException {
    BufferedImage bImage = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = bImage.createGraphics();
    DrawingContext drawingContext = new DrawingContext(g2d, DrawingContext.Quality.QUALITY,
            DrawingContext.Target.BITMAP);
    PiePlot plot = getPlot();
    plot.draw(drawingContext);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(bImage, "jpg", baos);
    baos.flush();
    byte[] bytes = baos.toByteArray();
    baos.close();
    return bytes;
}

但是它渲染成一个黑色矩形,并带有一些图例信息(图例没问题)。谁知道从GRAL绘图中正确地渲染JPG的方法?
1个回答

7
当然,我找到了一个内置的解决方案,DrawableWriter。现在导出的结果如下:
private byte[] getJpg() throws IOException {
        BufferedImage bImage = new BufferedImage(800, 600, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = (Graphics2D) bImage.getGraphics();
        DrawingContext context = new DrawingContext(g2d);
        PiePlot plot = getPlot();
        plot.draw(context);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DrawableWriter wr = DrawableWriterFactory.getInstance().get("image/jpeg");
        wr.write(plot, baos, 800, 600);
        baos.flush();
        byte[] bytes = baos.toByteArray();
        baos.close();
        return bytes;
    }

感谢开发者!一切都已完成。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接