import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*;图片的缩放
public static BufferedImage scale(BufferedImage imageToScale, int dWidth, int dHeight) {
BufferedImage scaledImage = null;
if (imageToScale != null) {
scaledImage = new BufferedImage(dWidth, dHeight, imageToScale.getType());
Graphics2D graphics2D = scaledImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(imageToScale, 0, 0, dWidth, dHeight, null);
graphics2D.dispose();
}
return scaledImage;
}
byte数组转BufferedImage
public static BufferedImage byteToBufferedImage(byte[] b) {
try {
ByteArrayInputStream in = new ByteArrayInputStream(b);
return ImageIO.read(in);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
BufferedImage转byte数组
public static byte[] bufferedImageToByte(BufferedImage image) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write(image, "jpg", out);
} catch (IOException e) {
e.printStackTrace();
}
return out.toByteArray();
}
保存byte数组为图片到指定的路径
public static void saveByteToImage(byte[] b, String filepath) {
try {
FileOutputStream fileOutputStream = new FileOutputStream(filepath);
fileOutputStream.write(b);
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
BufferedImage转InputStream
public static InputStream bufferedImageToInputStream(BufferedImage image) {
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", os);
return new ByteArrayInputStream(os.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
保存BufferedImage到指定的路径
public static void saveBufferedImage(BufferedImage image, String filepath) {
try {
File file = new File(filepath);
ImageIO.write(image, "jpg", file);
} catch (IOException e) {
e.printStackTrace();
}
}
创作不易,喜欢的话加个关注点个赞,❤谢谢谢谢❤



