当前位置: 移动技术网 > IT编程>开发语言>Java > java实现创建缩略图、伸缩图片比例生成的方法

java实现创建缩略图、伸缩图片比例生成的方法

2019年07月22日  | 移动技术网IT编程  | 我要评论

本文实例讲述了java实现创建缩略图、伸缩图片比例生成的方法。分享给大家供大家参考。具体实现方法如下:

该实例支持将image的宽度、高度缩放到指定width、height,并保存在指定目录 通过目标对象的大小和标准(指定)大小计算出图片缩小的比例,可以设置图片缩放质量,并且可以根据指定的宽高缩放图片。

具体代码如下所示:

复制代码 代码如下:

package com.hoo.util;
 
import java.awt.image;
import java.awt.image.bufferedimage;
import java.io.file;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.net.malformedurlexception;
import java.net.url;
import javax.imageio.imageio;
import com.sun.image.codec.jpeg.imageformatexception;
import com.sun.image.codec.jpeg.jpegcodec;
import com.sun.image.codec.jpeg.jpegencodeparam;
import com.sun.image.codec.jpeg.jpegimageencoder;
 
/**
 * <b>function:</b> 缩放图片工具类,创建缩略图、伸缩图片比例
 * @author hoojo
 * @createdate 2012-2-3 上午10:08:47
 * @file scaleimageutils.java
 * @package com.hoo.util
 * @version 1.0
 */
public abstract class scaleimageutils {
 
    private static final float default_scale_quality = 1f;
    private static final string default_image_format = ".jpg"; // 图像文件的格式
    private static final string default_file_path = "c:/temp-";
   
    /**
     * <b>function:</b> 设置图片压缩质量枚举类;
     * some guidelines: 0.75 high quality、0.5  medium quality、0.25 low quality
     * @author hoojo
     * @createdate 2012-2-7 上午11:31:45
     * @file scaleimageutils.java
     * @package com.hoo.util
     * @project jquerymobile
     * @version 1.0
     */
    public enum imagequality {
        max(1.0f), high(0.75f), medium(0.5f), low(0.25f);
       
        private float quality;
        public float getquality() {
            return this.quality;
        }
        imagequality(float quality) {
            this.quality = quality;
        }
    }
   
    private static image image;
   
    /**
     * <b>function:</b> 通过目标对象的大小和标准(指定)大小计算出图片缩小的比例
     * @author hoojo
     * @createdate 2012-2-6 下午04:41:48
     * @param targetwidth 目标的宽度
     * @param targetheight 目标的高度
     * @param standardwidth 标准(指定)宽度
     * @param standardheight 标准(指定)高度
     * @return 最小的合适比例
     */
    public static double getscaling(double targetwidth, double targetheight, double standardwidth, double standardheight) {
        double widthscaling = 0d;
        double heightscaling = 0d;
        if (targetwidth > standardwidth) {
            widthscaling = standardwidth / (targetwidth * 1.00d);
        } else {
            widthscaling = 1d;
        }
        if (targetheight > standardheight) {
            heightscaling = standardheight / (targetheight * 1.00d);
        } else {
            heightscaling = 1d;
        }
        return math.min(widthscaling, heightscaling);
    }
   
    /**
     * <b>function:</b> 将image的宽度、高度缩放到指定width、height,并保存在savepath目录
     * @author hoojo
     * @createdate 2012-2-6 下午04:54:35
     * @param width 缩放的宽度
     * @param height 缩放的高度
     * @param savepath 保存目录
     * @param targetimage 即将缩放的目标图片
     * @return 图片保存路径、名称
     * @throws imageformatexception
     * @throws ioexception
     */
    public static string resize(int width, int height, string savepath, image targetimage) throws imageformatexception, ioexception {
        width = math.max(width, 1);
        height = math.max(height, 1);
        bufferedimage image = new bufferedimage(width, height, bufferedimage.type_int_rgb);
        image.getgraphics().drawimage(targetimage, 0, 0, width, height, null);
       
        if (savepath == null || "".equals(savepath)) {
            savepath = default_file_path + system.currenttimemillis() + default_image_format;
        }
       
        fileoutputstream fos = new fileoutputstream(new file(savepath));
        jpegimageencoder encoder = jpegcodec.createjpegencoder(fos);
        encoder.encode(image);
 
        image.flush();
        fos.flush();
        fos.close();
       
        return savepath;
    }
   
    /**
     * <b>function:</b> 可以设置图片缩放质量,并且可以根据指定的宽高缩放图片
     * @author hoojo
     * @createdate 2012-2-7 上午11:01:27
     * @param width 缩放的宽度
     * @param height 缩放的高度
     * @param quality 图片压缩质量,最大值是1; 使用枚举值:{@link imagequality}
     *             some guidelines: 0.75 high quality、0.5  medium quality、0.25 low quality
     * @param savepath 保存目录
     * @param targetimage 即将缩放的目标图片
     * @return 图片保存路径、名称
     * @throws imageformatexception
     * @throws ioexception
     */
    public static string resize(int width, int height, float quality, string savepath, image targetimage) throws imageformatexception, ioexception {
        width = math.max(width, 1);
        height = math.max(height, 1);
        bufferedimage image = new bufferedimage(width, height, bufferedimage.type_int_rgb);
        image.getgraphics().drawimage(targetimage, 0, 0, width, height, null);
       
        if (savepath == null || "".equals(savepath)) {
            savepath = default_file_path + system.currenttimemillis() + default_image_format;
        }
       
        fileoutputstream fos = new fileoutputstream(new file(savepath));
        jpegimageencoder encoder = jpegcodec.createjpegencoder(fos);
       
        jpegencodeparam encodeparam = jpegcodec.getdefaultjpegencodeparam(image);
        if (quality == null || quality <= 0) {
            quality = default_scale_quality;
        }
        /** 设置图片压缩质量 */ 
        encodeparam.setquality(quality, true); 
        encoder.encode(image, encodeparam); 
 
        image.flush();
        fos.flush();
        fos.close();
       
        return savepath;
    }
   
    /**
     * <b>function:</b> 通过指定大小和图片的大小,计算出图片缩小的合适大小
     * @author hoojo
     * @createdate 2012-2-6 下午05:53:10
     * @param width 指定的宽度
     * @param height 指定的高度
     * @param image 图片文件
     * @return 返回宽度、高度的int数组
     */
    public static int[] getsize(int width, int height, image image) {
        int targetwidth = image.getwidth(null);
        int targetheight = image.getheight(null);
        double scaling = getscaling(targetwidth, targetheight, width, height);
        long standardwidth = math.round(targetwidth * scaling);
        long standardheight = math.round(targetheight * scaling);
        return new int[] { integer.parseint(long.tostring(standardwidth)), integer.parseint(string.valueof(standardheight)) };
    }
   
    /**
     * <b>function:</b> 通过指定的比例和图片对象,返回一个放大或缩小的宽度、高度
     * @author hoojo
     * @createdate 2012-2-7 上午10:27:59
     * @param scale 缩放比例
     * @param image 图片对象
     * @return 返回宽度、高度
     */
    public static int[] getsize(float scale, image image) {
        int targetwidth = image.getwidth(null);
        int targetheight = image.getheight(null);
        long standardwidth = math.round(targetwidth * scale);
        long standardheight = math.round(targetheight * scale);
        return new int[] { integer.parseint(long.tostring(standardwidth)), integer.parseint(string.valueof(standardheight)) };
    }
   
    public static int[] getsize(int width, image image) {
        int targetwidth = image.getwidth(null);
        int targetheight = image.getheight(null);
        long height = math.round((targetheight * width) / (targetwidth * 1.00f));
        return new int[] { width, integer.parseint(string.valueof(height)) };
    }
   
    public static int[] getsizebyheight(int height, image image) {
        int targetwidth = image.getwidth(null);
        int targetheight = image.getheight(null);
        long width = math.round((targetwidth * height) / (targetheight * 1.00f));
        return new int[] { integer.parseint(string.valueof(width)), height };
    }
   
    /**
     *
     * <b>function:</b> 将指定的targetfile图片文件的宽度、高度大于指定width、height的图片缩小,并保存在savepath目录
     * @author hoojo
     * @createdate 2012-2-6 下午04:57:02
     * @param width 缩小的宽度
     * @param height 缩小的高度
     * @param savepath 保存目录
     * @param targetimage 改变的目标图片
     * @return 图片保存路径、名称
     * @throws imageformatexception
     * @throws ioexception
     */
    public static string resize(int width, int height, string savepath, file targetfile) throws imageformatexception, ioexception {
        image = imageio.read(targetfile);
        int[] size = getsize(width, height, image);
        return resize(size[0], size[1], savepath, image);
    }
   
    /**
     *
     * <b>function:</b> 将指定的targeturl网络图片文件的宽度、高度大于指定width、height的图片缩小,并保存在savepath目录
     * @author hoojo
     * @createdate 2012-2-6 下午04:57:07
     * @param width 缩小的宽度
     * @param height 缩小的高度
     * @param savepath 保存目录
     * @param targetimage 改变的目标图片
     * @return 图片保存路径、名称
     * @throws imageformatexception
     * @throws ioexception
     */
    public static string resize(int width, int height, string savepath, url targeturl) throws imageformatexception, ioexception {
        image = imageio.read(targeturl);
        int[] size = getsize(width, height, image);
        return resize(size[0], size[1], savepath, image);
    }
   
    /**
     * <b>function:</b> 将一个本地的图片文件按照指定的比例进行缩放
     * @author hoojo
     * @createdate 2012-2-7 上午10:29:18
     * @param scale 缩放比例
     * @param savepath 保存文件路径、名称
     * @param targetfile 本地图片文件
     * @return 新的文件名称
     * @throws imageformatexception
     * @throws ioexception
     */
    public static string resize(float scale, string savepath, file targetfile) throws imageformatexception, ioexception {
        image = imageio.read(targetfile);
        int[] size = getsize(scale, image);
        return resize(size[0], size[1], savepath, image);
    }
   
    /**
     * <b>function:</b> 将一个网络图片文件按照指定的比例进行缩放
     * @author hoojo
     * @createdate 2012-2-7 上午10:30:56
     * @param scale 缩放比例
     * @param savepath 保存文件路径、名称
     * @param targetfile 本地图片文件
     * @return 新的文件名称
     * @throws imageformatexception
     * @throws ioexception
     */
    public static string resize(float scale, string savepath, url targeturl) throws imageformatexception, ioexception {
        image = imageio.read(targeturl);
        int[] size = getsize(scale, image);
        return resize(size[0], size[1], savepath, image);
    }
   
    /**
     * <b>function:</b> 按照固定宽度进行等比缩放本地图片
     * @author hoojo
     * @createdate 2012-2-7 上午10:49:56
     * @param width 固定宽度
     * @param savepath 保存路径、名称
     * @param targetfile 本地目标文件
     * @return 返回保存路径
     * @throws imageformatexception
     * @throws ioexception
     */
    public static string resize(int width, string savepath, file targetfile) throws imageformatexception, ioexception {
        image = imageio.read(targetfile);
        int[] size = getsize(width, image);
        return resize(size[0], size[1], savepath, image);
    }
   
    /**
     * <b>function:</b> 按照固定宽度进行等比缩放网络图片
     * @author hoojo
     * @createdate 2012-2-7 上午10:50:52
     * @param width 固定宽度
     * @param savepath 保存路径、名称
     * @param targetfile 本地目标文件
     * @return 返回保存路径
     * @throws imageformatexception
     * @throws ioexception
     */
    public static string resize(int width, string savepath, url targeturl) throws imageformatexception, ioexception {
        image = imageio.read(targeturl);
        int[] size = getsize(width, image);
        return resize(size[0], size[1], savepath, image);
    }
   
    /**
     *
     * <b>function:</b> 按照固定高度进行等比缩放本地图片
     * @author hoojo
     * @createdate 2012-2-7 上午10:51:17
     * @param height 固定高度
     * @param savepath 保存路径、名称
     * @param targetfile 本地目标文件
     * @return 返回保存路径
     * @throws imageformatexception
     * @throws ioexception
     */
    public static string resizebyheight(int height, string savepath, file targetfile) throws imageformatexception, ioexception {
        image = imageio.read(targetfile);
        int[] size = getsizebyheight(height, image);
        return resize(size[0], size[1], savepath, image);
    }
   
    /**
     * <b>function:</b> 按照固定高度进行等比缩放网络图片
     * @author hoojo
     * @createdate 2012-2-7 上午10:52:23
     * @param height 固定高度
     * @param savepath 保存路径、名称
     * @param targetfile 本地目标文件
     * @return 返回保存路径
     * @throws imageformatexception
     * @throws ioexception
     */
    public static string resizebyheight(int height, string savepath, url targeturl) throws imageformatexception, ioexception {
        image = imageio.read(targeturl);
        int[] size = getsizebyheight(height, image);
        return resize(size[0], size[1], savepath, image);
    }
   
    /**
     * <b>function:</b>
     * @author hoojo
     * @createdate 2012-2-3 上午10:08:47
     * @param args
     * @throws ioexception
     * @throws malformedurlexception
     * @throws imageformatexception
     */
    public static void main(string[] args) throws imageformatexception, malformedurlexception, ioexception {
       
        system.out.println(scaleimageutils.resize(140, 140, null, new url("http://www.open-open.com/lib/images/logo.jpg")));
        scaleimageutils.resize(100, 100, imagequality.high.getquality(), null, imageio.read(new url("http://www.open-open.com/lib/images/logo.jpg")));
    }
}

希望本文所述对大家的java程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网