`
TimerBin
  • 浏览: 355098 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java 缩略图

    博客分类:
  • JAVA
阅读更多

文章说明:根据用户上传的图片按等比例生成相应的的缩略图,两小例笔记。

第一、java-image-scaling 开源小工具生成图片缩略图

Google Code Url:http://code.google.com/p/java-image-scaling/

Maven地址:http://mvnrepository.com/artifact/com.mortennobel/java-image-scaling/

以下实例以0.8.5为例实例

1、Maven配置文件Pom.xml

<dependency>
  <groupId>com.mortennobel</groupId>
  <artifactId>java-image-scaling</artifactId>
  <version>0.8.5</version>
</dependency>

 

2、简单工具类

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.mortennobel.imagescaling.ResampleOp;

 

/**
  * 生产缩略图主方法
  * @param fileFromPath 来源文件地址 
  * @param fileOutPath 输出缩略图地址
  * @param outWidth   输出缩略图宽
  * @param outHeight  输出缩略图高
  * @throws IOException
  */
  public void chreatePictureChange(String fileFromPath,String fileOutPath, int outWidth, int outHeight) throws IOException {
      //创建输出缩略图文件 =空
      new File(fileOutPath).mkdirs();
      //来源文件
      BufferedImage fromImage = makeFormFile(new File(fileFromPath));
      //输出缩略图尺寸计算
       int[] outSize = countOutImgSize(fromImage, outWidth, outHeight);
       //输出缩略图 文件类型
        String outFileType = fileFromPath.substring(fileFromPath.lastIndexOf(".")+1);
        //输出缩略图 IO输出
        writeOutImage(makeOutFile(fromImage, (int) outSize[0], (int) outSize[1]),outFileType ,new File(fileOutPath));
  }

   /**
     * 将地址转为图片
     * @param file
     * @return
     * @throws IOException
     */
private  BufferedImage makeFormFile(File file) throws IOException {
      BufferedImage image = null;
      if (file != null && file.isFile() && file.exists()) {
           image = ImageIO.read(file);
       }
        return image;
 }

/**
  * 初始化 封装 目标图片大小
  * @param fromImage 源文件
  * @param outWidth 目标文件宽
  * @param outHeight 目标文件高
   * @return
   */
 private  int[] countOutImgSize(BufferedImage fromImage, int outWidth, int outHeight) {
         int[] zoomSize = new int[2];
         int fromWidth = fromImage.getWidth();
         int fromcHeigth = fromImage.getHeight();
         // 根据输出缩略图的宽和高 除以  原图的宽与高 获取缩放比例
         double ratio = 1.0;
         if (fromcHeigth > outHeight || fromWidth > outWidth) {
                // 根据原图中的 宽和高中较大的 值进行缩放
                if (fromcHeigth > fromWidth) {
                      ratio = outHeight * 1.0 / fromcHeigth;
                } else {
                      ratio = outWidth * 1.0 / fromWidth;
              }
         }
         //计算输出的缩略图宽度和高度
         zoomSize[0] = (int) (fromImage.getWidth() * ratio);
         zoomSize[1] = (int) (fromImage.getHeight() * ratio);
         return zoomSize;
  }

 /**
  * 组装 目标文件流
  * @param fromImage 源文件
  * @param outWidth  目标文件宽
  * @param outHeigth 目标文件高
  * @return
  */
 private BufferedImage makeOutFile(BufferedImage fromImage, int outWidth, int outHeigth) {
        ResampleOp resampleOp = new ResampleOp(outWidth, outHeigth);
        BufferedImage tag = resampleOp.filter(fromImage, null);
        return tag;
 }
 /**
  * 文件写出
  * @param fromImage 文件源
  * @param outName 目标文件名
  * @param outFileType 根据原文件名称后缀获得、指定生成缩略图是以哪种格式生成(JPG/PNG/GIF等) 
  * @throws IOException
  */
private  void writeOutImage(BufferedImage fromImage, String outFileType, File outFile)
    throws IOException {
          if (fromImage != null && outFileType!= null && !"".equals(outFileType)
          && outFile != null) {
               ImageIO.write(fromImage, outFileType, outFile);
           }
  }

 

 

直接调用chreatePictureChange方法就可以生成图片缩略图信息

所遇到问题:

1、生成缩略图的尺寸尽量宽高全部都填写---已解决。

2、注意生成的缩略图所设置的格式信息,部分PNG图片格式强制转为JPG时会导致生成的缩略图图片变为红色 --已解决。

优点:生成的大于当前尺寸的缩略图所占物理空间较小(小于等于原图大小),速度较快。

确定:图片清晰度较差。

第二种、thumbnailator 图片缩略小工具

Google Code Url:https://code.google.com/p/thumbnailator/

Maven下载地址:http://mvnrepository.com/artifact/net.coobird/thumbnailator

以下实例以0.4.6版本为例:

1、Maven配置文件Pom.xml

<dependency>
    <groupId>net.coobird</groupId>
    <artifactId>thumbnailator</artifactId>
    <version>0.4.6</version>
</dependency>

 

2、简单工具类

    

 /**
      * 图片缩略图切割 --- 根据文件地址 生成图片缩略图
      * @param fileFromPath 图片源文件地址
      * @param fileOutPath 图片缩略图地址
      * @param outWidth  图片缩略图宽度
      * @param outHeight 图片缩略图高度
      * @param scale 缩放比例
      * @param rotate 旋转度数
      * @return
      */
    public void chreatePictureChange(String fileFromPath,String fileOutPath, Integer outWidth, Integer outHeight, Float scale, Integer rotate) {
            Builder<File> builder = (Builder<File>) Thumbnails.of(fileFromPath);
            if (null != outWidth && null == outHeight) {
                builder.width(outWidth);
            }
            if (null == outWidth && null != outHeight) {
                builder.height(outHeight);
            }
            if (null != outWidth && null != outHeight) {
                builder.size(outWidth, outHeight);
            }
            if (null != scale) {
                builder.scale(scale);
            }
            if (null != rotate) {
                builder.rotate(rotate);
            }
            builder.toFile(fileOutPath);
     }

     /**
       * 图片缩略图切割 --- 根据文件流 生成图片缩略图
       * @param fileFrom 图片源文件地址
       * @param fileOutPath 图片缩略图地址
       * @param outWidth  图片缩略图宽度
       * @param outHeight 图片缩略图高度
       * @param scale 缩放比例
       * @param rotate 旋转度数
       * @return
       */
     public static void chreatePictureChange(InputStream fileFrom,String fileOutPath, Integer outWidth, Integer outHeight, Float scale, Integer rotate) {
         Boolean pictureChangeResult = false;
         try {
             Builder<InputStream> builder = (Builder<InputStream>) Thumbnails.of(fileFrom);
             if (null != outWidth && null == outHeight) {
                 builder.width(outWidth);
             }
             if (null == outWidth && null != outHeight) {
                 builder.height(outHeight);
             }
             if (null != outWidth && null != outHeight) {
                 builder.size(outWidth, outHeight);
             }
             if (null != scale) {
                 builder.scale(scale);
             }
             if (null != rotate) {
                 builder.rotate(rotate);
             }
             builder.outputQuality(1.0d);
             builder.toFile(fileOutPath);
             pictureChangeResult = true;
         } catch (Exception e) {
             e.printStackTrace();
         }finally{
          fileFrom.close();
         }
     }

 

 

 outputQuality:输出的图片质量,范围:0.0~1.0,1为最高质量。注意使用该方法时输出的图片格式必须为jpg(即outputFormat("jpg")。其他格式我没试过,感兴趣的自己可以试试)。否则若是输出png格式图片,则该方法作用无效【这其实应该算是bug】。

来源地址:http://blog.csdn.net/wangpeng047/article/details/19624993

 

优点:清晰,可以指定缩略图的清晰度,功能强大。

缺点:生成缩略图物理大小大于原图,超出很多。

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics