Java缩放图片及剪裁圆形图标代码
来源:互联网 发布:完整id查询软件 编辑:程序博客网 时间:2024/06/06 17:07
剪裁圆形图标对生成png格式有效,对jpg无效,代码如下:
import java.awt.AlphaComposite;import java.awt.Color;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Image;import java.awt.RenderingHints;import java.awt.geom.RoundRectangle2D;import java.awt.image.BufferedImage;import java.awt.image.ConvolveOp;import java.awt.image.Kernel;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import javax.imageio.ImageIO;import javax.swing.ImageIcon;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGEncodeParam;import com.sun.image.codec.jpeg.JPEGImageEncoder;/** * <b> </b> * <p> * 功能: * </p> * * @作者 stone * @创建时间 2016年7月19日 下午2:40:59 * @修改内容 * @修改时间 */public class ImageUtil { /** * 重新设置图像大小 * @param sfile 图片源文件 * @param tfile 目标文件 * @param newWidth 新宽度 * @param newHeight 新高度 * @param quality 质量系数 * @throws IOException */ public static void resizeImg(String sfile, String tfile, int newWidth, int newHeight, float quality) throws IOException { if(quality > 1) { throw new IllegalArgumentException( "Quality has to be between 0 and 1"); } if(newWidth <=0 || newHeight<=0) { throw new IllegalArgumentException( "Width or Height must greater than zero"); } //This code ensures that all the pixels in the image are loaded. Image srcImg = new ImageIcon(sfile).getImage(); Image resizedImage = srcImg.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH); // This code ensures that all the pixels in the image are loaded. Image temp = new ImageIcon(resizedImage).getImage(); // Create the buffered image. BufferedImage bufferedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB); // Copy image to buffered image. Graphics g = bufferedImage.createGraphics(); // Clear background and paint the image. g.setColor(Color.white); g.fillRect(0, 0, newWidth, newHeight); g.drawImage(temp, 0, 0, null); g.dispose(); // Soften. float softenFactor = 0.05f; float[] softenArray = { 0, softenFactor, 0, softenFactor, 1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 }; Kernel kernel = new Kernel(3, 3, softenArray); ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); bufferedImage = cOp.filter(bufferedImage, null); // Write the jpeg to a file. FileOutputStream out = null; try { out = new FileOutputStream(new File(tfile)); // Encodes image as a JPEG data stream JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder .getDefaultJPEGEncodeParam(bufferedImage); param.setQuality(quality, true); encoder.setJPEGEncodeParam(param); encoder.encode(bufferedImage); } catch (IOException e) { e.printStackTrace(); throw e; } finally { if (out != null) { out.close(); } } } public static void makeRoundedCornerImg(String sfile, String tfile, int cornerRadius) throws IOException { BufferedImage image = ImageIO.read(new File(sfile)); 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(); String filetype = tfile.substring(tfile.lastIndexOf(".")+1); try { ImageIO.write(output, filetype, new File(tfile)); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { String filepath = "D:/img/"; ImageUtil.resizeImg(filepath+"1.png", filepath+"1-256-256-1.png", 256, 256, 1.0f); ImageUtil.resizeImg(filepath+"1.png", filepath+"1-256-256-2.png", 256, 256, 0.7f); ImageUtil.resizeImg(filepath+"2.png", filepath+"2-256-256.png", 256, 256, 1.0f); ImageUtil.resizeImg(filepath+"3.jpg", filepath+"3-256-256.jpg", 256, 256, 1.0f); ImageUtil.makeRoundedCornerImg(filepath+"2-256-256.png", filepath+"2-256-256-90.png", 90); ImageUtil.makeRoundedCornerImg(filepath+"2-256-256.png", filepath+"2-256-256-180.png", 180); ImageUtil.makeRoundedCornerImg(filepath+"2-256-256.png", filepath+"2-256-256-360.png", 360); ImageUtil.makeRoundedCornerImg(filepath+"3-256-256.jpg", filepath+"3-256-256-360.png", 360); } }
0 0
- Java缩放图片及剪裁圆形图标代码
- Android--自定义View加载网络图片,实现缩放,圆形剪裁
- 图片剪裁成圆形
- Android Matrix的用法--图片的缩放及剪裁
- 将图片剪裁为圆形
- c#缩放和剪裁图片
- Java图片缩放代码
- php图片处理代码分享,包括缩放、剪裁、缩放、翻转、旋转、透明、锐化等
- java代码对图片缩放
- Java 图片剪裁
- CGContext对缩放图片的剪裁
- 动态缩放图片,剪裁指定位置大小
- 图片处理类(剪裁、缩放、水印)
- 图片处理类(剪裁、缩放、水印)
- 图片处理 缩放与圆形
- C#图片处理之:图片缩放和剪裁
- C#图片处理之:图片缩放和剪裁
- C#图片处理之:图片缩放和剪裁
- Android事件传递机制-View篇
- 位置无关码、位置有关码、链接地址、加载地址
- 【Android界面实现】实现底部扇形展开菜单效果
- 深度学习的方法、书籍、资料、网站
- 总结2
- Java缩放图片及剪裁圆形图标代码
- JS中字符串与字符数组相互转化
- Linux 文件目录介绍
- 用户头像bitmap保存入本地,避免重复从网上get
- c++ tips
- java中内存的主要划分
- linux网络基本之ip命令
- win7 设置启用remote control 设置方法
- 剑指offer——第一个只出现一次的字符