Java 图片裁切及放大缩小

编程技术  /  houtizong 发布于 3年前   227

   Java 图片裁切及放大缩小, 仅供参考, 要学会举一反三, 其它问题欢迎交流...

 

import java.awt.Color;import java.awt.Graphics2D;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;/** * @author Colin Davis ( [email protected] ) * */public final class DealImage {private File folder;public static final String[] ACCEPT_IMG_TYPES = { ".png", ".jpg", ".jpeg" };public DealImage(String folder) {this.folder = new File(folder);}public void DealImages() throws IOException {for ( File file : this.folder.listFiles() ) {if ( file.isFile() && this.IsAcceptImage(file) ) {this.Write2File(this.Img2Square(file, 200), this.FileSuffix(file), file.getAbsolutePath(), "square");}}}public boolean IsAcceptImage(File file) {boolean accept = false;String fileName = file.getName();for ( String type : ACCEPT_IMG_TYPES ) {if ( fileName.endsWith(type) ) {accept = true;break;}}return accept;}public void Cut2Square(final File imgFile, final int sideLen) throws IOException {String formatName = FileSuffix(imgFile);Image img = this.Img2Square(imgFile, sideLen);BufferedImage bufImg = new BufferedImage(sideLen, sideLen, BufferedImage.TYPE_INT_RGB);Graphics2D g2d = bufImg.createGraphics(); // ** 创建画板g2d.drawImage(img, 0, 0, sideLen, sideLen, null); // ** 把 img(图像) 画到指定位置(x:0, y:0)指定区域(x:sideLen + 0, y:sideLen + 0)g2d.dispose();String destFile = imgFile.getAbsolutePath() + ".square" + formatName;ImageIO.write(bufImg, formatName.substring(1), new File(destFile));}/** * 缩小, 缩小比例为 图片大小(宽或高)/sideLen */private BufferedImage Img2Small(File imgFile, final int sideLen) throws IOException {String formatName = FileSuffix(imgFile);if (formatName.toLowerCase().equals(".jpeg")) {imgFile = RenameJpeg2Jpg(imgFile);formatName = ".jpg";}BufferedImage img = ImageIO.read(imgFile);int imgWidth = img.getWidth(), imgHeight = img.getHeight(), newWidth = sideLen, newHeight = sideLen;if ( imgHeight > sideLen || imgWidth > sideLen ) {float scale = ( imgWidth > imgHeight ? imgWidth * 0.1f / sideLen : imgHeight * 0.1f / sideLen ) + 0.1f;newWidth = (int) (imgWidth * scale);newHeight = (int) (imgHeight * scale);}BufferedImage bi = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);Graphics2D g2d = bi.createGraphics();g2d.drawImage(img, 0, 0, newWidth, newHeight, null);g2d.dispose();return bi;}/** * 缩小后到图片变成正方形, 用白色补齐空白区域 */private BufferedImage Img2Square(File imgFile, int sideLen) throws IOException {Image img = this.Img2Small(imgFile, sideLen);int imgWidth = img.getWidth(null), imgHeight = img.getHeight(null);sideLen = imgWidth > imgHeight ? imgWidth : imgHeight;BufferedImage bufImg = new BufferedImage(sideLen, sideLen, BufferedImage.TYPE_INT_RGB);Graphics2D g2d = bufImg.createGraphics();g2d.setColor(Color.WHITE);g2d.fillRect(0, 0, sideLen, sideLen);int toX = ( sideLen - imgWidth ) / 2, toY = ( sideLen - imgHeight ) / 2;g2d.drawImage(img, toX, toY, imgWidth, imgHeight, null);g2d.dispose();return bufImg;}public File RenameJpeg2Jpg(File jpegFile) throws IOException {String imgName = jpegFile.getName();int idx = imgName.lastIndexOf(".");File destFile = new File(jpegFile.getParentFile(), imgName.substring(0, idx) + ".jpg");jpegFile.renameTo(destFile);return destFile;}public String FileSuffix(File file) {int idx = file.getName().lastIndexOf(".");return idx > 0 ? file.getName().substring(idx) : "";}public void Write2File(final BufferedImage img, String fmt, String path, String append) throws IOException {String outFile = path + "." + append + fmt;ImageIO.write(img, fmt.substring(1), new File(outFile));}public static void main(String[] args) throws IOException {DealImage si = new DealImage("/home/cpu/images/");si.DealImages();}}

 

上一篇:Java MD5
下一篇:收集互联网博客

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客