maven依赖:

    1. <dependency>
    2. <groupId>com.madgag</groupId>
    3. <artifactId>animated-gif-lib</artifactId>
    4. <version>1.2</version>
    5. </dependency>

    尺寸压缩:

    1. /** * @param imagePath 原图片路径地址,如:F:\\a.png * @param imgStyle 目标文件类型
    2. * gif类型图片尺寸压缩
    3. * @param width 目标文件宽
    4. * @param height 目标文件高
    5. * @param outputPath 输出文件路径(不带后缀),如:F:\\b,默认与原图片路径相同,为空时将会替代原文
    6. * @throws IOException
    7. */
    8. public static void zoomGifBySize(String imagePath,int width, int height, String outputPath) throws IOException {
    9. //GIF需要特殊处理
    10. GifDecoder decoder = new GifDecoder(); int status = decoder.read(imagePath);
    11. if (status != GifDecoder.STATUS_OK) {
    12. throw new IOException("read image " + imagePath + " error!");
    13. }
    14. //拆分一帧一帧的压缩之后合成
    15. AnimatedGifEncoder encoder = new AnimatedGifEncoder();
    16. encoder.start(outputPath);
    17. encoder.setRepeat(decoder.getLoopCount());
    18. for (int i = 0; i < decoder.getFrameCount(); i++) {
    19. encoder.setDelay(decoder.getDelay(i));
    20. //设置播放延迟时间
    21. BufferedImage bufferedImage = decoder.getFrame(i);
    22. //获取每帧BufferedImage流
    23. BufferedImage zoomImage = new BufferedImage(width, height, bufferedImage.getType());
    24. Image image = bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
    25. Graphics gc = zoomImage.getGraphics(); gc.setColor(Color.WHITE);
    26. gc.drawImage(image, 0, 0, null);
    27. encoder.addFrame(zoomImage);
    28. }
    29. encoder.finish();
    30. File outFile = new File(outputPath);
    31. BufferedImage image = ImageIO.read(outFile);
    32. ImageIO.write(image, outFile.getName(), outFile);
    33. }

    质量压缩:

    1. /**
    2. * @param imagePath 原图片路径地址,如:F:\\a.png * @param imgStyle 目标文件类型
    3. * @param quality 输出的图片质量,范围:0.0~1.0,1为最高质量。
    4. * @param outputPath 输出文件路径(不带后缀),如:F:\\b,默认与原图片路径相同,为空时将会替代原文件
    5. * @throws IOException
    6. */
    7. public void zoomGifByQuality(String imagePath, float quality, String outputPath) throws IOException {
    8. //GIF需要特殊处理
    9. GifDecoder decoder = new GifDecoder();
    10. int status = decoder.read(imagePath);
    11. if (status != GifDecoder.STATUS_OK) {
    12. throw new IOException("read image " + imagePath + " error!");
    13. }
    14. //拆分一帧一帧的压缩之后合成
    15. AnimatedGifEncoder encoder = new AnimatedGifEncoder();
    16. encoder.start(outputPath);
    17. //设置合成位置
    18. encoder.setRepeat(decoder.getLoopCount());
    19. //设置GIF重复次数
    20. int frameCount = decoder.getFrameCount();
    21. //获取GIF有多少个frame
    22. for (int i = 0; i < frameCount; i++) {
    23. encoder.setDelay(decoder.getDelay(i));
    24. //设置GIF延迟时间
    25. BufferedImage bufferedImage = decoder.getFrame(i);
    26. //利用java SDK压缩BufferedImage
    27. byte[] tempByte = zoomBufferedImageByQuality(bufferedImage, quality, outputPath);
    28. ByteArrayInputStream in = new ByteArrayInputStream(tempByte);
    29. BufferedImage zoomImage = ImageIO.read(in);
    30. encoder.addFrame(zoomImage);// 合成
    31. }
    32. encoder.finish();
    33. File outFile = new File(outputPath);
    34. BufferedImage image = ImageIO.read(outFile);
    35. ImageIO.write(image, outFile.getName(), outFile);
    36. }
    37. /**
    38. * 压缩gif
    39. * @param bufferedImage
    40. * @param quality
    41. * @param path
    42. * @return
    43. * @throws IOException
    44. */
    45. public byte[] zoomBufferedImageByQuality(BufferedImage bufferedImage, float quality, String path) throws IOException {
    46. // 得到指定Format图片的writer
    47. Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");// 得到迭代器
    48. ImageWriter writer = (ImageWriter) iter.next(); // 得到writer
    49. // 得到指定writer的输出参数设置(ImageWriteParam)
    50. ImageWriteParam iwp = writer.getDefaultWriteParam();
    51. iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); // 设置可否压缩
    52. iwp.setCompressionQuality(quality); // 设置压缩质量参数,0~1,1为最高质量
    53. iwp.setProgressiveMode(ImageWriteParam.MODE_DISABLED);
    54. ColorModel colorModel = ColorModel.getRGBdefault();
    55. // 指定压缩时使用的色彩模式
    56. iwp.setDestinationType(new ImageTypeSpecifier(colorModel, colorModel.createCompatibleSampleModel(16, 16)));
    57. // 开始打包图片,写入byte[]
    58. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); // 取得内存输出流
    59. IIOImage iIamge = new IIOImage(bufferedImage, null, null);
    60. // 此处因为ImageWriter中用来接收write信息的output要求必须是ImageOutput
    61. // 通过ImageIo中的静态方法,得到byteArrayOutputStream的ImageOutput
    62. writer.setOutput(ImageIO.createImageOutputStream(byteArrayOutputStream));
    63. writer.write(null, iIamge, iwp);
    64. // 获取压缩后的btye
    65. byte[] tempByte = byteArrayOutputStream.toByteArray();
    66. // 创建输出文件,outputPath输出文件路径,imgStyle目标文件格式(png)
    67. File outFile = new File(path);
    68. FileOutputStream fos = new FileOutputStream(outFile);
    69. fos.write(tempByte);
    70. fos.close();
    71. return tempByte;
    72. }