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

Java裁剪压缩PNG图片 透明背景色变黑问题解决

时间:2023-09-03 09:18:18

相关推荐

Java裁剪压缩PNG图片 透明背景色变黑问题解决

package com.gblfy.test;import java.awt.Graphics2D;import java.awt.Image;import java.awt.Transparency;import java.awt.image.BufferedImage;import java.io.File;import javax.imageio.ImageIO;/*** 图片工具类*/public class ImageUtil {/*** 裁剪PNG图片工具类** @param fromFile源文件* @param toFile 裁剪后的文件* @param outputWidth 裁剪宽度* @param outputHeight 裁剪高度* @param proportion 是否是等比缩放*/public static void resizePng(File fromFile, File toFile, int outputWidth, int outputHeight,boolean proportion) {try {BufferedImage bi2 = ImageIO.read(fromFile);int newWidth;int newHeight;// 判断是否是等比缩放if (proportion) {// 为等比缩放计算输出的图片宽度及高度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();@SuppressWarnings("static-access")Image from = bi2.getScaledInstance(newWidth, newHeight, bi2.SCALE_AREA_AVERAGING);g2d.drawImage(from, 0, 0, null);g2d.dispose();ImageIO.write(to, "png", toFile);} catch (Exception e) {e.printStackTrace();}}/*** 测试*/public static void main(String[] args) throws Exception {File fromFile = new File("D:\\22\\iconPath\\22.png");File toFile = new File("D:\\22\\iconPath\\2255555.png");resizePng(fromFile, toFile, 100, 100, false);//根据实际图片大小修改 水印图片的大小 动态适配// resizePng(fromFile, toFile, (int) (1244 * 0.71), (int) (1684 * 0.18), false);}}

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