zoukankan      html  css  js  c++  java
  • java 图片裁剪代码

    package com.actionsoft.apps.addons.invoice.pc.test;

    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.Arrays;

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

    import jodd.log.Logger;
    import jodd.log.LoggerFactory;

    public class JavaTest {

    public static void main(String[] args) {
    // System.out.println(",1,2,3,4,5,张三,李四,".replaceAll("张三,",""));
    new JavaTest().cutImage("C:/Users/Administrator/Pictures/5.jpg", "D:/",200, 300, 400, 300);
    }

    private Logger log = LoggerFactory.getLogger(getClass());

    private static String DEFAULT_CUT_PREVFIX = "cut_";

    /**
    * Description: 依据原图与裁切size截取局部图片
    * @param srcImg 源图片
    * @param output 图片输出流
    * @param rect 须要截取部分的坐标和大小
    */
    public void cutImage(File srcImg, OutputStream output,java.awt.Rectangle rect) {
    if (srcImg.exists()) {
    java.io.FileInputStream fis = null;
    ImageInputStream iis = null;
    try {
    fis = new FileInputStream(srcImg);
    // ImageIO 支持的图片类型 : [BMP, bmp, jpg, JPG, wbmp, jpeg, png, PNG,
    // JPEG, WBMP, GIF, gif]
    String types = Arrays.toString(ImageIO.getReaderFormatNames())
    .replace("]", ",");
    String suffix = null;
    // 获取图片后缀
    if (srcImg.getName().indexOf(".") > -1) {
    suffix = srcImg.getName().substring(srcImg.getName().lastIndexOf(".") + 1);
    }// 类型和图片后缀所有小写。然后推断后缀是否合法
    if (suffix == null
    || types.toLowerCase().indexOf(suffix.toLowerCase() + ",") < 0) {
    log.error("Sorry, the image suffix is illegal. the standard image suffix is {}."+ types);
    return;
    }
    // 将FileInputStream 转换为ImageInputStream
    iis = ImageIO.createImageInputStream(fis);
    // 依据图片类型获取该种类型的ImageReader
    ImageReader reader = ImageIO.getImageReadersBySuffix(suffix).next();
    reader.setInput(iis, true);
    ImageReadParam param = reader.getDefaultReadParam();
    param.setSourceRegion(rect);
    BufferedImage bi = reader.read(0, param);
    ImageIO.write(bi, suffix, output);
    log.info("图片生成成功,请到文件夹下查看");
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    if (fis != null)
    fis.close();
    if (iis != null)
    iis.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    } else {
    log.warn("the src image is not exist.");
    }
    }


    //生成目标文件路径
    public void cutImage(File srcImg, String destImgPath,java.awt.Rectangle rect) {
    File destImg = new File(destImgPath);
    if (destImg.exists()) {
    String p = destImg.getPath();
    try {
    if (!destImg.isDirectory())
    p = destImg.getParent();
    if (!p.endsWith(File.separator))
    p = p + File.separator;
    cutImage(srcImg,new java.io.FileOutputStream(p + DEFAULT_CUT_PREVFIX+ "_"+ srcImg.getName()), rect);
    } catch (FileNotFoundException e) {
    log.warn("the dest image is not exist.");
    }
    } else
    log.warn("the dest image folder is not exist.");
    }


    public void cutImage(String srcImg, String destImg, int x, int y, int width,
    int height) {
    cutImage(new File(srcImg), destImg, new java.awt.Rectangle(x, y, width, height));
    }




    }

  • 相关阅读:
    mysql 创建数据库的基本操作
    MySQL的数据类型 及注意事项
    在执行 pip install 时遇到错误:python setup.py egg_info Check the logs for full command output
    python3.8-运行jupyter 报raise NotImplementedError
    执行python 爬虫脚本时提示bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?
    Python:lambda表达式和yield关键字理解与使用讲解
    百度paddle框架学习(二):使用经典VGG网络完成人脸口罩判别
    深度学习中的梯度
    C++ OpenCV学习笔记(持续更新)
    Tensorflow常见报错
  • 原文地址:https://www.cnblogs.com/renpei/p/11671881.html
Copyright © 2011-2022 走看看