博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 图像处理两例:图像缩放与圆角图片的制作
阅读量:5890 次
发布时间:2019-06-19

本文共 2281 字,大约阅读时间需要 7 分钟。

hot3.png

package test;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ChangeImage {
    public static BufferedImage makeRoundedCorner(BufferedImage image,
            int cornerRadius) {
        int w = image.getWidth();
        int h = image.getHeight();
        BufferedImage output = new BufferedImage(w, h,
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = output.createGraphics();
        // This is what we want, but it only does hard-clipping, i.e. aliasing
        // g2.setClip(new RoundRectangle2D ...)
        // so instead fake soft-clipping by first drawing the desired clip shape
        // in fully opaque white with antialiasing enabled...
        g2.setComposite(AlphaComposite.Src);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(Color.WHITE);
        g2.fill(new RoundRectangle2D.Float(0, 0, w, h, cornerRadius,
                cornerRadius));
        // ... then compositing the image on top,
        // using the white shape from above as alpha source
        g2.setComposite(AlphaComposite.SrcAtop);
        g2.drawImage(image, 0, 0, null);
        g2.dispose();
        return output;
    }
    public static BufferedImage createResizedCopy(Image originalImage, int scaledWidth,
            int scaledHeight, boolean preserveAlpha) {
        int imageType = preserveAlpha ? BufferedImage.TYPE_INT_RGB
                : BufferedImage.TYPE_INT_ARGB;
        BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight,
                imageType);
        Graphics2D g = scaledBI.createGraphics();
        if (preserveAlpha) {
            g.setComposite(AlphaComposite.Src);
        }
        g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null);
        g.dispose();
        return scaledBI;
    }
    public static void main(String[] args) throws IOException {
        BufferedImage icon = ImageIO.read(new File("/home/june/桌面/mm.jpg"));
        BufferedImage rounded = makeRoundedCorner(icon, 30);
        ImageIO.write(rounded, "png", new File("/home/june/桌面/mm_rounded.png"));
        
        BufferedImage pic = ImageIO.read(new File("/home/june/桌面/zhang.jpg"));
        BufferedImage resized = createResizedCopy(pic, 250, 250*768/1024, true);
        ImageIO.write(resized, "jpg", new File("/home/june/桌面/zhang_small.jpg"));
    }
}

转载于:https://my.oschina.net/u/937886/blog/122529

你可能感兴趣的文章
CAP原理中的一致性
查看>>
TMSScripter语法
查看>>
控制钱箱,客显,打印
查看>>
步步为营 .NET 代码重构学习笔记 二、提炼方法(Extract Method)
查看>>
步步为营 C# 技术漫谈 七、事务处理(Transaction)
查看>>
AutoMapper在MVC中的运用03-字典集合、枚举映射,自定义解析器
查看>>
lodash用法系列(1),数组集合操作
查看>>
【Tomcat】Tomcat 系统架构与设计模式,第 2 部分: 设计模式分析
查看>>
cxrichedit导入WORD
查看>>
ArcGIS模型构建器案例教程-批量修改工作空间所有要素类的空间参考
查看>>
用GibbsLDA做Topic Modeling
查看>>
linux驱动面试题目汇总
查看>>
csharp skype send message in winform
查看>>
jQuery plugin: Tablesorter 2.0
查看>>
csharp:datagridview enter Half Width and Full Width characters
查看>>
MMORPG 游戏服务器端设计--转载
查看>>
C#实现无标题栏窗体点击任务栏图标正常最小化或还原的解决方法
查看>>
[转]GetLastInputInfo计时用户离开电脑及软件在指定时间锁定等
查看>>
Windows 操作系统与 .NET Framework
查看>>
Box2dの自定义多边形
查看>>