2010-06-16 143 views
3

我编写了一个程序,该程序生成一个BufferedImage以显示在屏幕上,然后打印出来。部分图像包含1像素宽的网格线。也就是说,这条线是1个像素,线之间有大约10个像素。由于屏幕分辨率的原因,图像显示得比这更大,每行有几个像素。我想绘制它更小,但是当我缩放图像(通过使用Image.getScaledInstance或Graphics2D.scale)时,我失去了大量的细节。java设置图像的分辨率和打印尺寸

我想打印图像,并处理相同的问题。在这种情况下,我使用此代码来设置分辨率:

HashPrintRequestAttributeSet set = new HashPrintRequestAttributeSet(); 
PrinterResolution pr = new PrinterResolution(250, 250, ResolutionSyntax.DPI); 
set.add(pr); 
job.print(set); 

它可以使图像变小而不会丢失细节。但问题是,图像在相同的边界被切断,就好像我没有设置分辨率一样。我也很困惑,因为我希望更多的DPI能够制作出更小的图像,但是它正在以另一种方式发挥作用。

我在eclipse上使用Windows 7上的Java 1.6。

回答

0

这听起来像你的问题是,你正在使网格线BufferedImage的一部分,并且它缩放时看起来不好。为什么不使用drawLine()在绘制图像后生成网格?

+0

我不确定你的建议。也许我应该缩放图像,然后画线?我不想缩放图像或线条,而我的屏幕和打印机都有大量的DPI来显示所有细节。我提到了线条,但也有很多很小的(10像素方块)图标,如果我缩放1/2,它们就会变得模糊不清。这对我来说没有意义,这很不容易(大概很简单,我只是不知道该怎么做)。 – Ingrid 2010-06-17 01:02:31

+0

Printable提供了一个Graphics2D对象:'public int print(Graphics g,PageFormat pf,int page)'。不要将您的页面渲染为一个巨大的像素化图像,而应将其渲染为矢量图形调用:例如,多个'g.drawLine()'和'g.drawImage()'调用。 – Justin 2010-06-17 11:43:52

+0

我会试试看看它的外观。如果我理解正确,但这只会让不那么细致的显示效果更好。有没有办法显示和打印所有的细节? – Ingrid 2010-06-17 18:38:34

-1

您可以使用以下任一方式来提高缩放质量。我相信BiCubic会有更好的效果,但比BILINEAR慢。

g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); 
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 

我也不会使用Image.getScaledInstance(),因为它非常慢。由于我在类似的问题上挣扎,我不确定印刷。

2

关于在页面边界上被截断的图像,您是否检查过图形的剪辑区域?我的意思是尝试:

System.out.println(graphics.getClipBounds()); 

并确保它被正确设置。

0

使用Java转换具有尺寸的图像的代码并打印转换后的图像。

类别: ConvertImageWithDimensionsAndPrint.java

package com.test.convert; 

import java.awt.Graphics2D; 
import java.awt.image.BufferedImage; 
import java.io.File; 

import javax.imageio.ImageIO; 

public class ConvertImageWithDimensionsAndPrint { 
    private static final int IMAGE_WIDTH = 800; 
    private static final int IMAGE_HEIGHT = 1000; 

public static void main(String[] args) { 
    try { 
     String sourceDir = "C:/Images/04-Request-Headers_1.png"; 
     File sourceFile = new File(sourceDir); 
     String destinationDir = "C:/Images/ConvertedImages/";//Converted images save here 
     File destinationFile = new File(destinationDir); 
     if (!destinationFile.exists()) { 
      destinationFile.mkdir(); 
     } 
     if (sourceFile.exists()) { 
      String fileName = sourceFile.getName().replace(".png", ""); 
      BufferedImage bufferedImage = ImageIO.read(sourceFile); 
      int type = bufferedImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : bufferedImage.getType(); 

      BufferedImage resizedImage = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, type); 
      Graphics2D graphics2d = resizedImage.createGraphics(); 
      graphics2d.drawImage(bufferedImage, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, null);//resize goes here 
      graphics2d.dispose(); 

      ImageIO.write(resizedImage, "png", new File(destinationDir + fileName +".png")); 

      int oldImageWidth = bufferedImage.getWidth(); 
      int oldImageHeight = bufferedImage.getHeight(); 
      System.out.println(sourceFile.getName() +" OldFile with Dimensions: "+ oldImageWidth +"x"+ oldImageHeight); 
      System.out.println(sourceFile.getName() +" ConvertedFile converted with Dimensions: "+ IMAGE_WIDTH +"x"+ IMAGE_HEIGHT); 

      //Print the image file 
      PrintActionListener printActionListener = new PrintActionListener(resizedImage); 
      printActionListener.run(); 
     } else { 
      System.err.println(destinationFile.getName() +" File not exists"); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
} 

PrintActionListener.java的Reference

package com.test.convert; 

import java.awt.Graphics; 
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; 

public class PrintActionListener implements Runnable { 

private BufferedImage image; 

public PrintActionListener(BufferedImage image) { 
    this.image = image; 
} 

@Override 
public void run() { 
    PrinterJob printJob = PrinterJob.getPrinterJob(); 
    printJob.setPrintable(new ImagePrintable(printJob, image)); 

    if (printJob.printDialog()) { 
     try { 
      printJob.print(); 
     } catch (PrinterException prt) { 
      prt.printStackTrace(); 
     } 
    } 
} 

public class ImagePrintable implements Printable { 

    private double x, y, width; 

    private int orientation; 

    private BufferedImage image; 

    public ImagePrintable(PrinterJob printJob, BufferedImage image) { 
     PageFormat pageFormat = printJob.defaultPage(); 
     this.x = pageFormat.getImageableX(); 
     this.y = pageFormat.getImageableY(); 
     this.width = pageFormat.getImageableWidth(); 
     this.orientation = pageFormat.getOrientation(); 
     this.image = image; 
    } 

    @Override 
    public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException { 
     if (pageIndex == 0) { 
      int pWidth = 0; 
      int pHeight = 0; 
      if (orientation == PageFormat.PORTRAIT) { 
       pWidth = (int) Math.min(width, (double) image.getWidth()); 
       pHeight = pWidth * image.getHeight()/image.getWidth(); 
      } else { 
       pHeight = (int) Math.min(width, (double) image.getHeight()); 
       pWidth = pHeight * image.getWidth()/image.getHeight(); 
      } 
      g.drawImage(image, (int) x, (int) y, pWidth, pHeight, null); 
      return PAGE_EXISTS; 
     } else { 
      return NO_SUCH_PAGE; 
     } 
    } 

} 

}

输出:

04-Request-Headers_1.png OldFile with Dimensions: 1224x1584 
04-Request-Headers_1.png ConvertedFile converted with Dimensions: 800x1000 

一个图像的转换后打印窗口将开放用于打印转换后的图像。窗口显示如下,从选择打印机名称下拉框并点击确定按钮。

Print Window

0

我有同样的问题。这是我的解决方案。

首先更改打印作业的分辨率...

PrinterJob job = PrinterJob.getPrinterJob(); 
    // Create the paper size of our preference 
    double cmPx300 = 300.0/2.54; 
    Paper paper = new Paper(); 
    paper.setSize(21.3 * cmPx300, 29.7 * cmPx300); 
    paper.setImageableArea(0, 0, 21.3 * cmPx300, 29.7 * cmPx300); 
    PageFormat format = new PageFormat(); 
    format.setPaper(paper); 
    // Assign a new print renderer and the paper size of our choice ! 
    job.setPrintable(new PrintReport(), format); 

    if (job.printDialog()) { 
     try { 
      HashPrintRequestAttributeSet set = new HashPrintRequestAttributeSet(); 
      PrinterResolution pr = new PrinterResolution((int) (dpi), (int) (dpi), ResolutionSyntax.DPI); 
      set.add(pr); 
      job.setJobName("Jobname"); 
      job.print(set); 
     } catch (PrinterException e) { 
     } 
    } 

现在你能得到你的喜欢到喜欢这个新的高分辨率纸的一切!

public class PrintReport implements Printable { 

    @Override 
    public int print(Graphics g, PageFormat pf, int page) throws PrinterException { 
     // Convert pixels to cm to lay yor page easy on the paper... 
     double cmPx = dpi/2.54; 
     Graphics2D g2 = (Graphics2D) g; 
     int totalPages = 2; // calculate the total pages you have... 
     if (page < totalPages) { 

      // Draw Page Header 
      try { 
       BufferedImage image = ImageIO.read(ClassLoader.getSystemResource(imgFolder + "largeImage.png")); 
       g2.drawImage(image.getScaledInstance((int) (4.8 * cmPx), -1, BufferedImage.SCALE_SMOOTH), (int) (cmPx), 
         (int) (cmPx), null); 
      } catch (IOException e) { 
      } 
      // Draw your page as you like... 
      // End of Page 
      return PAGE_EXISTS; 
     } else { 
      return NO_SUCH_PAGE; 
     } 
    }