图片截取

文章底部有所需的jar包。
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

public class CutPicture {
    public static void main(String[] args) throws IOException {
        String orsrc="F:\\The originar file\\00000001.tif";
        BufferedImage sourceImg=ImageIO.read(new File(orsrc));
        //System.out.println(sourceImg);
        int width=sourceImg.getWidth();
        int height=sourceImg.getHeight();
        System.out.println(width+"\n"+height);
        orsrc=orsrc.replaceAll("\\\\", "/");
        int lastindex= orsrc.lastIndexOf("/");
        String srcone= orsrc.substring(0,lastindex+1);
        String srctwo= orsrc.substring(lastindex+1,orsrc.length());
        String fusrc=srcone+"cope"+srctwo;
        boolean blog= cutPic(orsrc,fusrc, 0, 0, width, height/10);
        System.out.println(blog);
    }
    /** 
     * @param srcFile源文件 
     * @param outFile输出文件 
     * @param x坐标 
     * @param y坐标 
     * @param width宽度 
     * @param height高度 
     * @return 
     * @描述 —— 裁剪图片 
     */  
    public static boolean cutPic(String srcFile, String outFile, int x, int y,  
            int width, int height) {  
        FileInputStream is = null;  
        ImageInputStream iis = null;  
        try {  
            // 如果源图片不存在  
            if (!new File(srcFile).exists()) {  
                return false;  
            }  
  
            // 读取图片文件  
            is = new FileInputStream(srcFile);  
  
            // 获取文件格式  
            String ext = srcFile.substring(srcFile.lastIndexOf(".") + 1);  
  
            // ImageReader声称能够解码指定格式  
            Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(ext);  
            ImageReader reader = it.next();  
  
            // 获取图片流  
            iis = ImageIO.createImageInputStream(is);  
  
            // 输入源中的图像将只按顺序读取  
            reader.setInput(iis, true);  
  
            // 描述如何对流进行解码  
            ImageReadParam param = reader.getDefaultReadParam();  
  
            // 图片裁剪区域  
            Rectangle rect = new Rectangle(x, y, width, height);  
  
            // 提供一个 BufferedImage,将其用作解码像素数据的目标  
            param.setSourceRegion(rect);  
  
            // 使用所提供的 ImageReadParam 读取通过索引 imageIndex 指定的对象  
            BufferedImage bi = reader.read(0, param);  
  
            // 保存新图片  
            File tempOutFile = new File(outFile);  
            if (!tempOutFile.exists()) {  
                tempOutFile.mkdirs();  
            }  
            ImageIO.write(bi, ext, new File(outFile));  
            return true;  
        } catch (Exception e) {  
            e.printStackTrace();  
            return false;  
        } finally {  
            try {  
                if (is != null) {  
                    is.close();  
                }  
                if (iis != null) {  
                    iis.close();  
                }  
            } catch (IOException e) {  
                e.printStackTrace();  
                return false;  
            }  
        }  
    }
    public void ss()throws Exception {
        /*FileOutputStream out = new FileOutputStream("e:/a.png");
        ImageDecoder decoder = ImageCodec.createImageDecoder("tiff",new File("e:/a.tif"),null);
        ImageEncoder encoder = ImageCodec.createImageEncoder("png",out,null);
        encoder.encode( decoder.decodeAsRenderedImage() );;
        out.close();*/
    }
}
<点击下载所需的jar包>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值