java图片缩放剪切处理

 

package action;   
  
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 java.io.FileOutputStream;   
import java.io.IOException;   
  
import javax.imageio.ImageIO;   
  
import com.sun.image.codec.jpeg.JPEGCodec;   
import com.sun.image.codec.jpeg.JPEGImageEncoder;   
  
public class ImageProcess {   
  
    /**  
     * 对图片进行缩放  
     *   
     * @param srcImgFileName  
     * @throws IOException  
     */  
    public void zoomImage(String srcImgFileName) throws IOException {   
        // 读入文件   
        File _file = new File(srcImgFileName);   
        // 构造Image对象   
        BufferedImage src = javax.imageio.ImageIO.read(_file);   
        int width = src.getWidth();   
        int height = src.getHeight();   
  
        // 边长缩小为二分之一   
        BufferedImage tag = new BufferedImage(width / 2, height / 2, BufferedImage.TYPE_INT_RGB);   
        // 绘制缩小后的图   
        tag.getGraphics().drawImage(src, 0, 0, width / 2, height / 2, null);   
        FileOutputStream out = new FileOutputStream("D:\\test1\\targetIMG1-4.jpg");   
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);   
        encoder.encode(tag);   
        out.close();   
  
        // 边长扩大为2倍   
        tag = new BufferedImage(width * 2, height * 2, BufferedImage.TYPE_INT_RGB);   
        tag.getGraphics().drawImage(src, 0, 0, width * 2, height * 2, null);   
        out = new FileOutputStream("D:\\test1\\targetIMGx2.jpg");   
        encoder = JPEGCodec.createJPEGEncoder(out);   
        encoder.encode(tag);   
        out.close();   
  
    }   
  
    /**  
     * 将图片分成九块  
     *   
     * @param srcImageFile  
     * @throws IOException  
     */  
    public void cut(String srcImageFile) throws IOException {   
        Image img;   
        ImageFilter cropFilter;   
        String dir = null;   
        // 读取源图像   
        BufferedImage src = ImageIO.read(new File(srcImageFile));   
        int destWidth = src.getWidth() / 3;   
        int destHeight = src.getHeight() / 3;   
        // 循环   
        for (int i = 0; i < 3; i++) {   
            for (int j = 0; j < 3; j++) {   
                // 四个参数分别为图像起点坐标和宽高   
                cropFilter = new CropImageFilter(j * destWidth, i * destHeight, destWidth, destHeight);   
                img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(src.getSource(), cropFilter));   
                BufferedImage tag = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);   
                Graphics g = tag.getGraphics();   
                g.drawImage(img, 0, 0, null); // 绘制小图   
                g.dispose();   
                // 输出为文件   
                dir = "D:\\test1\\cut_image_" + i + "_" + j + ".jpg";   
                File f = new File(dir);   
                ImageIO.write(tag, "JPEG", f);   
            }   
        }   
    }   
  
    public static void main(String[] args) throws IOException {   
        String imgFileName = "D:\\test\\test.png";   
        ImageProcess iZoom = new ImageProcess();   
  
        iZoom.zoomImage(imgFileName);   
        iZoom.cut(imgFileName);   
    }   
}  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值