java图片压缩比为1

Python017

java图片压缩比为1,第1张

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

public static void main(String[] args) {

try {

//图片所在路径

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

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

int height = templateImage.getHeight()

int width = templateImage.getWidth()

//通过比例压缩

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, BufferedImage.TYPE_INT_RGB)

finalImage.getGraphics().drawImage(templateImage.getScaledInstance(dowithWidth, doWithHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null)

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

FileOutputStream fileOutputStream = new FileOutputStream("D:/image/tupian.jpg")

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fileOutputStream)

encoder.encode(finalImage)

fileOutputStream.close()

} 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

文章知

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

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

public String compressPic(String inputDir, String outputDir,

String inputFileName, String outputFileName, int width,

int height, boolean gp,String hzm) {

try {

if (!image.exists()) {

return ""

}

Image img = ImageIO.read(image)

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

if (img.getWidth(null) == -1) {

return "no"

} else {

int newWidthint newHeight

// 判断是否是等比缩放

if (gp == true) {

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

double rate1 = ((double) img.getWidth(null)) / (double) width

double rate2 = ((double) img.getHeight(null)) / (double) height

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

double rate = rate1 >rate2 ? rate1 : rate2

newWidth = (int) (((double) img.getWidth(null)) / rate)

newHeight = (int) (((double) img.getHeight(null)) / rate)

} else {

newWidth = img.getWidth(null)// 输出的图片宽度

newHeight = img.getHeight(null)// 输出的图片高度

}

BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB)

/*

* Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的

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

*/

Image im = img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH)

tag.getGraphics().drawImage(im, 0, 0, null)

FileOutputStream out = new FileOutputStream(outputDir + outputFileName)

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

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out)

encoder.encode(tag)

out.close()

}

} catch (IOException ex) {

ex.printStackTrace()

}

return "ok"

}

 可以使用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 = ImageIO.getImageWritersByFormatName("jpg").next()

        imgWriteParams = new javax.imageio.plugins.jpeg.JPEGImageWriteParam(

                null)

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

        imgWriteParams.setCompressionMode(imgWriteParams.MODE_EXPLICIT)

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

        imgWriteParams.setCompressionQuality((float) 1)

        imgWriteParams.setProgressiveMode(imgWriteParams.MODE_DISABLED)

        ColorModel colorModel =ImageIO.read(new File(srcFilePath)).getColorModel()// ColorModel.getRGBdefault()

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

//        imgWriteParams.setDestinationType(new javax.imageio.ImageTypeSpecifier(

//                colorModel, colorModel.createCompatibleSampleModel(16, 16)))

        imgWriteParams.setDestinationType(new javax.imageio.ImageTypeSpecifier(

                colorModel, colorModel.createCompatibleSampleModel(16, 16)))

        try {

            if (isBlank(srcFilePath)) {

                return false

            } else {

                file = new File(srcFilePath)System.out.println(file.length())

                src = ImageIO.read(file)

                out = new FileOutputStream(descFilePath)

                imgWrier.reset()

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

                // OutputStream构造

                imgWrier.setOutput(ImageIO.createImageOutputStream(out))

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

                imgWrier.write(null, new IIOImage(src, null, null),

                        imgWriteParams)

                out.flush()

                out.close()

            }

        } catch (Exception e) {

            e.printStackTrace()

            return false

        }

        return true

    }

public static boolean isBlank(String string) {

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

            return true

        }

        return false

    }