【发布时间】:2018-07-06 01:20:59
【问题描述】:
我正在尝试用 Java 打印一个带有小旋转变换的简单图像。打印图像时,图像边界处存在原始图像中不存在的伪影,因此图像在边界处带有这些黑线。
有人见过吗?
要复制,请运行以下代码:
package test;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import javax.print.PrintService;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.standard.Destination;
public class TestPrintBug implements Printable
{
public static void main(String [] args)
{
try
{
PrintService[] services = PrinterJob.lookupPrintServices();
for (int index = 0; index < services.length; index++)
{
if (services[index].getName().equalsIgnoreCase("Microsoft XPS Document Writer"))
{
PrinterJob pjob = PrinterJob.getPrinterJob();
HashPrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(new Destination(new File("c:/test.xps").toURI()));
pjob.setPrintable(new TestPrintBug(), new PageFormat());
pjob.setPrintService(services[index]);
pjob.print(attributes);
}
}
}
catch(Throwable t)
{
t.printStackTrace();
}
}
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException
{
if (pageIndex == 0)
{
try
{
int width = (int)(8.5 * 72);
int height = 1 * 72;
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D biG = bi.createGraphics();
biG.setColor(Color.yellow);
biG.fillRect(0, 0, width, height);
Graphics2D g2Print = (Graphics2D)graphics;
g2Print.transform(AffineTransform.getRotateInstance(-0.02));
g2Print.drawImage(bi, 0, 144, null);
return Printable.PAGE_EXISTS;
}
catch(Throwable t)
{
t.printStackTrace();
}
}
return Printable.NO_SUCH_PAGE;
}
}
【问题讨论】: