最近在一个项目中需要做一个上传图片的接口,由于后台系统只能识别jpg,所以研究了一下java对图片的处理。
下面就是自己研究的java对图片的压缩(缩放)和图片的格式转换(jpg转png)。
package com.billy.utils;import javax.imageio.ImageIO;import java.awt.*;import java.awt.image.BufferedImage;import java.io.File;public class ImageUtils {public static void main(String[] args) {String imagePath = "D:\\压缩图片\\压缩后图片\\3hamburger-01.png";if (isBlank(imagePath)) {System.err.println("图片文件路径不能为空");return;}File file = new File(imagePath);if (!file.exists() || file.isDirectory()) {System.err.println("图片文件不存在");return;}String suffix = getFileSuffix(file);if (!isImageSuffix(suffix)) {System.err.println("不是有效的图片文件");return;}convertPngToJpg(file);scale(file, 0.5f);}/*** 将png图片转为jpg图片*/public static void convertPngToJpg(File file) {String suffix = getFileSuffix(file);if (!"png".equalsIgnoreCase(suffix)) {System.err.println("不是有效的png图片文件");return;}String fileName = getFileName(file);try {BufferedImage bufferedImage = ImageIO.read(file);// 创建一个rgb格式的空图片BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);// 将原图画到创建的空图片中,并设置背景色为白色newBufferedImage.getGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null);File outFile = new File(file.getParentFile(), fileName + "-convert.jpg");ImageIO.write(newBufferedImage, "jpg", outFile);System.out.println("转换图片(png->jpg):" + file + " -> " + outFile);} catch (Exception e) {e.printStackTrace();}}/*** 缩放图片** @param scaleRatio 缩放比。缩小:<1.0,放大:>1.0*/public static void scale(File file, float scaleRatio) {String fileName = getFileName(file);try {BufferedImage bufferedImage = ImageIO.read(file);int newWidth = Math.round(bufferedImage.getWidth() * scaleRatio);int newHeight = Math.round(bufferedImage.getHeight() * scaleRatio);// 创建一个rgb格式的空图片BufferedImage newBufferedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);// 获得缩放后的原图Image srcScaledImage = bufferedImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);// 将缩放后的原图画到创建的空图片中,并设置背景色为白色newBufferedImage.getGraphics().drawImage(srcScaledImage, 0, 0, Color.WHITE, null);File outFile = new File(file.getParentFile(), fileName + "-scale.jpg");ImageIO.write(newBufferedImage, "jpg", outFile);System.out.println("缩放图片(" + scaleRatio + "):" + file + " -> " + outFile);} catch (Exception e) {e.printStackTrace();}}public static boolean isImageSuffix(String suffix) {if (isBlank(suffix)) {return false;}suffix = suffix.toLowerCase();return "jpg".equals(suffix) || "jpeg".equals(suffix) || "png".equals(suffix);}/*** 获取文件的文件名(不含后缀)。*/public static String getFileName(File file) {String name = file.getName();int lastIndexOf = name.lastIndexOf(".");if (lastIndexOf < 0) {return name;}return name.substring(0, lastIndexOf);}/*** 获取文件的文件后缀*/public static String getFileSuffix(File file) {String name = file.getName();int lastIndexOf = name.lastIndexOf(".");if (lastIndexOf < 0) {return "";}return name.substring(lastIndexOf + 1);}public static boolean isBlank(String s) {if (s == null || s.length() == 0) {return true;}return s.trim().length() == 0;}}