Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Swing / AWT / SWT and the fly likes How to print an RenderedImage? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to print an RenderedImage?" Watch "How to print an RenderedImage?" New topic
Author

How to print an RenderedImage?

Alvaro Quintana
Greenhorn

Joined: Dec 03, 2010
Posts: 10

Hi all,

-Problem Context-
I've build an applet to properly manipulate and visualize TIFF images in browsers (manipulate means to rotate, zoom, an multitiff support). The applet receive base64 string representation of the TIFF image and show it to the user properly using JAI API. The applet keep the image without any transformation (as a RenderedImage), and all operations (rotate, zoom) are kept in another Object (RenderedOp).

-Problem-
I want to implement a print function that allows the user to print the TIFF image kept in the applet, but when i print the image, only a portion of the image is printed because the image is to big. I tried to scale the image to fit the page size, but when printed, the image is not showed properly (image lost quality). After that, i tried to save the image into a file, to check if the image, when coded and decoded to a Base64 string representation, was losing quality. After save the image, i printed it using an image viewer (Ubuntu Image Viewer is the one i am using), but when printed from the image viewer, the printout was correct (the image was fitted in the paper without lose quality).

-Question-
¿How can i print an image bigger than paper size without lose quality?
¿Am i doing the wrong operation to the image to fit the page? (scale operation in this case).

Below i put the code of the print method (implemented in a Printable Object) that is used to print the image. In this code the image is scale to fit the page.

-Code-


Any comments or suggestions are welcome. Thanks in advance for your help.
Alvaro Quintana
Greenhorn

Joined: Dec 03, 2010
Posts: 10

Well, after a lot of work i've discover that the printout image has poor quality because PrintJob Object by default set page dimension for 72 DPI. Now my problem is to properly change that value. Below i show the code i'm using to do it, but is not working.

Now the real problem for me is to properly change DPI attribute ..

Thanks in advance,
Alvaro Quintana
Greenhorn

Joined: Dec 03, 2010
Posts: 10

Problem Solved!

-¿What was wrong?-
I was scaling the image (RenderedImage) Object.

-Solution-
Keep the original image (without scale) and scale the Graphics2D object before draw image on it. After that the image is automatically adjusted to Graphics2D object and correctly printed (using best DPI configuration). Below i add the correct code to properly print an RenderedImage Object.





Hope this help!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to print an RenderedImage?
 
Similar Threads
Converting an byte[] to an image
Help!!! Image print problem
Help!!! Image print problem
Help!!! Image print problem
Help!!! Image print problem