Java之图片裁剪工具类-yellowcong
来源:互联网 发布:装修设计效果图软件 编辑:程序博客网 时间:2024/06/01 08:33
对于图片,我们需要做的大致有,图片裁剪,添加水印和文字的操作,图片裁剪工具这个只包含了图片裁剪,后面的水印功能我会陆续添加上来
图片裁剪工具
package com.yellowcong.utils;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import javax.imageio.ImageIO;import net.coobird.thumbnailator.Thumbnails;import net.coobird.thumbnailator.geometry.Positions;import org.apache.commons.io.FilenameUtils;public class ImageUtils { private ImageUtils(){} /** * 获取图片宽度 * @param file * @return */ public static int getImageWidth(File file){ try { BufferedImage img =ImageIO.read(file); return img.getWidth(); } catch (Exception e) { e.printStackTrace(); } return 0; } /** * 获取图片宽度 * @param file * @return */ public static int getImageWidth(InputStream in){ try { BufferedImage img =ImageIO.read(in); return img.getWidth(); } catch (Exception e) { e.printStackTrace(); } return 0; } /** * 获取图片的高度 * @param file 传入图片文件对象 * @return */ public static int getImageHeight(File file){ try { BufferedImage img =ImageIO.read(file); return img.getHeight(); } catch (Exception e) { e.printStackTrace(); } return 0; } /** * 获取图片的高度 * @param in 传入图片文件对象 * @return */ public static int getImageHeight(InputStream in ){ try { BufferedImage img =ImageIO.read(in); return img.getHeight(); } catch (Exception e) { e.printStackTrace(); } return 0; } /** * 传入图片的输入流,然后根据给定的图片的宽度和高度来生成新的图片 宽度和高度 * @param in * @param width * @param height * @param path 需要写出的路径 * @return */ public static boolean getNewImage(InputStream in,String path,int width,int height){ boolean flag = false; BufferedImage img =null; BufferedImage buff = null; try { img = ImageIO.read(in); double oldHeight = img.getHeight(); double oldWidth = img.getWidth(); double scaleWidth; double scaleHeight; //判断图片是否合适 if(oldWidth>width && oldHeight>height){ //判断是否 小于宽度的1.2,当高度大于原本的 一点点就不管了,直接将图片写出, 然后再截图 if(oldHeight <1.2*height && oldWidth <1.2*width){ buff = Thumbnails.of(img).scale((((double)width)/oldWidth)).asBufferedImage(); flag = true; } //方案一,按照宽度缩放,宽度肯定是一是 if(!flag){ buff = Thumbnails.of(img).scale((((double)width)/oldWidth)*1.2).asBufferedImage(); scaleHeight = buff.getHeight(); if(scaleHeight < height){ flag = false; }else{ flag = true; } } //方案二, 按照高度来写数据, 判断高度是否 合适 if(!flag){ buff = Thumbnails.of(img).scale((((double)height)/oldHeight)*1.2).asBufferedImage(); scaleWidth = buff.getWidth(); if(scaleWidth <width){ flag = false; }else{ flag = true; } } //写出图片 if(true){ buff = Thumbnails.of(buff).sourceRegion(Positions.CENTER,width,height).scale(1.0D).asBufferedImage(); ImageIO.write(buff, FilenameUtils.getExtension(path), new File(path)); } }else{ //图片 大小不合适 flag = false; } } catch (Exception e) { throw new RuntimeException("图片生成失败"); } return flag; } /** * 传递一个文件对象 来生成一个一定规格的图片 ,根据图片的宽度 , 修改图片生成 策略 * @param file * @param width * @param height * @return */ public static boolean getNewImage(File file,int width,int height){ boolean flag = false; try { BufferedImage img =ImageIO.read(new FileInputStream(file)); BufferedImage buff = null; double oldHeight = img.getHeight(); double oldWidth = img.getWidth(); double scaleWidth; double scaleHeight; //判断图片是否合适 if(oldWidth>width && oldHeight>height){ //判断是否 小于宽度的1.2,当高度大于原本的 一点点就不管了,直接将图片写出, 然后再截图 if(oldHeight <1.2*height && oldWidth <1.2*width){ buff = Thumbnails.of(img).scale((((double)width)/oldWidth)).asBufferedImage(); flag = true; } //方案一,按照宽度缩放,宽度肯定是一是 if(!flag){ buff = Thumbnails.of(img).scale((((double)width)/oldWidth)*1.2).asBufferedImage(); scaleHeight = buff.getHeight(); if(scaleHeight < height){ flag = false; }else{ flag = true; } } //方案二, 按照高度来写数据, 判断高度是否 合适 if(!flag){ buff = Thumbnails.of(img).scale((((double)height)/oldHeight)*1.2).asBufferedImage(); scaleWidth = buff.getWidth(); if(scaleWidth <width){ flag = false; }else{ flag = true; } } //写出图片 if(true){ buff = Thumbnails.of(buff).sourceRegion(Positions.CENTER,width,height).scale(1.0D).asBufferedImage(); ImageIO.write(buff, FilenameUtils.getExtension(file.getPath()), new File(file.getPath())); } }else{ //图片 大小不合适 flag = false; } } catch (Exception e) { throw new RuntimeException("图片生成失败"); } return flag; } /** * 截图传递过来坐标,宽度和高度 进行图片的截取 * @param x x坐标 * @param y y坐标 * @param w 宽度 * @param h 高度 * @param file 文件对象 * @return */ public static boolean cutImage(int x,int y,int w,int h,File file){ try { BufferedImage img =ImageIO.read(new FileInputStream(file)); //这个sourceRegion 传递经来的是 坐标 和 宽度 ,高度 img = Thumbnails.of(img).sourceRegion( x, y,w,h).scale(1.0f).asBufferedImage(); ImageIO.write(img, FilenameUtils.getExtension(file.getPath()), new File(file.getPath())); } catch (Exception e) { throw new RuntimeException("截图失败"); } return false; } /** * 获取图片信息 * @return */ public static ImageInfo getImageInfo(File file){ ImageInfo info = null; try { BufferedImage img =ImageIO.read(new FileInputStream(file)); byte [] data =FileUtils.copyFileToByteArray(file); info = new ImageInfo(img.getHeight(), img.getWidth(), FilenameUtils.getExtension(file.getName()), data); } catch (Exception e) { // TODO: handle exception } return info; } /** * 定义图片的类型,供html 转 doc用 * @author yellowcong * @date 2016年1月8日 * */ public static class ImageInfo{ //图片高度 private int height; //图片宽度 private int width; //图片类型 jpg png啥的 private String type; //图片的数据 private byte [] data; public ImageInfo(int height, int width, String type, byte[] data) { super(); this.height = height; this.width = width; this.type = type; this.data = data; } public ImageInfo() { super(); // TODO Auto-generated constructor stub } 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 String getType() { return type; } public void setType(String type) { this.type = type; } public byte[] getData() { return data; } public void setData(byte[] data) { this.data = data; } }}
阅读全文
0 0
- Java之图片裁剪工具类-yellowcong
- JAVA图片裁剪工具类
- Java之字符串工具类-yellowcong
- Java之日期工具类-yellowcong
- Java之文件工具类-yellowcong
- Java之Json工具类-yellowcong
- Java之Luence工具类-yellowcong
- Java之颜色工具-yellowcong
- java实现图片裁剪缩放工具类
- java图片裁剪处理工具类代码
- JAVA 裁剪 压缩图片 工具类
- 图片裁剪工具类
- 图片裁剪工具类
- Java之请求发送工具类(HttpClientUtils,爬虫)-yellowcong
- JAVA之Java Decompiler反编译工具-yellowcong
- java缩放图片、java裁剪图片代码工具类
- Oracle之JAVA数据导出工具-yellowcong
- Java工具包之图片水印-yellowcong
- Handler解析(一):是如何实现线程之间的切换
- Java之请求发送工具类(HttpClientUtils,爬虫)-yellowcong
- 程序从片内地址0开始,但为什么链接地址又设0x30000000,那不就从0x30000000开始了,反汇编可以看到不是从0开始的
- Java之请求发送工具类(HttpRequestUtils,百度站长)-yellowcong
- (es5)JavaScript 知识梳理 (1)
- Java之图片裁剪工具类-yellowcong
- Spring Boot 系列(四)静态资源处理
- WSDL WebService的创建和使用实例
- github设置添加SSH
- Crazy Learning for Day 16
- FTPrep, 18 4Sum
- LeetCode 87.Scramble String
- Java之Json工具类-yellowcong
- 右值引用