首页 > 编程语言 > 详细

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

时间:2015-06-15 09:29:32      阅读:435      评论:0      收藏:0      [点我收藏+]

public static BufferedImage resize(int faceWidth,BufferedImage srcImg,HttpServletRequest request) throws IOException{
        
        int imgWidth = 0;
        if(faceWidth>70 && faceWidth<120){
            imgWidth = faceWidth*7/8;
        }else if(faceWidth>60 && faceWidth<70){
            imgWidth = faceWidth*11/10;
        }else if(faceWidth < 60){
            imgWidth = faceWidth*6/5;
        }else{
            imgWidth = faceWidth*4/5;
        }
        int imgHeight = imgWidth*srcImg.getHeight()/srcImg.getWidth();
        
        //构建新的图片
        BufferedImage resizedImg = new BufferedImage(imgWidth,imgHeight,BufferedImage.TYPE_INT_RGB);
        //将原图放大或缩小后画下来:并且保持png图片放大或缩小后背景色是透明的而不是黑色
        Graphics2D resizedG = resizedImg.createGraphics();
        resizedImg = resizedG.getDeviceConfiguration().createCompatibleImage(imgWidth,imgHeight,Transparency.TRANSLUCENT);
        resizedG.dispose();
        resizedG = resizedImg.createGraphics();
        Image from = srcImg.getScaledInstance(imgWidth, imgHeight, srcImg.SCALE_AREA_AVERAGING);
        resizedG.drawImage(from, 0, 0, null);
        resizedG.dispose();           
          
        return resizedImg;
    }

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

原文:http://blog.csdn.net/u012599724/article/details/46493727

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!