Hallo,
ich möchte ein DIN A4 Bild ausdrucken. Der Ausdruck aus Java heraus ist etwas unscharf, speziell bei Schriften. Anders als ein Ausdruck auf Betriebssystem Ebene (Ubuntu).
Kann mir jemand sagen woran das liegt?
Hier der Code
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class PrintA4Test {
public static double imageableX;
public static double imageableY;
public static double imageableWidth;
public static double imageableHeight;
public static void main(String[] args) throws IOException{
BufferedImage image = ImageIO.read(new File("/home/$user/test.png"));
PrinterJob pj = PrinterJob.getPrinterJob();
if (pj.printDialog()){
PageFormat pf = pj.defaultPage();
Paper paper = pf.getPaper();
imageableX = 0;
imageableY = 0;
imageableWidth = pf.getWidth() - (2 * imageableX);
imageableHeight = pf.getHeight() - (2 * imageableY);
paper.setImageableArea(imageableX, imageableY, imageableWidth, imageableHeight);
pf.setOrientation(PageFormat.LANDSCAPE);
pf.setPaper(paper);
PageFormat validatePage = pj.validatePage(pf);
pj.setPrintable(new TestPrintable(image), validatePage);
try{
pj.print();
}catch (PrinterException ex){
ex.printStackTrace();
}
}
}
public static class TestPrintable implements Printable {
private BufferedImage image;
public TestPrintable(BufferedImage image){
this.image = image;
}
@Override
public int print(Graphics g, PageFormat pf, int page) throws PrinterException{
Graphics2D g2d = (Graphics2D) g.create();
if (page != 0){
return NO_SUCH_PAGE;
}
g2d.drawImage(image.getScaledInstance((int)imageableHeight, (int)imageableWidth, BufferedImage.SCALE_SMOOTH), (int)imageableX, (int)imageableY, null);
g2d.dispose();
return PAGE_EXISTS;
}
}
}