前言
前些时候,我在某个网站上注册了一个账号,在修改个人头像时,发现该网站仅支持矩形头像的显示,但是我个人是比较喜欢圆形头像的;
因此我需要将电脑上一张矩形图片处理成白底圆形的图片,但是我找了好多在线方法(ps:不太喜欢给电脑上下载许多不常用的工具),发现
都不太好用,于是决定看能否用代码来实现这个需求,因此有了本文。
构思
在Java语言中,其实有一个操作图像的类库,就是 BufferedImage
。
Image 是一个抽象类,BufferedImage 是 Image 的实现类,是一个操作缓冲区图像的类。BufferedImage 的主要作用是将一张图片加载到内存中,
然后我们就可以实现对图像的一些操作。比如:获得绘图对象、图像缩放、选择图像平滑度、图片大小变换、改变图片显示颜色、设置透明度等功能。
实现
package com.abc.learn;
import org.apache.commons.io.FilenameUtils; import org.junit.Test; import sun.misc.BASE64Encoder; import javax.imageio.ImageIO; import java.awt.*; import java.awt.geom.Ellipse2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.net.URL; import java.nio.charset.StandardCharsets; import java.time.Instant;
public class ToCirclePicture {
private static final String localImgPath = "C:/Users/Administrator/Pictures/4K/312591.jpg";
private static final String netImgUrl = "https://pic.imgdb.cn/item/613e1f7c44eaada739eb4a11.jpg";
public static BufferedImage changeToCircular(BufferedImage bufferedImage, int radius) {
BufferedImage destImg = new BufferedImage(radius, radius, BufferedImage.TYPE_4BYTE_ABGR); Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, radius, radius); Graphics2D g2 = destImg.createGraphics(); g2.setClip(shape); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.drawImage(bufferedImage, 0, 0, null); g2.setBackground(Color.white); g2.dispose(); return destImg; }
public static BufferedImage scaleByPercentage(BufferedImage sourceImage, int newWidth, int newHeight) { int type = sourceImage.getColorModel().getTransparency(); int width = sourceImage.getWidth(); int height = sourceImage.getHeight(); RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); BufferedImage img = new BufferedImage(newWidth, newHeight, type); Graphics2D graphics2d = img.createGraphics(); graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics2d.drawImage(sourceImage, 0, 0, newWidth, newHeight, 0, 0, width, height, null); graphics2d.dispose(); return img; }
public static String getBase64(String str) {
String destStr = ""; try { byte[] sourceArray = str.getBytes(StandardCharsets.UTF_8); destStr = new BASE64Encoder().encode(sourceArray);
} catch (Exception e) { e.printStackTrace(); } return destStr; }
@Test public void testBase64() { String oldStr = "hello java"; String newStr = getBase64(oldStr); System.out.println(newStr); }
@Test public void testLocalImg() { try { BufferedImage sourceImg = ImageIO.read(new FileInputStream(localImgPath)); BufferedImage destImg = scaleByPercentage(sourceImg, sourceImg.getWidth(), sourceImg.getHeight()); File outputFile = new File(FilenameUtils.getBaseName(localImgPath) + "-副本-" + Instant.now().toEpochMilli() + ".png"); ImageIO.write(destImg, "png", outputFile); System.out.println("转换图片完成"); } catch (Exception e) { e.printStackTrace(); } }
@Test public void testNetUrlImg() { try { BufferedImage sourceImg = ImageIO.read(new URL(netImgUrl)); BufferedImage destImg = scaleByPercentage(sourceImg, sourceImg.getWidth(), sourceImg.getHeight()); String name = FilenameUtils.getBaseName(netImgUrl); File outputFile = new File(name + "-副本-" + Instant.now().toEpochMilli() + ".png"); ImageIO.write(destImg, "png", outputFile); System.out.println("转换图片完成"); } catch (Exception e) { e.printStackTrace(); } }
@Test public void testLocalImgToCircular() { try { BufferedImage sourceImg = ImageIO.read(new File(localImgPath)); BufferedImage destImg = changeToCircular(sourceImg, sourceImg.getHeight());
File outputFile = new File(FilenameUtils.getBaseName(localImgPath) + "-副本-圆形" + Instant.now().toEpochMilli() + ".png"); ImageIO.write(destImg, "png", outputFile);
System.out.println("图片已裁剪成圆形"); } catch (Exception e) { e.printStackTrace(); } }
@Test public void testUtils() { String str = FilenameUtils.getName(localImgPath); String baseName = FilenameUtils.getBaseName(localImgPath); System.out.println(str); System.out.println(baseName); }
@Test public void testNewTime() { Instant instant = Instant.now(); System.out.println(instant.getEpochSecond()); System.out.println(instant.toEpochMilli()); System.out.println(instant.getNano()); }
}
|
特别注意上面的裁剪成圆形图像的方法,最后输出的文件格式要是png的才可以,至于原因我暂时不清楚。
文章参考:https://blog.csdn.net/jiachunchun/article/details/89670721