zoukankan      html  css  js  c++  java
  • 图片截取(裁剪)

        public static void cutImage(File file) {
            String fileName = file.getPath();
            String lastName = fileName.substring(fileName.lastIndexOf(".") + 1,fileName.length());
            FileInputStream fis = null;
            ImageInputStream iis = null;
            try {
                /**读取图片*/
                Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(lastName);
                ImageReader reader = it.next();
                /**获取图片流*/
                fis = new FileInputStream(file);
                iis = ImageIO.createImageInputStream(fis);
                reader.setInput(iis, true);

                ImageReadParam param = reader.getDefaultReadParam();
                BufferedImage image = null;
                image = ImageIO.read(file);
                int width = image.getWidth();
                int height = image.getHeight();
                Rectangle rect;
                if (300 < height) {
                    rect = new Rectangle(0, 0, width, height-50);
                } else {
                    rect = new Rectangle(0, 0, width, height);
                }
                param.setSourceRegion(rect);
                BufferedImage bi = reader.read(0, param);

                ImageIO.write(bi, lastName, file);
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if (fis != null) {
                        fis.close();
                    }
                    if (iis != null) {
                        iis.close();
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }

  • 相关阅读:
    LOJ#6501. 「雅礼集训 2018 Day4」Cube 题解
    LOJ#6510. 「雅礼集训 2018 Day8」A 题解
    LOJ#6513. 「雅礼集训 2018 Day10」足球大战 题解
    LOJ#6507. 「雅礼集训 2018 Day7」A 题解
    LOJ#6038. 「雅礼集训 2017 Day5」远行 题解
    Luogu P4208 [JSOI2008]最小生成树计数
    CodeForces 916D Jamie and To-do List
    CodeForces 573B Bear and Blocks
    CodeForces 460C Present
    CodeForces 786B Legacy
  • 原文地址:https://www.cnblogs.com/sand-tiny/p/3582794.html
Copyright © 2011-2022 走看看