图片剪切【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);
	}

}

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值