import java.awt.Graphics;
import java.awt.Image;
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.File;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;
import com.google.gson.Gson;
public class PictureVO
{
private static final Logger log = Logger.getLogger(PictureVO.class);
private int x;
private int y;
private int width;
private int height;
public PictureVO()
{
}
public PictureVO(int x, int y, int width, int height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public int getHeight()
{
return height;
}
public void setHeight(int height)
{
this.height = height;
}
public int getWidth()
{
return width;
}
public void setWidth(int width)
{
this.width = width;
}
public int getX()
{
return x;
}
public void setX(int x)
{
this.x = x;
}
public int getY()
{
return y;
}
public void setY(int y)
{
this.y = y;
}
public String cutpic(String srcImageFile, String descDir, int x1, int y1,
int w, int h)
{
Message msg=new Message();
msg.setCode("ERROR");
Gson gson=new Gson();
try
{
System.setProperty("java.awt.headless", "true");
Image img;
ImageFilter cropFilter;
// 读取源图像
BufferedImage bi = ImageIO.read(new File(srcImageFile));
int srcWidth = bi.getWidth(); // 源图宽度
int srcHeight = bi.getHeight(); // 源图高度
if (srcWidth >= w && srcHeight >= h)
{
Image image = bi.getScaledInstance(srcWidth, srcHeight,Image.SCALE_DEFAULT);
// 剪切起始坐标点
int wth = w;
int hth = h;
int xx = x1;
int yy = y1;
// 四个参数分别为图像起点坐标和宽高
// 即: CropImageFilter(int x,int y,int width,int height)
cropFilter = new CropImageFilter(xx, yy, wth, hth);
img = Toolkit.getDefaultToolkit().createImage(
new FilteredImageSource(image.getSource(), cropFilter));
BufferedImage tag = new BufferedImage(wth, hth,
BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, null); // 绘制缩小后的图
g.dispose();
// 输出为文件
ImageIO.write(tag, "JPEG", new File(descDir));
msg.setCode("S_OK");
msg.setVar("succuess");
}
else
{
msg.setCode("ERROR");
msg.setVar("你期望剪切的高度或宽度大于图片实际高宽度,图片将不能被裁减");
log.error("你期望剪切的高度或宽度大于图片实际高宽度,图片将不能被裁减");
return gson.toJson(msg);
}
}
catch (Exception e)
{
msg.setCode("ERROR");
msg.setVar("文件格式错误");
log.error("图片剪切现错误",e);
return gson.toJson(msg);
}
return gson.toJson(msg);
}
}
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
//import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;
import cn.richinfo.cmail.webmail.service.addr.model.Message;
//import com.sun.image.codec.jpeg.JPEGCodec;
//import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ZipPicProc{
private static final Logger log = Logger.getLogger(ZipPicProc.class);
public ZipPicProc(){
}
public void imageOp(String inFilePath, String outFilePath, int width, int height){
System.setProperty("java.awt.headless", "true");
File tempFile = new File(inFilePath);
log.error("infilepath="+inFilePath);
Image image = null;
try {
image = ImageIO.read(tempFile);
} catch (IOException e) {
log.error("file path is error");
}
int originalImageWidth = image.getWidth(null);
int originalImageHeight = image.getHeight(null);
log.error("1111");
BufferedImage originalImage = new BufferedImage(
originalImageWidth,
originalImageHeight,
BufferedImage.TYPE_3BYTE_BGR);
log.error("222");
Graphics2D g2d = originalImage.createGraphics();
g2d.drawImage(image, 0, 0, null);
BufferedImage changedImage =
new BufferedImage(
width,
height,
BufferedImage.TYPE_3BYTE_BGR);
double widthBo = (double)width/originalImageWidth;
double heightBo = (double)width/originalImageHeight;
AffineTransform transform = new AffineTransform();
transform.setToScale(widthBo, heightBo);
AffineTransformOp ato = new AffineTransformOp(transform, null);
ato.filter(originalImage, changedImage);
File fo = new File(outFilePath);
try {
ImageIO.write(changedImage, "jpeg", fo);
} catch (Exception e) {
e.printStackTrace();
}
}
public Message resizeImg(String imgsrc, String imgdist, int widthdist,
int heightdist)
{
Message msg=new Message();
try
{
System.setProperty("java.awt.headless", "true");
msg.setCode("S_OK");
File srcfile = new File(imgsrc);
if (!srcfile.exists())
{
msg.setCode("ERROR");
msg.setVar("图片不存在");
return msg;
}
Image src = javax.imageio.ImageIO.read(srcfile);
BufferedImage tag = new BufferedImage(widthdist, heightdist,
BufferedImage.TYPE_INT_RGB);
/*
* SCALE_SMOOTH:尺寸平滑 SCALE_AREA_AVERAGING:尺度区平均 SCALE_FAST:尺度快速
* SCALE_REPLICATE:尺度复制
*/
tag.getGraphics().drawImage(
src.getScaledInstance(widthdist, heightdist,
Image.SCALE_SMOOTH), 0, 0, null);
/*FileOutputStream out = new FileOutputStream(imgdist);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
*/
String type = imgsrc.substring(imgsrc.lastIndexOf(".")+1);
ImageIO.write(tag, type, new File(imgdist));
}
catch (IOException e)
{
log.error("cmd=zipImg | result=failure",e);
msg.setCode("ERROR");
msg.setVar("图片压缩错误");
}
return msg;
}
}