以前用.Net开发的时候,做过一次缩略图的功能
缩出来的图如下:现在用Java开发了,又碰到这个功能:
缩出来的图如下:
老板说。java的缩略图不够柔和,谁能告诉我怎么把图变柔和啊?我的java代码如下:
File _file = new File(request.getSession().getServletContext().getRealPath("/") + bfilename); //读入文件
Image src = javax.imageio.ImageIO.read(_file); //构造Image对象
BufferedImage tag = new BufferedImage(145,109,BufferedImage.TYPE_INT_RGB);
    
tag.getGraphics().drawImage(src,0,0,145,109,null); //绘制缩小后的图
FileOutputStream out=new FileOutputStream(request.getSession().getServletContext().getRealPath("/") + sfilename); //输出到文件流     
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); //近JPEG编码
out.close(); 

解决方案 »

  1.   

    //把原图改成缩略图
         InputStream stream2 = file.getInputStream(); 
         BufferedImage src=ImageIO.read(stream2);//构造Image对象
         int wideth=src.getWidth(null); //得到源图宽
            int height=src.getHeight(null); //得到源图长
            BufferedImage tag=new BufferedImage(wideth/2,height/2,BufferedImage.TYPE_INT_BGR);
            tag.getGraphics().drawImage(src,0,0,wideth/2,height/2,null);//绘制缩小后的图
      

  2.   

    缩放图的算法你可以用awt里自带的 3中插值方法,参考AffineTransform
    此外还可以google搜索lancozs
      

  3.   

    对于一个已经存在的Image对象,得到它的一个缩放的Image对象可以使用Image的getScaledInstance方法: 
    Image scaledImage=sourceImage. getScaledInstance(100,100, Image.SCALE_DEFAULT); //得到一个100X100的图像 
    Image doubledImage=sourceImage. getScaledInstance(sourceImage.getWidth(this)*2,sourceImage.getHeight(this)*2, Image.SCALE_DEFAULT); //得到一个放大两倍的图像,这个程序一般在一个swing的组件中使用,而类Jcomponent实现了图像观察者接口ImageObserver,所有可以使用this。 
      

  4.   

    显示的时候用css美化 一下
      

  5.   

    java按比例压缩图片的源代码,用java如何把图片处理到指定大小
      

  6.   

    报告5,6楼。
    我试过了。但是缩出来的图还是很锐化,
    老板说.Net的图比较钝。。我也不知道这个要怎么做。。很苦恼啊。。
    谁能来指点我一下呀。。报告10楼。我不要按比例缩。谢谢啦
      

  7.   

    报告7楼。不会CSS
    如果真的要做还要现学一下。。
      

  8.   

    没人回答了嘛?
    试过CSS了。好像也不行。使图表看着模糊了。也不是钝化。
    谁能帮一下呀。。
      

  9.   

    用这个开源的的jmagick(功能很强大,有生成的缩略图.加水印,旋转....)
      

  10.   

    public static void createMiniPic(File file, float width, float height)
    throws IOException {
    Image src = javax.imageio.ImageIO.read(file); // 构造Image对象
    int old_w = src.getWidth(null); // 得到源图宽
    int old_h = src.getHeight(null);
    int new_w = 0;
    int new_h = 0; // 得到源图长
    float tempdouble;
    if (old_w >= old_h) {
    tempdouble = old_w / width;
    } else {
    tempdouble = old_h / height;
    } if (old_w >= width || old_h >= height) { // 如果文件小于锁略图的尺寸则复制即可
    new_w = Math.round(old_w / tempdouble);
    new_h = Math.round(old_h / tempdouble);// 计算新图长宽
    while (new_w > width && new_h > height) {
    if (new_w > width) {
    tempdouble = new_w / width;
    new_w = Math.round(new_w / tempdouble);
    new_h = Math.round(new_h / tempdouble);
    }
    if (new_h > height) {
    tempdouble = new_h / height;
    new_w = Math.round(new_w / tempdouble);
    new_h = Math.round(new_h / tempdouble);
    }
    }
    BufferedImage tag = new BufferedImage(new_w, new_h,
    BufferedImage.TYPE_INT_RGB);
    tag.getGraphics().drawImage(src, 0, 0, new_w, new_h, null); // 绘制缩小后的图
    FileOutputStream newimage = new FileOutputStream(file); // 输出到文件流
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(tag);
    param.setQuality((float) (100 / 100.0), true);// 设置图片质量,100最大,默认70
    encoder.encode(tag, param);
    encoder.encode(tag); // 将JPEG编码
    newimage.close();
    }
    }
    ---
    param.setQuality((float) (100 / 100.0), true);// 设置图片质量,100最大,默认70

    应该这段话可以! 自己试试吧! 
      

  11.   

    public static boolean createMark(String filePath, String water,
    String font) {
    ImageIcon imgIcon = new ImageIcon(filePath);
    Image theImg = imgIcon.getImage();
    ImageIcon waterIcon = new ImageIcon(water);
    Image waterImg = waterIcon.getImage();
    int width = theImg.getWidth(null);
    int height = theImg.getHeight(null);
    BufferedImage bimage = new BufferedImage(width, height,
    BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bimage.createGraphics();
    g.setColor(Color.red);
    Font f = new Font("黑体", Font.BOLD, 20);
    g.setFont(f);
    g.setBackground(Color.white);
    g.drawImage(theImg, 0, 0, null);
    g.drawImage(waterImg, 0, 0, null);
    g.drawString(font, 0, 20); // 添加文字
    g.dispose();
    try {
    FileOutputStream out = new FileOutputStream(filePath);
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
    param.setQuality(50f, true);
    encoder.encode(bimage, param);
    out.close();
    } catch (Exception e) {
    return false;
    }
    return true;
    }=========怎么把图片显示在! 左下角!! g.drawImage(waterImg, 0, 0, null); 0 0 是x 和 y 坐标!大家帮我算算!