图片剪切【JAVA-后台】

package com.*.*.web.utils;

import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class CutImageUtil {
	/**
	 * 重写头像剪切方法(判断剪切的像素是否大于原大小,大于则按照原图剪切)
	 * @param srcImageFile
	 * @param result
	 * @param x
	 * @param y
	 * @param width
	 * @param scaleWidth
	 */
	public static final void cutScaleSquare(String srcImageFile, String result, int x, int y, int width, int scaleWidth)
	  {
	    try
	    {
	      Image imageSrc = Toolkit.getDefaultToolkit().getImage(srcImageFile);
	      BufferedImage bi = toBufferedImage(imageSrc);

	      String imgType = srcImageFile
	        .substring(srcImageFile
	        .lastIndexOf(".") + 
	        1);
	      int srcWidth = bi.getHeight();//原图高
	      int srcHeight = bi.getWidth();//原图宽
	      if ((srcWidth > 0) && (srcHeight > 0))
	      {
	    	if(width > srcWidth || width > srcHeight){
	    		if(srcWidth < srcHeight){
	    			bi = bi.getSubimage(x, y, srcWidth, width);
	    		} else {
	    			bi = bi.getSubimage(x, y, srcHeight, width);
	    		}
	    		
	    	} else {
	    		bi = bi.getSubimage(x, y, width, width);
	    	}
	        

	        double ratio = 0.0D;

	        BufferedImage tempImg = null;
	        if (bi.getWidth() > scaleWidth)
	        {
	          ratio = new Integer(scaleWidth).doubleValue() / bi
	            .getWidth();

	          AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), 
	            null);

	          tempImg = op.filter(bi, null);
	        } else {
	          tempImg = bi;
	        }
	        ImageIO.write(tempImg, imgType, new File(result));
	      }
	    }
	    catch (Exception e) {
	      e.printStackTrace();
	    }
	  }


	 public static final BufferedImage toBufferedImage(Image image)
	  {
	    if ((image instanceof BufferedImage)) {
	      return (BufferedImage)image;
	    }

	    image = new ImageIcon(image).getImage();
	    BufferedImage bimage = null;

	    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
	    try {
	      int transparency = 1;
	      GraphicsDevice gs = ge.getDefaultScreenDevice();
	      GraphicsConfiguration gc = gs.getDefaultConfiguration();
	      bimage = gc.createCompatibleImage(image.getWidth(null), image
	        .getHeight(null), 
	        transparency);
	    }
	    catch (HeadlessException localHeadlessException) {
	    }
	    if (bimage == null)
	    {
	      int type = 1;

	      bimage = new BufferedImage(image.getWidth(null), image
	        .getHeight(null), 
	        type);
	    }

	    Graphics g = bimage.createGraphics();

	    g.drawImage(image, 0, 0, null);
	    g.dispose();
	    return bimage;
	  }
	 
	public static void main(String[] args) {
		String str1 = "F:/workspace/.metadata/.plugins/"
				+ "org.eclipse.wst.server.core/tmp0/wtpwebapps/*/tmp/201801/1516331770849TpXeEO.jpeg";
		String str2 = "F:/workspace/.metadata/.plugins/"
						+ "org.eclipse.wst.server.core/*/userphoto//pss\20180119\1516331830897pGpqkn.jpg";
		cutScaleSquare(str1, str2, 0, 16, 462, 80);
	}

}

*交流分享,杜绝私藏。崇尚开源


你可能感兴趣的:(until)