?? Java 图片裁切及放大缩小, 仅供参考, 要学会举一反三, 其它问题欢迎交流...
?
class="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 ( nilocsivad@hotmail.com ) * */ 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(); } }
?