在网上找到如下代码,但不是很明白,请各位帮助:import java.awt.image.*;
import java.awt.*;
import java.net.*;public class app extends java.applet.Applet {
  Image source;
  Image resizedImage;  public void init() {
    MediaTracker media = new MediaTracker(this);
    // java how-to image for example
    source = getImage(getDocumentBase(),"jht.gif");
    media.addImage(source,0);
    try {
      media.waitForID(0);
      // scale down, half the original size  
      ImageFilter replicate = 
         new ReplicateScaleFilter
           (source.getWidth(this)/2, source.getHeight(this)/2);
      ImageProducer prod = 
         new FilteredImageSource(source.getSource(),replicate);
      resizedImage = createImage(prod);
      media.addImage(resizedImage,1);
      media.waitForID(1);
      } 
    catch(InterruptedException e) {}
    }  public void paint(Graphics g) {
    g.drawImage(source, 10,10,this);
    g.drawImage(resizedImage,10, 80,this);
    }
}1。如果巴"jht.gif"改成任何格式的图片文件名称,还能奏效吗?
2。怎么最后没有存储缩放后图片的代码?
3。对于jpg图片,如此缩放后压缩质量怎样保留或改变?
4。如果哪位能提供功能更强大的任意(或者多种)图片格式缩放代码,小弟将不胜感激!!!!

解决方案 »

  1.   

    险些忘了一点:5。单纯代码缩放的图片效果不会很好,谁有alpha或者其他模糊算法能够使所放出来图片看上去更自然,请不吝赐教,小弟另有分相赠!!
      

  2.   

    public static boolean renderImage(OutputStream output,
                                          InputStream input) {        try {
                // イメージの読み込み | read image use JAI(SeekableSteam ,RenderedOp)
                SeekableStream ss = SeekableStream.wrapInputStream(input, true);
                PlanarImage src = JAI.create("stream", ss);            // サムネールの作成 // | Construct Thumbnail            // 画像サイズの取得 | get the size of image
                float srcWidth = Float.parseFloat(src.getProperty("image_width").
                                                  toString());
                float srcHeight = Float.parseFloat(src.getProperty("image_height").
                                                   toString());            // 倍率の割り出し | fit diameter
                float srcScale = srcWidth / srcHeight;            float tagWidth = 0, tagHeight = 0;
                if (srcScale < IMAGE_SCALE) {
                    tagHeight = srcHeight > IMAGE_HEIGHT ? IMAGE_HEIGHT : srcHeight;
                    tagWidth = tagHeight * srcScale;            }
                else {
                    tagWidth = srcWidth > IMAGE_WIDTH ? IMAGE_WIDTH : srcWidth;
                    tagHeight = tagWidth / srcScale;
                }            float xScale = (float) tagWidth / srcWidth;
                float yScale = (float) tagHeight / srcHeight;            // サイズを変更 | change the size use java.awt.image.render.ParameterBlock
                ParameterBlock pb = new ParameterBlock();
                pb.addSource(src);
                //--modify--
                //---new---
                pb.add(xScale); //width scale
                pb.add(yScale); //height scale
                //---end---
                pb.add(0.0F);
                pb.add(0.0F);
                src = JAI.create("scale", pb, null);            // エンコード |encode
                RenderedOp small = JAI.create("encode", src, output, "PNG", null);
                output.flush();
                return true;
            }
            catch (Exception e) {
                return false;
            }    }
      

  3.   

    请问JAI是什么?哪个包?谢谢!