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);
}
}