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

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

原创:找图网 2023-05-03 17:44:30
  • java图片压缩比为1

  • java压缩图片,按照比例进行压缩

    public static void main(String[] args) {

    try {

    //图片所在路径

    BufferedImage templateImage = (new File("C:\\Users\\晏丁丁\\Pictures\\图片1.png"));

    //原始图片的长度和宽度

    int height = ();

    int width = ();

    //通过比例压缩

    float scale = 0.5f;

    //通过固定长度压缩

    /*int doWithHeight = 100;

    int dowithWidth = 300;*/

    //压缩之后的长度和宽度

    int doWithHeight = (int) (scale * height);

    int dowithWidth = (int) (scale * width);

    BufferedImage finalImage = new BufferedImage(dowithWidth, doWithHeight, _INT_RGB);

    ().drawImage((dowithWidth, doWithHeight, _SMOOTH), 0, 0, null);

    //图片输出路径,以及图片名

    FileOutputStream fileOutputStream = new FileOutputStream("D:/image/");

    JPEGImageEncoder encoder = (fileOutputStream);

    (finalImage);

    ();

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    文章知

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

  • java要实现把一个大图片压缩到指定大小的图片且长宽比不变可以尝试以下操作:

    建立一个AffineTransform

    AffineTransform(double m00, double m10, double m01, double m11, double m02, double m12)

    转换矩阵,缩放比较简单(矩阵可以干很多事情,想做图像处理软件可以研究下)

    [ x'] [ m00 m01 m02 ] [ x ] [ m00x + m01y + m02 ]

    [ y'] = [ m10 m11 m12 ] [ y ] = [ m10x + m11y + m12 ]

    [ 1 ] [ 0 0 1 ] [ 1 ] [ 1 ]

    10倍比较难算(根号10啊,当然你想算也行),9倍好点(9的开方是3),m00为1/3,m01为0,m02为0,m10为0,m11为1/3,m12为0。

    再建一个AffineTransformOp,把上面的转换传进去

    AffineTransformOp(AffineTransform xform, int interpolationType)

    最后调用AffineTransformOp的BufferedImage filter(BufferedImage src, BufferedImage dst) ,src传原图片,返回值就是想要的Image,注意是返回值,不是dst,不明白可以看下Java API

  • 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