对于java的打印问题

779 篇文章 0 订阅
<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </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"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值