当前位置:首页 > 开发 > 编程语言 > Java > 正文

Java 图片裁切及放大缩小

发表于: 2015-03-09   作者:Colin_Davis   来源:转载   浏览次数:
摘要:    Java 图片裁切及放大缩小, 仅供参考, 要学会举一反三, 其它问题欢迎交流...   import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File;

   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();
	}
	
}

 

Java 图片裁切及放大缩小

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
JQuery支持图片放大、缩小: 一、JQuery支持图片放大、缩小: 今天在"懒人之家"看到JQuery支持图片放
一、放大、缩小图片 private LinearLayout imageLayout; private ImageView imageView; private But
JQuery支持图片放大、缩小: 一、JQuery支持图片放大、缩小: 今天在"懒人之家"看到JQuery支持图片放
JQuery支持图片放大、缩小: 一、JQuery支持图片放大、缩小: 今天在"懒人之家"看到JQuery支持图片放
/Files/Snowfun/zoomer.for.jquery-1.0.zip <%@ Page Language= " C# " AutoEventWireup= " true
运用Matrix对象来缩放图片文件 范例说明 手机的功能越来越强大,相机功能俨然成为目前手机的标准配备
There are times when this will not always be the case and you need to stretch or shrink the i
Matrix ,中文里叫矩阵,高等数学里有介绍,在图像处理方面,主要是用于平面的缩放、平移、旋转等操
ZoomControls控件是一个可以缩放但控件,效果如下图 以下是它但一些主要但方法 hasFocus ():判断焦
Android多点触控 Matrix图片随意的放大缩小,拖动 一、新建一个项目AnroidImageDragAndZoom,并准备
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号