gpt4 book ai didi

image - JFreeChart & iText : black image when creating pdf

转载 作者:行者123 更新时间:2023-12-04 05:41:12 25 4
gpt4 key购买 nike

我使用 JFreeChart 1.0.9 创建图表,并使用 iText 1.3.1 在 PDF 文件中显示此图表。
为此,我创建了一个 JFreeChart,然后将其转换为字节数组:

final TimeSeriesCollection dataset = new TimeSeriesCollection();
final TimeSeries s1 = new TimeSeries("Series 1", Minute.class);
s1.add(new Minute(0, 0, 7, 12, 2003), 1.2);
s1.add(new Minute(30, 12, 7, 12, 2003), 3.0); // 12h30 => value = 3
s1.add(new Minute(15, 14, 7, 12, 2003), 8.0);

final TimeSeries s2 = new TimeSeries("Series 2", Minute.class);
s2.add(new Minute(0, 3, 7, 12, 2003), 0.0);
s2.add(new Minute(30, 9, 7, 12, 2003), 0.0);
s2.add(new Minute(15, 10, 7, 12, 2003), 0.0);

dataset.addSeries(s1);
dataset.addSeries(s2);
XYDataset dataset2 = dataset;

final JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Sample Chart",
"Date",
"Value",
dataset2,
true,
true,
false
);
chart.setBackgroundPaint(Color.white);
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());

BufferedImage originalImage = chart.createBufferedImage(500, 300, info);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( originalImage, "jpg", baos );
byte[] imageInByte = baos.toByteArray();

然后我简单地使用 iText 将这个字节数组放入 PDF 文件中,如下所示:
Document document=new Document();
PdfWriter.getInstance(document,new FileOutputStream("hello.pdf"));
document.open();

Image image = Image.getInstance(imageInByte);
document.add(image);

document.close();

但是当我打开我的 PDF 文件时,我的图像应该是全黑的区域。

你知道可能是什么原因吗?

最佳答案

看起来图像在转换过程中被反转了。试试下面的代码

package demo;

import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.data.time.Minute;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfWriter;


public class PDF {

public static void main(String[] args) {
final TimeSeriesCollection dataset = new TimeSeriesCollection();
final TimeSeries s1 = new TimeSeries("Series 1", Minute.class);
s1.add(new Minute(0, 0, 7, 12, 2003), 1.2);
s1.add(new Minute(30, 12, 7, 12, 2003), 3.0); // 12h30 => value = 3
s1.add(new Minute(15, 14, 7, 12, 2003), 8.0);

final TimeSeries s2 = new TimeSeries("Series 2", Minute.class);
s2.add(new Minute(0, 3, 7, 12, 2003), 0.0);
s2.add(new Minute(30, 9, 7, 12, 2003), 0.0);
s2.add(new Minute(15, 10, 7, 12, 2003), 0.0);

dataset.addSeries(s1);
dataset.addSeries(s2);
XYDataset dataset2 = dataset;

final JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Sample Chart",
"Date",
"Value",
dataset2,
true,
true,
false
);
chart.setBackgroundPaint(Color.white);
java.awt.Image originalImage = chart.createBufferedImage(500, 300);
try {
Document document=new Document();
PdfWriter.getInstance(document,new FileOutputStream("hello.pdf"));
document.open();
com.lowagie.text.Image image1 = com.lowagie.text.Image.getInstance(originalImage,Color.white);
document.add(image1);
document.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

使用最新版本的 iText 你我得到这个 PDF

enter image description here

希望你会得到同样的

我对您的代码进行了以下更改
java.awt.Image originalImage = chart.createBufferedImage(500, 300);

已删除
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());

因为它不需要。我还删除了 ByteArrayOutputStream并将其替换为
   com.lowagie.text.Image image1 = com.lowagie.text.Image.getInstance(originalImage,Color.white);

注意 com.lowagie.text.Imag 的全名使用e 和 java.awt.Image因为 AWT 和 Lowagie 都有称为 Image 的类。

关于image - JFreeChart & iText : black image when creating pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11221816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com