好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java实现的上传并压缩图片功能【可等比例压缩或原尺寸压缩】

本文实例讲述了java实现的上传并压缩图片功能。分享给大家供大家参考,具体如下:

先看效果:

原图:1.33m

处理后:27.4kb

关键代码:

?

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

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

package codegenerate.util;

import java.awt.color;

import java.awt.graphics2d;

import java.awt.image;

import java.awt.image.bufferedimage;

import java.io.file;

import java.io.filenotfoundexception;

import java.io.ioexception;

import javax.imageio.imageio;

public class imageziputil {

   public static void main(string[] args) {

     zipwidthheightimagefile( new file( "c:\\spider\\3.png" ), new file( "c:\\spider\\3-1.jpg" ), 425 , 638 , 0 .7f);

     //zipimagefile(new file("c:\\spider\\2.jpg"),new file("c:\\spider\\2-2.jpg"),425,638,0.7f);

     //zipimagefile(new file("c:\\spider\\3.jpg"),new file("c:\\spider\\3-3.jpg"),425,638,0.7f);

     system.out.println( "ok" );

   }

   /**

    * 根据设置的宽高等比例压缩图片文件<br> 先保存原文件,再压缩、上传

    * @param oldfile 要进行压缩的文件

    * @param newfile 新文件

    * @param width 宽度 //设置宽度时(高度传入0,等比例缩放)

    * @param height 高度 //设置高度时(宽度传入0,等比例缩放)

    * @param quality 质量

    * @return 返回压缩后的文件的全路径

    */

   public static string zipimagefile(file oldfile,file newfile, int width, int height, float quality) {

     if (oldfile == null ) {

       return null ;

     }

     try {

       /** 对服务器上的临时文件进行处理 */

       image srcfile = imageio.read(oldfile);

       int w = srcfile.getwidth( null );

       int h = srcfile.getheight( null );

       double bili;

       if (width> 0 ){

         bili=width/( double )w;

         height = ( int ) (h*bili);

       } else {

         if (height> 0 ){

           bili=height/( double )h;

           width = ( int ) (w*bili);

         }

       }

       string srcimgpath = newfile.getabsolutefile().tostring();

       system.out.println(srcimgpath);

       string subfix = "jpg" ;

       subfix = srcimgpath.substring(srcimgpath.lastindexof( "." )+ 1 ,srcimgpath.length());

       bufferedimage buffimg = null ;

       if (subfix.equals( "png" )){

         buffimg = new bufferedimage(width, height, bufferedimage.type_int_argb);

       } else {

         buffimg = new bufferedimage(width, height, bufferedimage.type_int_rgb);

       }

       graphics2d graphics = buffimg.creategraphics();

       graphics.setbackground( new color( 255 , 255 , 255 ));

       graphics.setcolor( new color( 255 , 255 , 255 ));

       graphics.fillrect( 0 , 0 , width, height);

       graphics.drawimage(srcfile.getscaledinstance(width, height, image.scale_smooth), 0 , 0 , null );

       imageio.write(buffimg, subfix, new file(srcimgpath));

     } catch (filenotfoundexception e) {

       e.printstacktrace();

     } catch (ioexception e) {

       e.printstacktrace();

     }

     return newfile.getabsolutepath();

   }

   /**

    * 按设置的宽度高度压缩图片文件<br> 先保存原文件,再压缩、上传

    * @param oldfile 要进行压缩的文件全路径

    * @param newfile 新文件

    * @param width 宽度

    * @param height 高度

    * @param quality 质量

    * @return 返回压缩后的文件的全路径

    */

   public static string zipwidthheightimagefile(file oldfile,file newfile, int width, int height, float quality) {

     if (oldfile == null ) {

       return null ;

     }

     string newimage = null ;

     try {

       /** 对服务器上的临时文件进行处理 */

       image srcfile = imageio.read(oldfile);

       string srcimgpath = newfile.getabsolutefile().tostring();

       system.out.println(srcimgpath);

       string subfix = "jpg" ;

       subfix = srcimgpath.substring(srcimgpath.lastindexof( "." )+ 1 ,srcimgpath.length());

       bufferedimage buffimg = null ;

       if (subfix.equals( "png" )){

         buffimg = new bufferedimage(width, height, bufferedimage.type_int_argb);

       } else {

         buffimg = new bufferedimage(width, height, bufferedimage.type_int_rgb);

       }

       graphics2d graphics = buffimg.creategraphics();

       graphics.setbackground( new color( 255 , 255 , 255 ));

       graphics.setcolor( new color( 255 , 255 , 255 ));

       graphics.fillrect( 0 , 0 , width, height);

       graphics.drawimage(srcfile.getscaledinstance(width, height, image.scale_smooth), 0 , 0 , null );

       imageio.write(buffimg, subfix, new file(srcimgpath));

     } catch (filenotfoundexception e) {

       e.printstacktrace();

     } catch (ioexception e) {

       e.printstacktrace();

     }

     return newimage;

   }

}

说明:

1、根据需求大家可以自行设置质量参数 quality ,到底设置成多少,可以先看下效果在取值;

2、网上通用的方法用的是jdk自带jar包中方法,我这里衍生了一下:用 graphics2d ,能够同时处理jpg和png格式;

3、 new color(255,255,255) 是白色,等同于white,但是用white 的话,linux下某些图片会有其它色值;

4、main中的宽425和高638可以根据自己的需求自行设置,但是对于长和宽一样的,按照400(小值的值425)*400来处理;

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

原文链接:https://blog.csdn.net/xb12369/article/details/51121504

查看更多关于Java实现的上传并压缩图片功能【可等比例压缩或原尺寸压缩】的详细内容...

  阅读:48次