java 剪切 压缩 图片

剪切图片

package com.ad.web.servlet.userimg.imgprocess.cutimg;
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;
public class cutPic
{

     private int x;

     private int y;


     private int width;

     private int height;

     public cutPic()
     {

     }

     public cutPic(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 void cutpic(String srcImageFile, String descDir, int x1, int y1,
       int w, int h)
     {

   System.setProperty("java.awt.headless", "true");  //linux上要加上这一句

      try
      {
       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 x = x1;
        int y = y1;
        int destWidth = w; // 切片宽度
        int destHeight = h; // 切片高度
        // 图片比例
        double pw=480.0;
        double ph=360.0;
        double m = (double) srcWidth / pw;
        double n = (double) srcHeight / ph;
      
        int wth = (int) (destWidth * m);
        int hth = (int) (destHeight * n);
        int xx = (int) (x * m);
        int yy = (int) (y * n);
        // 四个参数分别为图像起点坐标和宽高
        // 即: 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));
       } else
       {
             System.out.println("Oh,你期望剪切的高度或宽度大于图片实际高宽度,图片将不能被裁减。");
       }
      } catch (Exception e)
      {
       e.printStackTrace();
      }
     }
    
    
     public static void main(String[] args) throws Exception
     {

      String name = "D:\\1.jpeg";
      String descdir="D:\\2.gif";
      cutPic cut=new cutPic();
      cut.cutpic(name, descdir, 0, 0, 100, 100);
     }
}

二压缩图片

package com.ad.web.servlet.userimg.imgprocess.zipimg;
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.FileNotFoundException;   
import java.io.IOException;   
import java.io.InputStream;   
 
import javax.imageio.ImageIO;   
 
 
public class zipPic{   
       
    public zipPic(){   
           
    }   
       
    @SuppressWarnings("unused")
    public void imageOp(InputStream inFile, String outFilePath, int width, int height){   
        Image image = null;   
        try {   
            image = ImageIO.read(inFile);   
        } catch (IOException e) {   
            System.out.println("file path error...");   
        }   
           
        int originalImageWidth = image.getWidth(null);   
        int originalImageHeight = image.getHeight(null);   
           
        BufferedImage originalImage = new BufferedImage(   
                originalImageWidth,   
               originalImageHeight,   
               BufferedImage.TYPE_3BYTE_BGR);   
        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 void imageOp(String inFilePath, String outFilePath, int width, int height){   
       File tempFile = new File(inFilePath);   
        Image image = null;   
        try {   
           image = ImageIO.read(tempFile);   
        } catch (IOException e) {   
           System.out.println("file path error...");   
       }   
           
        int originalImageWidth = image.getWidth(null);   
        int originalImageHeight = image.getHeight(null);   
           
        BufferedImage originalImage = new BufferedImage(   
                originalImageWidth,   
                originalImageHeight,   
                BufferedImage.TYPE_3BYTE_BGR);   
        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 static void main(String[] args) throws FileNotFoundException {   
        zipPic t1 = new zipPic();   
        t1.imageOp("D:/2.jpg", "D:/2.jpg", 100, 100);
 
    }   
       


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值