对于java的打印问题

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

  对于java打印问题,各种书上谈的很少。我想主要原因可能是java的打印功能太弱了,没有什么可介绍的。 最近,我因为工作的关系,用到了java的打印图象功能。不过因为缺少可参考的例子和教材,我只有查看jdk 的API文档,的确不是很爽。下面就把我的程序给大家讲讲,顺便白话一下java的打印(是jdk1.2的)。 java的打印类都在java.awt.print包下,主要有四个类和两个interface:PrinterJob,PageFormat, Paper,Book; Printable,Pageable。(详细的情况,请查看jdk的API文档,我只讲我程序中用到的部分)

  Interface包括 Printable:主要是用来打印的接口,在打印的时候,它的print()方法不断地被调用,直到 返回NO_SUCH_PAGE为止。

  PrinterJob:初始化打印操作,可以显示系统特定的打印对话框,例如windows的。

  PageFormat:描述可打印区。例如我的程序用的几个方法

  public double getImageableX();

  public double getImageableY();

  public double getImageableWidth();

  public double getImageableHeight();

  packagejinicup.printer;

  importjava.awt.*;

  importjava.awt.print.*;

  importjava.awt.event.*;

  importjavax.swing.JPanel;

  importjavax.swing.JFrame;

  importjavax.swing.ImageIcon;

  importjava.io.*;

  /**********************************

  * Implemenation of the printer service

  ***********************************/

  public class PrinterImpl extends JPanel

  implements Printable {

  private Image image;

  private PrinterJob printJob;

  private double x,y,w,h;

  private intimagew,imageh;

  PrinterImpl () {

  printJob = PrinterJob.getPrinterJob();

  printJob.setPrintable(this);

  printJob.printDialog();

  }

  public int print (Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {

  System.out.println("pageIndex"+pageIndex);

  if (pageIndex >= 1) {

  returnPrintable.NO_SUCH_PAGE;

  }

  x = pageFormat.getImageableX();

  y = pageFormat.getImageableY();

  w = pageFormat.getImageableWidth();

  h = pageFormat.getImageableHeight();

  if(imagew >= imageh){

  h=w*imageh/imagew;

  }else{

  w=h*imagew/imageh;

  }

  System.out.println(x+" "+y);

  System.out.println(w+" "+h);

  drawGraphics(graphics);

  returnPrintable.PAGE_EXISTS;

  }

  public void paint (Graphics graphics) {

  drawGraphics(graphics);

  }

  private void drawGraphics (Graphics graphics) {

  graphics.drawImage(image, (int)x,(int)y,(int)w,(int)h, null);

  // graphics.drawOval(10, 10, 100, 50);

  }

  /**********************************

  * starts the printing

  * @param byteArrayOfJPEGFile a valid byte array of a jpg file (can be directly from the camera)

  ***********************************/

  public void printByteArray (byte[] byteArrayOfJPEGFile) {

  //Toolkit tool = Toolkit.getToolkit();

  // image=tool.createImage(byteArrayOfJPEGFile);

  image = (new ImageIcon(byteArrayOfJPEGFile)).getImage();

  imagew=image.getWidth(null);

  imageh=image.getHeight(null);

  System.out.println(imagew+" "+imageh);

  System.out.println("kkk");

  try {

  System.out.println("start printing");

  printJob.print();

  System.out.println("printing was spooled to the printer");

  } catch (Exception ex) {

  System.out.println(ex);

  }

  return;

  }

  /**********************************

  * main method, only for text purposes

  * @param args no args are used

  ***********************************/

  public static void main (String[] args) {

  PrinterImpl pi = new PrinterImpl();

  try {

  FileInputStream fs = new FileInputStream("e:/test.jpg");

  System.out.println(fs.available());

  byte[] array = new byte[fs.available()];

  fs.read(array);

  pi.printByteArray(array);

  } catch (Exception e) {

  System.out.println(e);

  }

  }

  }

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
阅读更多
个人分类: xml ajax
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭