发布于 2016-07-12 06:04:45 | 142 次阅读 | 评论: 1 | 来源: 网友投递

这里有新鲜出炉的Java设计模式,程序狗速度看过来!

Java程序设计语言

java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE(j2ee), JavaME(j2me), JavaSE(j2se))的总称。


这篇文章主要介绍了解决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");  

          } 
}



最新网友评论  共有(1)条评论 发布评论 返回顶部
zpgppy 发布于2016-08-14 19:28:29
我的怎么没有用呢
支持(0)  反对(0)  回复

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务