public class CutImageUtils { public static SecureRandom rnd = new SecureRandom(); public static String cutImage(int x, int y, int width, int height,String srcPath) throws Exception{ String root = ApplicationContext.getProperty("upload_folder"); String imagePath = root+srcPath; FileInputStream is = null; ImageInputStream iis = null; String uploadFolder = null ; String filepath = null ; String serverName = null ; uploadFolder = ApplicationContext.getProperties().getProperty("upload_folder").toString() ; filepath = generateServerFolder() ; serverName = generateServerName(srcPath) ; File file = new File(uploadFolder + filepath); if (!file.exists()) { file.mkdirs(); } try { // 读取图片文件 saveMinPhoto(imagePath, imagePath, 300, 0.9d); //resetsize(imagePath, imagePath); is = new FileInputStream(imagePath); String ext = srcPath.substring(srcPath.lastIndexOf(".") + 1); Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(ext); ImageReader reader = it.next(); // 获取图片流 iis = ImageIO.createImageInputStream(is); reader.setInput(iis, true); ImageReadParam param = reader.getDefaultReadParam(); /** * * 图片裁剪区域。Rectangle 指定了坐标空间中的一个区域,通过 Rectangle 对象 * * 的左上顶点的坐标(x,y)、宽度和高度可以定义这个区域。 */ Rectangle rect = new Rectangle(x, y, width, height); // 提供一个 BufferedImage,将其用作解码像素数据的目标。 param.setSourceRegion(rect); BufferedImage bi = reader.read(0, param); // 保存新图片 ImageIO.write(bi, ext, new File(uploadFolder + filepath + serverName)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block if (is != null) is.close(); if (iis != null) iis.close(); e.printStackTrace(); } return filepath + serverName ; } /** * * @param config * @param file * @param request * @return */ public static String generateServerName(String clientPath) { //按当前时间的分钟毫秒+3位随机数 Calendar c = Calendar.getInstance(); String min = get(c,Calendar.MINUTE); String sec = get(c,Calendar.SECOND); String mis = get(c,Calendar.MILLISECOND); String rnd = random(); String ext = getFileExt(getClientName(clientPath)); return min + sec + mis + rnd + ext ; } /** 客户端文件名 */ public static String getClientName(String clientPath) { if(null != clientPath){ int index1 = clientPath.lastIndexOf("https://www.jb51.net/"); int index2 = clientPath.lastIndexOf("\\"); if(index2 > index1){ index1 = index2; } return clientPath.substring(index1+1,clientPath.length()); } return null; } public static String getFileExt(String fileName){ if(null != fileName){ int dot = fileName.lastIndexOf("."); if(dot >= 0){ return fileName.substring(dot); } } return ""; } public static String random(){ String value = String.valueOf(rnd.nextInt(1000)); if(value.length() < 3){ for(int i=value.length();i<3;i++){ value = "0" + value; } } return value; } public static String generateServerFolder() { //按当前年月日和小时生成文件路径 Calendar c = Calendar.getInstance(); String year = get(c,Calendar.YEAR); String mon = get(c,Calendar.MONTH); String day = get(c,Calendar.DAY_OF_MONTH); String hour = get(c,Calendar.HOUR_OF_DAY); return year + "https://www.jb51.net/" + mon + "https://www.jb51.net/" + day + "https://www.jb51.net/" + hour + "https://www.jb51.net/"; } public static String get(Calendar c,int field){ int v = c.get(field); if(field == Calendar.MONTH){ v += 1; } String value = String.valueOf(v); if(value.length() == 1){ value = "0" + value; } return value; } /** * 缩小图片到固定长高 * @param srcImagePath 读取图片路径 * @param toImagePath 写入图片路径 * @param width 缩小后图片宽度 * @param height 缩小后图片长度 * @throws IOException */ public static void reduceImageByWidthHeight(String srcImagePath, String toImagePath, int width, int height) throws IOException{ FileOutputStream out = null; try{ //读入文件 File file = new File(srcImagePath); String ext = srcImagePath.substring(srcImagePath.lastIndexOf(".") + 1); // 构造Image对象 BufferedImage src = javax.imageio.ImageIO.read(file); // 缩小边长 BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 绘制缩小后的图片 tag.getGraphics().drawImage(src, 0, 0, width, height, null); out = new FileOutputStream(toImagePath); ImageIO.write(tag, ext, new File(toImagePath)); }catch(Exception e){ e.printStackTrace(); }finally{ if(out != null){ out.close(); } out = null; System.gc(); } } /** * 等比例压缩算法: * 算法思想:根据压缩基数和压缩比来压缩原图,生产一张图片效果最接近原图的缩略图 * @param srcURL 原图地址 * @param deskURL 缩略图地址 * @param comBase 压缩基数 * @param scale 压缩限制(宽/高)比例 一般用1: * 当scale>=1,缩略图height=comBase,width按原图宽高比例;若scale<1,缩略图width=comBase,height按原图宽高比例 * @throws Exception */ public static void saveMinPhoto(String srcURL, String deskURL, double comBase, double scale) throws Exception { File srcFile = new java.io.File(srcURL); String ext = srcURL.substring(srcURL.lastIndexOf(".") + 1); Image src = ImageIO.read(srcFile); int srcHeight = src.getHeight(null); int srcWidth = src.getWidth(null); int deskHeight = 0;// 缩略图高 int deskWidth = 0;// 缩略图宽 double srcScale = (double) srcHeight / srcWidth; /**缩略图宽高算法*/ if ((double) srcHeight > comBase || (double) srcWidth > comBase) { if (srcScale >= scale || 1 / srcScale > scale) { if (srcScale >= scale) { deskHeight = (int) comBase; deskWidth = srcWidth * deskHeight / srcHeight; } else { deskWidth = (int) comBase; deskHeight = srcHeight * deskWidth / srcWidth; } } else { if ((double) srcHeight > comBase) { deskHeight = (int) comBase; deskWidth = srcWidth * deskHeight / srcHeight; } else { deskWidth = (int) comBase; deskHeight = srcHeight * deskWidth / srcWidth; } } } else { deskHeight = srcHeight; deskWidth = srcWidth; } BufferedImage tag = new BufferedImage(deskWidth, deskHeight, BufferedImage.TYPE_3BYTE_BGR); tag.getGraphics().drawImage(src, 0, 0, deskWidth, deskHeight, null); //绘制缩小后的图 FileOutputStream deskImage = new FileOutputStream(deskURL); //输出到文件流 ImageIO.write(tag, ext, new File(deskURL)); deskImage.close(); } public static void main(String[] args) { try { String src = CutImageUtils.cutImage(11, 12, 100, 100, "2017/01/04/17/6348162d-5b50-4e7d-b414-93140498f8de.jpg"); System.out.println(src); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
利用imgareaselect辅助后台实现图片上传裁剪(2)
内容版权声明:除非注明,否则皆为本站原创文章。