Всем привет! Сам пишу на С++, но вот выпала нелегкая доля писать на Java  Сейчас бьюсь над тем, чтобы была возможность печати изображения, которое читается не из файла, а формируется передаваясь по сети. И все вроде просто и все вроде хорошо - но при посылке на печать одного изображения, в буффере формируется аж !15 документов :-( А HP принтер иногда вот такое выдает: PCL XL error Sybsystem: Image Error: MissingData Operator: ReadImage Position: 83 Реализация: | Код | public class ImagePainter extends Component implements Printable { private static final long serialVersionUID = 1L; private Image image;
public ImagePainter(Image image) { this.image = image; }
// Draw image, implements Printable interface public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException { if (image.getWidth(null) == 0 || image.getHeight(null) == 0) return NO_SUCH_PAGE;
Graphics2D g2D = (Graphics2D) g; g2D.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
g2D.setPaint(Color.black); MediaTracker mt = new MediaTracker(this); mt.addImage(image, 0); try { mt.waitForID(0); } catch (InterruptedException e) { } Rectangle drawRect = getDrawRect(pageFormat); g2D.drawImage(image, drawRect.x, drawRect.y, drawRect.width, drawRect.height, null // Do not wait for asynchronous operation ); g2D.dispose(); g2D = null; return PAGE_EXISTS; } // Returns draw coordinates private Rectangle getDrawRect(PageFormat pageFormat) { // Do resize if image size is more than page size if (image.getWidth(null) > pageFormat.getWidth() || image.getHeight(null) > pageFormat.getHeight()) { double maxValue = Math.max(image.getWidth(null), image.getHeight(null)); boolean isWidthMax = image.getWidth(null) == maxValue ? true : false; double scale = 0; if (isWidthMax) { scale = image.getWidth(null) / pageFormat.getWidth(); } else { scale = image.getHeight(null) / pageFormat.getHeight(); } int width = (int) (image.getWidth(null) / scale); int heigth = (int) (image.getHeight(null) / scale); return new Rectangle(0, 0, width, heigth); } // if less then page size, draws it in the center of the page int x = (int)(pageFormat.getWidth() - (int)image.getWidth(null)) / 2; return new Rectangle(x, 0, (int)image.getWidth(null), (int)image.getHeight(null)); } }
|
использование: | Код | private void OnPrint() { Image imgToPrint = m_ImageScroll.getImage(); if (imgToPrint == null) return ; PrinterJob printJob = PrinterJob.getPrinterJob(); PageFormat format = new PageFormat(); Book book = new Book(); format = printJob.pageDialog(format); book.append(new ImagePainter(imgToPrint), format); printJob.setPageable(book);
if (printJob.printDialog()) { try { printJob.print(); } catch (Exception PrintException) { PrintException.printStackTrace(); } } }
|
Помогите, люди добрые, а то я совсем на Java ничего никогда не писал, а тут вот надо по работе :-(((
|