我正在尝试编写一个小程序,以便在java打印api上生成并打印非常具体的明信片。在打印之前,我将PrinterResolution
300,400添加到HashPrintRequestAttributeSet
,但它实际上会打印出更大,更低的dpi图片。如果我设置我添加到150x150的PrinterResolution
,它会打印出正确的大小。我不确定我做错了什么但是我制作的printerresolution
越大,打印出的图像就越大(因此实际产生的dpi就越少。
我最大的问题是尝试旋转我的第二个printableBufferedImage
。当我尝试时,由于一些奇怪的原因,打印输出(最左边的四分之一)中大约有四分之一的图像丢失了
以下是我尝试打印背面图像时的代码:
if(e.getSource() == printBack)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // Use the native L&F
}
catch (Exception cnf)
{
//do nothing
}
PrinterJob job = PrinterJob.getPrinterJob();
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new PrinterResolution(150, 150, PrinterResolution.DPI));
PageFormat pf = job.pageDialog(aset);
job.setPrintable(rotatedback(), pf);
boolean ok = job.printDialog(aset);
if (ok)
{
try
{
job.print(aset);
}
catch (PrinterException ex)
{
/* The job did not successfully complete */
}
}
这是我的printableBufferedImage类
public class printableBufferedImage extends BufferedImage implements Printable
{
protected int dotperinch;
public printableBufferedImage(ColorModel cm, WritableRaster raster, boolean isRasterPremultiplied, Hashtable properties)
{
super(cm, raster, isRasterPremultiplied, properties);
}
public printableBufferedImage(int width, int height, int imageType)
{
super(width, height, imageType);
}
public printableBufferedImage(int width, int height, int imageType, IndexColorModel cm)
{
super(width, height, imageType, cm);
}
public int print(Graphics g, PageFormat pf, int page) throws
PrinterException {
Graphics2D g2d = (Graphics2D) g;
if (page > 0) { /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}
/* User (0,0) is typically outside the imageable area, so we must
* translate by the X and Y values in the PageFormat to avoid clipping
*/
try
{
g2d.setClip(0,0,2100,3300);
g2d.drawImage(this, 225, 0,null);
g2d.drawImage(this,225, 1550,null);
}
catch(Exception exc)
{
}
/* tell the caller that this page is part of the printed document */
//return NO_SUCH_PAGE;
return PAGE_EXISTS;
}
public BufferedImage resizeOurImageFromImage(BufferedImage OurImage, int targetWidth, int targetHeight)
throws Exception
{
double tempWidth, tempHeight;
int w,h,type;
Boolean OurImageHasAlpha;
OurImageHasAlpha = OurImage.getColorModel().hasAlpha();
if(OurImageHasAlpha)
{
type = BufferedImage.TYPE_INT_ARGB;
}
else
{
type = BufferedImage.TYPE_INT_RGB;
}
w = OurImage.getWidth();
h = OurImage.getHeight();
if((targetWidth == 0) && (targetHeight != 0))
{
targetWidth = (targetHeight * w) / h;
}
else
{
if((targetHeight == 0) && (targetWidth != 0))
{
targetHeight = (targetWidth * h) / w;
}
}
if((targetHeight == 0) || (targetWidth == 0))
{
throw(new Exception("In the Resize Image module with one dimension still zero after trying proportion"));
}
do
{
if(w > targetWidth)
{
tempWidth = ((double) w)/1.2;
if (tempWidth < (double) targetWidth)
{
w = targetWidth;
}
else
{
w = (int) java.lang.Math.round(tempWidth + 0.49);
}
}
else
{
w = targetWidth;
}
if(h > targetHeight)
{
tempHeight = ((double) h)/1.2;
if (tempHeight < (double) targetHeight)
{
h = targetHeight;
}
else
{
h = (int) java.lang.Math.round(tempHeight + 0.49);
}
}
else
{
h = targetHeight;
}
BufferedImage tmp = new BufferedImage(w, h, type);
Graphics2D g2 = tmp.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2.drawImage(OurImage, 0, 0, w, h, null);
g2.dispose();
OurImage = tmp;
} while ((targetHeight != h) || (targetWidth != w));
return OurImage;
}
}