ho bisogno di stampare un 1800 x 1200 pixel, 300 immagine dpi su 4" x 6" carta (noto anche come 4r)Stampa di un'immagine di 1800 x 1200 4 x 6 carta utilizzando Java
Quello che ho provato
ho creato un PrintRequestAttributeSet
che si prende cura della mia PrintableArea
(4 x 6), Printer print DPI
, Orientation
. Ho allegato un MCVE in basso.
Problema
Mentre il codice funziona, e ho un PageFormat
con i seguenti attributi (per la mia stampante):
x= 12.0
y= 12.32
w= 276.0
h= 419.67
La larghezza e l'altezza sono poco meno, perché la mia stampante doesn non supportare Zero Margin
. (Questo è quello che ho preso in considerazione. Se qualcuno è a conoscenza di un modo diverso da questo attraverso il quale posso forzare margine pari a zero, per favore fatemelo sapere)
sto fornendo il margin as 0
, perché queste immagini verranno stampate tramite stampanti che supportano margine zero (stampanti Photobooth).
aset.add(new MediaPrintableArea(0, 0, 4, 6, MediaPrintableArea.INCH));
L'area di stampa incluso il margine è di circa 4 x 6 secondo necessità. Il problema si verifica quando scala l'immagine per stampare all'interno dell'area stampabile.
Poiché l'immagine è 1800 x 1200, supporta un rapporto aspetto di 3: 2, che significa che l'immagine viene creata per essere stampata su una carta 4 x 6 (dopo essere stata ruotata e ridimensionata). For Reference.
Ora, poiché la larghezza della pagina e il peso di pagina dello PageFormat
non sono esattamente divisibili da ImageWidth e ImageHeight. Sto riscontrando problemi di ridimensionamento.
Nota: Ho ruotare l'immagine perché deve essere stampato su 4 x 6 e non 6 x 4.
L'immagine che dovrebbe prendere 4 x 6 spazio sta prendendo da qualche parte vicino a 4 x 5. Anche la dimensione dell'immagine viene ridotta drasticamente.
Come si supera questo problema?
Codice
Si prega di trovare il MCVE qui:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
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 java.io.IOException;
import javax.imageio.ImageIO;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.OrientationRequested;
import javax.print.attribute.standard.PrintQuality;
import javax.print.attribute.standard.PrinterResolution;
public class ImgPrinter implements Printable {
Image img;
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
throws PrinterException {
Graphics2D g2d = (Graphics2D) graphics;
g2d.translate((int) (pageFormat.getImageableX()),
(int) (pageFormat.getImageableY()));
if (pageIndex == 0) {
double pageWidth = pageFormat.getImageableWidth();
double pageHeight = pageFormat.getImageableHeight();
/**
* Swapping width and height, coz the image is later rotated
*/
double imageWidth = img.getHeight(null);
double imageHeight = img.getWidth(null);
double scaleX = pageWidth/imageWidth;
double scaleY = pageHeight/imageHeight;
g2d.scale(scaleX, scaleY);
g2d.rotate(Math.toRadians(90), img.getWidth(null)/2,
img.getHeight(null)/2);
g2d.drawImage(img, 0, 0, null);
return Printable.PAGE_EXISTS;
}
return Printable.NO_SUCH_PAGE;
}
public void printPage(String file, String size) {
try {
Image img = ImageIO.read(new File(file));
this.img = img;
PrintRequestAttributeSet aset = createAsetForMedia(size);
PrinterJob pj = PrinterJob.getPrinterJob();
PageFormat pageFormat = pj.getPageFormat(aset);
pj.setPrintable(this, pageFormat);
pj.print();
} catch (PrinterException ex) {
ex.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private PrintRequestAttributeSet createAsetForMedia(String size) {
PrintRequestAttributeSet aset = null;
try {
aset = new HashPrintRequestAttributeSet();
aset.add(PrintQuality.NORMAL);
aset.add(OrientationRequested.PORTRAIT);
/**
* Suggesting the print DPI as 300
*/
aset.add(new PrinterResolution(300, 300, PrinterResolution.DPI));
/**
* Setting the printable area and the margin as 0
*/
if (size.equals("3r")) {
aset.add(new MediaPrintableArea(0, 0, 3, 5,
MediaPrintableArea.INCH));
} else if (size.equals("4r")) {
aset.add(new MediaPrintableArea(0, 0, 4, 6,
MediaPrintableArea.INCH));
} else if (size.equals("5r")) {
aset.add(new MediaPrintableArea(0, 0, 5, 7,
MediaPrintableArea.INCH));
} else if (size.equals("6r")) {
aset.add(new MediaPrintableArea(0, 0, 6, 8,
MediaPrintableArea.INCH));
}
} catch (Exception e) {
e.printStackTrace();
}
return aset;
}
public static void main(String[] args) {
new ImgPrinter().printPage("/Some_URL/sam.jpg",
"4r");
}
}
Per eseguire il programma, è sufficiente fornire un percorso di immagine 1800x1200 al programma principale e sarà stampare sulla stampante predefinita.
carico JPG a BufferedImage, per scalare la risoluzione desiderata, ImageIO.write, poi per stampare direttamente una nuova immagine – mKorbel
nota dato che l'utilizzo di scaledInstance è piuttosto asincrono, ho provato a ridimensionare l'immagine convertendola in BufferImage. – mKorbel
Ho passato la nuova larghezza come '4 * 72 = 288' e altezza come' 6 * 72 = 432' (dato che tutto è 72dpi in java). Ma l'immagine viene distorta. Stampa un'immagine di 3,7 x 4 e utilizza un'area stampabile di 4 x 5,2. Anche la qualità di stampa è stata drasticamente ridotta. [Il collegamento al codice modificato] (http://pastie.org/9758244). Non sono sicuro, se sto utilizzando il processo corretto per convertire e tornare a BufferedImage in Immagine. – ItachiUchiha