1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 解决java压缩图片透明背景变黑色的问题

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

时间:2021-02-26 19:08:43

相关推荐

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

Java|java教程

java 图片

Java-java教程

手机网站登陆源码下载,使用优盘安装ubuntu,tomcat怎么改服务名,通用爬虫处理url,前后端分离php上传文件,仙桃seo设计lzw

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.jpg",200, 100,true);System.out.println("OK"); } }

更多解决java压缩图片透明背景变黑色的问题相关文章请关注PHP中文网!

软件源码是什么,vscode搭建gtk环境,ubuntu目录操作,tomcat获取配置失败,网络爬虫和爬虫解决方案,php form跳转,肥西seo优化哪家靠谱,外卖网站php,易企秀怎么用别人的模板lzw

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。