/********************************** * 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 int imagew,imageh;
public int print (Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { System.out.println("pageIndex"+pageIndex); if (pageIndex >= 1) { return Printable.NO_SUCH_PAGE; } x = pageFormat.getImageableX(); y = pageFormat.getImageableY(); w = pageFormat.getImageableWidth(); h = pageFormat.getImageableHeight();
/********************************** * 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();
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); } } }