This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Help!!! Image print problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Help!!! Image print problem" Watch "Help!!! Image print problem" New topic
Author

Help!!! Image print problem

sam patel
Ranch Hand

Joined: Jun 13, 2002
Posts: 103
hi,
I want to print an image using JAI(Java Advance Imaging). I have something which scales the image
first and then prints it out. It prints the image, but the resolution of the Image is horrible.
The image comes out so blury it hard to see. Can anybody help me with this problem.
Any help is always appreciated.
Here is the Code.

import java.util.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.print.*;
import javax.media.jai.*;
import com.sun.media.jai.codec.*;
import java.awt.image.renderable.ParameterBlock;
import javax.swing.RepaintManager;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class JAIImagePrint implements Printable {
protected RenderedImage renderedImage;
protected int imageWidth, imageHeight;
protected Point printLoc = new Point(0,0);

public void setPrintLocation(Point d) {
printLoc = d;
}
public Point getPrintLocation() {
return printLoc;
}
public void loadAndPrint(String filename){
RenderedOp renderedOp = JAI.create("fileload", filename);
renderedImage = renderedOp.createInstance();
print();
}

protected void print() {
PrinterJob pj = PrinterJob.getPrinterJob().getPrinterJob();
pj.setPrintable(this);
pj.printDialog();
try{
pj.print();
}catch(Exception e){System.out.println(e);}
}

public int print(Graphics g, PageFormat f, int pageIndex){
if(pageIndex >= 1) return Printable.NO_SUCH_PAGE;
Graphics2D g2d = (Graphics2D) g;
g2d.translate(f.getImageableX(), f.getImageableY());
if(renderedImage != null){
double scale = Math.min(f.getImageableWidth()/ renderedImage.getWidth(), f.getImageableHeight()/renderedImage.getHeight());
Interpolation interp = Interpolation.getInstance(Interpolation.INTERP_BICUBIC);
ParameterBlock params = new ParameterBlock();
params.addSource(renderedImage);
params.add(new Float(scale));
params.add(new Float(scale));
params.add(0.0F);
params.add(0.0F);
params.add(interp);
PlanarImage image = JAI.create("scale",params);
printImage(g2d, image);
return Printable.PAGE_EXISTS;
} else return Printable.NO_SUCH_PAGE;
}
public void printImage(Graphics2D g2d, RenderedImage image){
if((image == null)|| (g2d == null)) return;
int x = printLoc.x;
int y = printLoc.y;
AffineTransform at = new AffineTransform();
at.translate(x,y);
g2d.drawRenderedImage(image,at);
}

}
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Help!!! Image print problem
 
Similar Threads
Help!!! Image print problem
Help!!! Image print problem
Converting an byte[] to an image
Help!!! Image print problem
How to print an RenderedImage?