import com.sun.media.jai.codec.FileSeekableStream; import javax.media.jai.widget.ScrollingImagePanel; /** * This program decodes an image file of any JAI supported * formats, such as GIF, JPEG, TIFF, BMP, PNM, PNG, into a * RenderedImage, scales the image by 2X with bilinear * interpolation, and then displays the result of the scale * operation. */ public class JAISampleProgramScale{ /** The main method. */ public static void main(String[] args) { /* Validate input. */ if (args.length != 1) { System.out.println("Usage: java JAISampleProgram " + "input_image_filename"); System.exit(-1); } /* * Create an input stream from the specified file name * to be used with the file decoding operator. */ FileSeekableStream stream = null; try { stream = new FileSeekableStream(args[0]); } catch (IOException e) { e.printStackTrace(); System.exit(0); } /* Create an operator to decode the image file. */ RenderedOp image = JAI.create("stream", stream); /* * Create a standard bilinear interpolation object to be * used with the "scale" operator. */ Interpolation interp = Interpolation.getInstance( Interpolation.INTERP_BILINEAR); int value = 45; float angle = (float)(value * (Math.PI/180.0F)); // Create a ParameterBlock and specify the source and // parameters ParameterBlock pb = new ParameterBlock(); pb.addSource(image); // The source image pb.add(1.2F); // The xScale pb.add(1.2F); // The yScale pb.add(0.0F); // The x translation pb.add(0.0F); // The y translation pb.add(new InterpolationNearest()); // The interpolation // Create the scale operation RenderedOp im = JAI.create("scale", pb, null); //-----------------------------------add the other operator sample code here------------------------- /* Get the width and height of image2. */ int width = im.getWidth(); int height = im.getHeight(); /* Attach image2 to a scrolling panel to be displayed. */ ScrollingImagePanel panel = new ScrollingImagePanel(im, width, height); /* Create a frame to contain the panel. */ Frame window = new Frame("JAI Sample Program"); window.add(panel); window.pack(); window.show(); } }
用法: javac JAISampleProgramScale.java REM pause java JAISampleProgramScale a.jpga.jpg是楼主要显示的图形文件
当然,也不是没有办法。
对于第一种方法,速度慢主要是我需要得到BufferedImage,如何快速的从Image转化成BufferedImage呢?
对于第二种方法,似乎可以通过设置RenderingHints来提高图片的清晰度,但是怎么设置呢?下面是代码:public BufferedImage getScaledImage(BufferedImage bi, int fw, int fh){
double ratio=1.0;
int oh=bi.getHeight(),
ow=bi.getWidth();
if(oh<=fh&&ow<=fw)return bi;//smaller than the required bounds
double ratio1=(double)fh/(double)oh,ratio2=(double)fw/(double)ow;
if(ratio1<ratio2)ratio=ratio1;
else ratio=ratio2;
/*
//第一种方法:缺点(速度慢)优点(清晰度高)
int ww=(int)(ow*ratio),hh=(int)(oh*ratio);
Image tempimg=bi.getScaledInstance(ww,hh,Image.SCALE_SMOOTH);
//如何快速的把Image转化成BufferedImage?
BufferedImage tag=new BufferedImage(ww,hh,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(tempimg,0,0,ww,hh,null);
return tag;
*/
//第二种方法:缺点(清晰度低)优点(速度快)
RenderingHints hints=new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
//如何设置RenderingHints,提高图片的清晰度?
AffineTransformOp op = new
AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio),hints);
return op.filter(bi, null);
}
或者
RenderingHints hints=new RenderingHints(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
http://java.sun.com/products/java-media/jai/downloads/download-1_1_1_01.html
import java.awt.image.renderable.ParameterBlock;
import java.io.IOException;
import javax.media.jai.*;
import com.sun.media.jai.codec.FileSeekableStream;
import javax.media.jai.widget.ScrollingImagePanel;
/**
* This program decodes an image file of any JAI supported
* formats, such as GIF, JPEG, TIFF, BMP, PNM, PNG, into a
* RenderedImage, scales the image by 2X with bilinear
* interpolation, and then displays the result of the scale
* operation.
*/
public class JAISampleProgramScale{ /** The main method. */
public static void main(String[] args) {
/* Validate input. */
if (args.length != 1) {
System.out.println("Usage: java JAISampleProgram " +
"input_image_filename");
System.exit(-1);
} /*
* Create an input stream from the specified file name
* to be used with the file decoding operator.
*/
FileSeekableStream stream = null;
try {
stream = new FileSeekableStream(args[0]);
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
} /* Create an operator to decode the image file. */
RenderedOp image = JAI.create("stream", stream);
/*
* Create a standard bilinear interpolation object to be
* used with the "scale" operator.
*/
Interpolation interp = Interpolation.getInstance(
Interpolation.INTERP_BILINEAR);
int value = 45;
float angle = (float)(value * (Math.PI/180.0F)); // Create a ParameterBlock and specify the source and
// parameters ParameterBlock pb = new ParameterBlock();
pb.addSource(image); // The source image
pb.add(1.2F); // The xScale
pb.add(1.2F); // The yScale
pb.add(0.0F); // The x translation
pb.add(0.0F); // The y translation
pb.add(new InterpolationNearest()); // The interpolation // Create the scale operation
RenderedOp im = JAI.create("scale", pb, null);
//-----------------------------------add the other operator sample code here-------------------------
/* Get the width and height of image2. */
int width = im.getWidth();
int height = im.getHeight(); /* Attach image2 to a scrolling panel to be displayed. */
ScrollingImagePanel panel = new ScrollingImagePanel(im, width, height); /* Create a frame to contain the panel. */
Frame window = new Frame("JAI Sample Program");
window.add(panel);
window.pack();
window.show();
}
}
javac JAISampleProgramScale.java
REM pause
java JAISampleProgramScale a.jpga.jpg是楼主要显示的图形文件