outOfMemory printing a BufferedImage

M

Morten Nørgaard

Hi,

hope someone knows this one. I get an outOfMemory when I try to print my
bufferedImage, an JPEG at some 2400 x 3000 pixels, approx. 900 kb large
(disk-wise). It doesn't seem like overkill to me - but apparently to the
JVM. I could of course increase the default heap-size - this works wonders.
But I'd rather forego this option, as it's my hope my application will be
installed with customers, who won't have a clue about heap-size. There must
be a better way, obviously, I think. But ... how?

The code is this: ( 2 classes - one main, the below, and one
printable-class, the bottom one). I boiled it down to the absolute
essential:


import java.awt.print.*;
import javax.swing.*;

public class printTest
{
public printTest()
{
PrinterJob printJob = PrinterJob.getPrinterJob();
Book book = new Book();
PageFormat pg = printJob.defaultPage();

book.append( new printablePage( "C:\\imageTest.tif"), pg );
// printablePage class follows below.

printJob.setPageable( book );
if ( printJob.printDialog() )
{
try
{
printJob.print();
JOptionPane.showMessageDialog( null, "succes" );
}
catch ( Exception ex )
{
JOptionPane.showMessageDialog( null, "succes not so much" );
ex.printStackTrace();
}
}
}

public static void main( String[] args )
{
new printTest();
}
}
// end of printTest class

// beg. of printablePage class
import java.awt.*;
import java.awt.print.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;

public class printablePage implements Printable, ImageObserver
{
File file = null;

public printablePage( String s )
{
file = new File("c:\\ImageTest.jpg");
}

public int print( Graphics g, PageFormat pageFormat, int page )
{
try
{
BufferedImage img = ImageIO.read( file );
g.drawImage( img, (int) pageFormat.getImageableX() , (int)
pageFormat.getImageableY() , (int) pageFormat.getImageableWidth() , (int)
pageFormat.getImageableHeight(), 0,0,(int)img.getWidth() ,
(int)img.getHeight(), this );
}
catch (Exception ex )
{
ex.printStackTrace();
}

return (PAGE_EXISTS);
}

public boolean imageUpdate(Image img, int infoflags, int x, int y, int
width, int height)
{
return false;
}

}



Thanks in advance for any input you may have to offer,


Morten
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top