import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Arrays; import java.util.Date; import javax.imageio.ImageIO; import javax.imageio.ImageReadParam; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; import com.lg.res.bean.common.FileData; public class ImageTailorUtil { private static String DEFAULT_THUMB_PREVFIX = "thumb_"; private static String DEFAULT_CUT_PREVFIX = "cut_"; private static Boolean DEFAULT_FORCE = false; /** * @param srcImg 源图片 * @param output 图片输出流 * @param rect 需要截取部分的坐标和大小 */ public boolean cutImage(File srcImg, OutputStream output, Rectangle rect){ if(srcImg.exists()){ 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){ System.out.println("Sorry, the image suffix is illegal. the standard image suffix is {}." + types); return false; } // 将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); return true; } 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 { System.out.println("the src image is not exist."); } return false; } public void cutImage(File srcImg, OutputStream output, int x, int y, int width, int height){ cutImage(srcImg, output, new Rectangle(x, y, width, height)); } public FileData cutImage(File srcImg, String destImgPath, Rectangle rect){ FileData fd = new FileData(); String middledir = "cut_pic/"+SimpleDate.getCurDateLong();//中间路径 File destImg = new File(destImgPath+"/"+middledir); if(!destImg.exists()){ System.out.println("the dest "+destImgPath+" folder is not exist."); destImg.mkdirs(); System.out.println("create "+destImgPath+" folder "); } String cutFolderPath = destImg.getPath(); try { if(!destImg.isDirectory()) cutFolderPath = destImg.getParent(); if(!cutFolderPath.endsWith(File.separator)) cutFolderPath = cutFolderPath + File.separator; String fileName = srcImg.getName(); String suffix = fileName.substring(fileName.lastIndexOf(".")); String newfileName = DEFAULT_CUT_PREVFIX + "_" + new Date().getTime() + suffix; String outPutFilePath =cutFolderPath+ newfileName; System.out.println(outPutFilePath); boolean b = cutImage(srcImg, new FileOutputStream(outPutFilePath), rect); if (b) { fd.setTailorPicPath(middledir+"/"+newfileName); return fd; } } catch (FileNotFoundException e) { System.out.println("the dest "+destImgPath+" is not exist."); } return null; } /** * * @param srcImg 原图片 * @param destImg 存放新生成的图片的文件夹路径 * @param x 左上角 x轴坐标 * @param y 左上角 y轴坐标 * @param width 宽 * @param height 高 */ public FileData cutImage(File srcImg, String destImg, int x, int y, int width, int height){ if (!srcImg.exists()) { return null; } return cutImage(srcImg, destImg, new Rectangle(x, y, width, height)); } /** * * @param srcImg 原图片 * @param destImg 存放新生成的图片的文件夹路径 * @param x 左上角 x轴坐标 * @param y 左上角 y轴坐标 * @param width 宽 * @param height 高 */ public FileData cutImage(String srcImg, String destImg, int x, int y, int width, int height){ return cutImage(new File(srcImg), destImg, new Rectangle(x, y, width, height)); } }
java 根据坐标和宽高对图片做剪切
点赞
收藏