96编辑器旗下产品
找图
终身
ID:0
到期时间:永久
退出登录
您的位置:首页 > 百科管理 > 详情

java压缩图片thumbnails-求助java压缩图片存储大小的方法

原创:找图网 2023-05-03 01:05:48
  • 求一个Java无损压缩图片的示例,把原图片复制到指定目录,按原图比例改变尺寸,不影响图片质量。

  • 一般都是大尺寸变小尺寸,肯定有损的。

    不知道你的无损是什么意思,意思是变小尺寸了还能变大也像以前一样清晰?

    好像有种“矢量图片”就是可以随意放大缩小并且无损,缺点就是图片很占空间。

  • 求助java压缩图片存储大小的方法

  • 可以使用Draw这个类,通过改变像素来改变存储大小,实例如下:

    public static boolean compressPic(String srcFilePath, String descFilePath) throws IOException {

            File file = null;

            BufferedImage src = null;

            FileOutputStream out = null;

            ImageWriter imgWrier;

            ImageWriteParam imgWriteParams;

            // 指定写图片的方式为 jpg

            imgWrier = ("jpg").next();

            imgWriteParams = new (

                    null);

            // 要使用压缩,必须指定压缩方式为MODE_EXPLICIT

            (_EXPLICIT);

            // 这里指定压缩的程度,参数qality是取值0~1范围内,

            ((float) 1);

            (_DISABLED);

            ColorModel colorModel =(new File(srcFilePath)).getColorModel();// ();

            // 指定压缩时使用的色彩模式

    //        (new (

    //                colorModel, (16, 16)));

            (new (

                    colorModel, (16, 16)));

            try {

                if (isBlank(srcFilePath)) {

                    return false;

                } else {

                    file = new File(srcFilePath);(());

                    src = (file);

                    out = new FileOutputStream(descFilePath);

                    ();

                    // 必须先指定 out值,才能调用write方法, ImageOutputStream可以通过任何

                    // OutputStream构造

                    ((out));

                    // 调用write方法,就可以向输入流写图片

                    (null, new IIOImage(src, null, null),

                            imgWriteParams);

                    ();

                    ();

                }

            } catch (Exception e) {

                e.printStackTrace();

                return false;

            }

            return true;

        }

    public static boolean isBlank(String string) {

            if (string == null || () == 0 || ().equals("")) {

                return true;

            }

            return false;

        }

  • java如何实现把一个大图片压缩到指定大小的图片且长宽比不变

  • 也就是图片压缩,可以不修改任何大小的压缩(速度快),也可等比例修改大小压缩(较慢)

    下面这是一段等比例缩小图片的方法。

    public String compressPic(String inputDir, String outputDir,

    String inputFileName, String outputFileName, int width,

    int height, boolean gp,String hzm) {

    try {

    if (!()) {

    return "";

    }

    Image img = (image);

    // 判断图片格式是否正确

    if ((null) == -1) {

    return "no";

    } else {

    int newWidth; int newHeight;

    // 判断是否是等比缩放

    if (gp == true) {

    // 为等比缩放计算输出的图片宽度及高度

    double rate1 = ((double) (null)) / (double) width ;

    double rate2 = ((double) (null)) / (double) height ;

    // 根据缩放比率大的进行缩放控制

    double rate = rate1 > rate2 ? rate1 : rate2;

    newWidth = (int) (((double) (null)) / rate);

    newHeight = (int) (((double) (null)) / rate);

    } else {

    newWidth = (null); // 输出的图片宽度

    newHeight = (null); // 输出的图片高度

    }

    BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, _INT_RGB);

    /*

    * _SMOOTH 的缩略算法 生成缩略图片的平滑度的

    * 优先级比速度高 生成的图片质量比较好 但速度慢

    */

    Image im = (newWidth, newHeight, _SMOOTH);

    ().drawImage(im, 0, 0, null);

    FileOutputStream out = new FileOutputStream(outputDir + outputFileName);

    //JPEGImageEncoder可适用于其他图片类型的转换

    JPEGImageEncoder encoder = (out);

    (tag);

    ();

    }

    } catch (IOException ex) {

    ();

    }

    return "ok";

    }

    最新文章 更多
    最新素材 更多
    公司介绍 网站地图 图片资讯
    Copyright © 2010-2022 山东找图网络科技有限公司  鲁ICP备18007836号-3  邮箱:15653358549@163.com