繁体   English   中英

在 Java 中打印 BufferedImage

[英]Printing a BufferedImage in Java

有谁知道如何在 Java 中打印 BufferedImage ?

打印就像在屏幕上绘图一样,所以最终你会得到一个 Graphics 对象,你只需将图像绘制到其中即可。

我试图通过扩展 BufferedImage 来实现像这样的 Printable 接口(我从网上找到的代码拼贴)来做到这一点:

package components;
import java.awt.image.PixelGrabber;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.applet.Applet;
import javax.imageio.*;
import java.awt.Image.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.net.*;
import java.lang.*;
import java.io.*;
import java.io.File;
import java.lang.Integer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.RenderingHints;
import java.lang.String;
import java.io.File;
import java.util.Iterator;
import javax.imageio.*;
import javax.imageio.stream.*;
import java.lang.ClassLoader.*;
import java.lang.Class;
import java.io.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.print.attribute.standard.MediaSize.*;
import javax.print.event.*;
import java.awt.MediaTracker;
import java.awt.Color;
import java.awt.*;
import java.awt.print.*;
import javax.print.attribute.*;
import javax.swing.UIManager;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.util.Hashtable;
import java.awt.image.IndexColorModel;
import java.awt.print.Printable.*;
import java.awt.Rectangle;
import java.awt.Graphics2D;

public class printableBufferedImage extends BufferedImage implements Printable
{
   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;
 }
}

我不确定你说的印刷是什么意思。 在打印机上打印? 打印到标准输出? 写入文件?

您可以从 sun 查看本教程。 如果您要写入文件。 在您最喜欢的图像工具中打开并从那里打印。

http://download.oracle.com/javase/tutorial/2d/images/saveimage.html

如果您正在构建一个图形应用程序并使用类似 AWT 的东西,您可以使用 java 打印 API,这可能需要一些踢踏舞。

在这里检查我解决的问题: Java PrinterJob,无论如何,高质量打印最终得到 72 DPI解决方案是将图像绘制到 JPanel 的paintComponent(Graphics g) 方法并将此面板传递给 Print 实用程序类,在该类中,您可以轻松地根据需要缩放图像/面板以保持图像的高质量,听起来是一样的,但结果不是,我阅读了有关在“屏幕图形上下文”之外使用图像的建议,但在尝试之前不知道该怎么做解决办法,试试

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM