解决java压缩图片透明背景变黑色的问题

 更新时间:2014年04月15日 09:01:36   作者:  
这篇文章主要介绍了解决java压缩图片透明背景变黑色的问题,需要的朋友可以参考下

复制代码 代码如下:

public class Picture { 
        // TODO Auto-generated constructor stub 
     public static void resizePNG(String fromFile, String toFile, int outputWidth, int outputHeight,boolean proportion) {
              try { 
               File f2 = new File(fromFile); 

                  BufferedImage bi2 = ImageIO.read(f2); 
               int newWidth;
              int newHeight;
           // 判断是否是等比缩放
           if (proportion == true) {
            // 为等比缩放计算输出的图片宽度及高度
            double rate1 = ((double) bi2.getWidth(null)) / (double) outputWidth + 0.1;
            double rate2 = ((double) bi2.getHeight(null)) / (double) outputHeight + 0.1;
            // 根据缩放比率大的进行缩放控制
            double rate = rate1 < rate2 ? rate1 : rate2;
            newWidth = (int) (((double) bi2.getWidth(null)) / rate);
            newHeight = (int) (((double) bi2.getHeight(null)) / rate);
           } else {
            newWidth = outputWidth; // 输出的图片宽度
            newHeight = outputHeight; // 输出的图片高度
           }
                  BufferedImage to = new BufferedImage(newWidth, newHeight, 

                          BufferedImage.TYPE_INT_RGB); 

                  Graphics2D g2d = to.createGraphics(); 

                  to = g2d.getDeviceConfiguration().createCompatibleImage(newWidth,newHeight, 

                          Transparency.TRANSLUCENT); 

                  g2d.dispose(); 

                  g2d = to.createGraphics(); 

                  Image from = bi2.getScaledInstance(newWidth, newHeight, bi2.SCALE_AREA_AVERAGING); 
                  g2d.drawImage(from, 0, 0, null);
                  g2d.dispose(); 

                  ImageIO.write(to, "png", new File(toFile)); 

              } catch (IOException e) { 

                  e.printStackTrace(); 

              } 

          } 

          public static void main(String[] args) throws IOException { 

              System.out.println("Start"); 

              resizePNG("C:\\Documents and Settings\\Administrator\\桌面\\8d9e9c82d158ccbf8b31059319d8bc3eb035414e.jpg", "C:\\Documents and Settings\\Administrator\\桌面\\ell.png",200, 100,true); 

              System.out.println("OK"); 

          } 
}

相关文章

  • SpringBoot整合Mybatis,解决TypeAliases配置失败的问题

    SpringBoot整合Mybatis,解决TypeAliases配置失败的问题

    这篇文章主要介绍了SpringBoot整合Mybatis,解决TypeAliases配置失败的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • Java之SpringBoot集成ActiveMQ消息中间件案例讲解

    Java之SpringBoot集成ActiveMQ消息中间件案例讲解

    这篇文章主要介绍了Java之SpringBoot集成ActiveMQ消息中间件案例讲解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • SpringBoot配置自定义拦截器实现过程详解

    SpringBoot配置自定义拦截器实现过程详解

    在系统中经常需要在处理用户请求之前和之后执行一些行为,例如检测用户的权限,或者将请求的信息记录到日志中,即平时所说的"权限检测"及"日志记录",下面这篇文章主要给大家介绍了关于在SpringBoot项目中整合拦截器的相关资料,需要的朋友可以参考下
    2022-10-10
  • 解决BeanUtils.copyProperties无法成功封装的问题

    解决BeanUtils.copyProperties无法成功封装的问题

    这篇文章主要介绍了解决BeanUtils.copyProperties无法成功封装的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • spring mvc使用@InitBinder标签对表单数据绑定的方法

    spring mvc使用@InitBinder标签对表单数据绑定的方法

    这篇文章主要介绍了spring mvc使用@InitBinder标签对表单数据绑定的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • Go Java算法重复的DNA序列详解

    Go Java算法重复的DNA序列详解

    这篇文章主要为大家介绍了Go Java算法之重复的DNA序列的示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • Java高级特性(基础)

    Java高级特性(基础)

    这篇文章主要介绍了Java高级特性(基础),需要的朋友可以参考下
    2017-04-04
  • 如何通过自定义spring invalidator注解校验数据合法性

    如何通过自定义spring invalidator注解校验数据合法性

    这篇文章主要介绍了如何通过自定义spring invalidator注解校验数据合法性,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • 5分钟搞定java单例模式

    5分钟搞定java单例模式

    单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式,本文给大家介绍下java单例模式的相关知识,感兴趣的朋友一起看看吧
    2022-03-03
  • 轻量级声明式的Http库——Feign的独立使用

    轻量级声明式的Http库——Feign的独立使用

    这篇文章主要介绍了轻量级声明式的Http库——Feign的使用教程,帮助大家更好的理解和学习使用feign,感兴趣的朋友可以了解下
    2021-04-04

最新评论