Java截取图片

/**
 * <p>Title:ImageUtils.java </p>
 * <p>Description: 照片处理类</p>
 * <p>Copyright: Copyright (c) 2010</p>
 * <p>Company: </p>
 * @date 2010-08-12
 * @author ZongShuai
 * @version 3.0
*/


package com.jadlsoft.utils;


import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;


import javax.imageio.ImageIO;


import com.jadlsoft.domain.rygl.RyzpBean;


import sun.misc.BASE64Encoder;


public class ImageUtils {

/**
* 剪切图像
* @param inputStream 上传图像流
* @param ryzpBean 截图参数bean
* @return 图片的字节数组
* @throws IOException
*/



public static String cutImage(InputStream imgInputStream, int w, int h, int x1, int y1, int sw,
int sh) throws IOException {
Image img;
ImageFilter cropFilter;
// 读取源图像
BufferedImage bi = ImageIO.read(imgInputStream);
String returnValue = "";
if (sw >= w && sh >= h) {
Image image = bi.getScaledInstance(sw, sh, Image.SCALE_DEFAULT);
// 剪切起始坐标点
int x = x1;
int y = y1;
int destWidth = w; // 切片宽度
int destHeight = h; // 切片高度
// 图片比例
double pw = sw;
double ph = sh;
double m = (double) sw / pw;
double n = (double) sh / ph;
int wth = (int) (destWidth * m);
int hth = (int) (destHeight * n);
int xx = (int) (x * m);
int yy = (int) (y * n);
cropFilter = new CropImageFilter(xx, yy, wth, hth);
img = Toolkit.getDefaultToolkit().createImage(
new FilteredImageSource(image.getSource(), cropFilter));


BufferedImage tag = new BufferedImage(150, 219,
BufferedImage.TYPE_INT_RGB);


Graphics2D g = (Graphics2D) tag.getGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(img, 0, 0, 150, 219, null); // 绘制剪切后的图
g.dispose();


ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(tag, "JPEG", baos);
returnValue = (new BASE64Encoder().encode(baos.toByteArray()));


baos.close();
}
return returnValue;
}



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值