Skip to main content

[JAVA2D] Printing images efficiently

5 replies [Last post]
Anonymous

Hi guys. I'm running into a problem with printing Tiffs. While I realize
that Tiffs are a JAI problem, I was told by a guy off the JAI list that my
problem was more central to Java, Java2d and the Java print system than to
JAI, as once the images are loaded by JAI they're bitmaps anyway, and that
you might have some ideas on how to get around it.

When I print Tiffs from my application, the print spool can grow quite
large, and for some cases (50 tiffs are not uncommon in a print job) it
can completely freeze the machine due to lack of memory.

For example, I have two TIFF images that are stored in the database
using the the DEFLATE compression at 32k and 28k, but when I print
the images (under Windows), the overall size of the print job according
to the Windows spooler is 42 MB. As you can imagine, this quickly
becomes a problem.

Is there a right way to handle this? Is there anything I can do? I'm
currently using the following code, any suggestions are greatly
appreciated:

public int print(Graphics pgfx, PageFormat format, int pageIdx) throws
PrinterException {
if (pageIdx>=maxPages) {
return NO_SUCH_PAGE;
}
if (doc.getImage()!=null) {
// forcing this to 0 margins, because we want our printed
image to mimic
// the input.
pgfx.translate(0, 0);
int formh = (int) format.getHeight();
int formw = (int) format.getWidth();
// getImage() below returns a PlanarImage
pgfx.drawImage(this.getImage().getAsBufferedImage(), 0, 0,
formw, formh, this);
return PAGE_EXISTS;
} else {
logger.error("Trying to print null document image...");
return NO_SUCH_PAGE;
}
}

Thanks, as always, for any insight you might provide!

John

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
John Wells

マーズ
said:
> Greetings John,
> try lower quality resolution thru java.awt.RenderingHints before printing.

Could you possibly provide an example of this? I'm looking through the
docs but a little confused on how to actually accomplish a lower
resolution based on the keys provided.

Thanks!
John

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

philrace
Offline
Joined: 2003-06-09
Points: 0

Unfortunately I don't have much useful to offer.
You already know (from discussions on the JAI list) that
images are printed as 24 bit RGB and this is excessive
for images that could be represented as 8 bit or even
1 bit images.

I also have no reason to suppose that specifying a
different quality setting in 2D or the printer driver,
or different resolution etc will make a difference

None of that affects how much data is sent to the
printer. That is determined by the image resolution.

If your image is 1000 x 1000 pixels then we send
1000 x 1000 x 3 bytes per pixel = 3Mb to the printer.

Notice that we let the printer scale it so if you
prescaled it to 300 dpi for printing where that wasn't
really the image resolution you should refrain from
doing so.

Also ironically spool file size may be larger for
higher end PDL (eg Postscript or PCL 6) printers.

Inkjet printers may accept a simple compressed image
as their raw data format so they may actually do better
than those high end printers.

PCs with later versions of windows (at least XP) seem
to fare better with large spool files. More memory in
the PC helps too.

-Phil Race.
Java 2D

jbwiv
Offline
Joined: 2004-04-28
Points: 0

Hi Phil.

Sorry I missed your message...for some reason it didn't make it in to my inbox and I only found it when reviewing the forums.

Is there no way to scale down the image to present it at an acceptable resolution on an output device like a printer? Seems like one might need higher resolutions for monitor viewing, etc, but not really need these higher res values when sent to a printer.

I've found at least one temporary workaround. Using iText from lowagie.com, I'm converting the Tiffs and JPEGS to pdfs before printing, and this reduces the print job size by (on average) 140%.

Wonder why this is possible on conversion to pdf, but not directly from Java?

Thanks,
John

マーズ

Greetings John,
try lower quality resolution thru java.awt.RenderingHints before printing.

---in the article <57807.66.192.236.118.1125427549.squirrel@devsea.com>, John
Wells wrote:
>Hi guys. I'm running into a problem with printing Tiffs. While I
>realize
>that Tiffs are a JAI problem, I was told by a guy off the JAI list that
>my
>problem was more central to Java, Java2d and the Java print system than
>to
>JAI, as once the images are loaded by JAI they're bitmaps anyway, and
>that
>you might have some ideas on how to get around it.
>
>When I print Tiffs from my application, the print spool can grow quite
>large, and for some cases (50 tiffs are not uncommon in a print job) it
>can completely freeze the machine due to lack of memory.
>
>For example, I have two TIFF images that are stored in the database
>using the the DEFLATE compression at 32k and 28k, but when I print
>the images (under Windows), the overall size of the print job according
>to the Windows spooler is 42 MB. As you can imagine, this quickly
>becomes a problem.
>
>Is there a right way to handle this? Is there anything I can do? I'm
>currently using the following code, any suggestions are greatly
>appreciated:
>
>public int print(Graphics pgfx, PageFormat format, int pageIdx) throws
>PrinterException {
> if (pageIdx>=maxPages) {
> return NO_SUCH_PAGE;
> }
> if (doc.getImage()!=null) {
> // forcing this to 0 margins, because we want our printed
>image to mimic
> // the input.
> pgfx.translate(0, 0);
> int formh = (int) format.getHeight();
> int formw = (int) format.getWidth();
> // getImage() below returns a PlanarImage
> pgfx.drawImage(this.getImage().getAsBufferedImage(), 0, 0,
>formw, formh, this);
> return PAGE_EXISTS;
> } else {
> logger.error("Trying to print null document image...");
> return NO_SUCH_PAGE;
> }
> }
>
>Thanks, as always, for any insight you might provide!
>
>John
>
>===========================================================================
>To unsubscribe, send email to listserv@java.sun.com and include in the
>body
>of the message "signoff JAVA2D-INTEREST". For general help, send email
>to
>listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

John Wells

マーズ wrote:

>Greetings John,
>try lower quality resolution thru java.awt.RenderingHints before printing.
>
>
Thanks to all who've replied, both on and off list. I've run into
another problem that's delaying my capability to try some of your
suggestions, but as soon as they're resolved and I'm able to give them
ago I'll post my results. Thanks!

John

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".