博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
压缩图片
阅读量:7223 次
发布时间:2019-06-29

本文共 3974 字,大约阅读时间需要 13 分钟。

  hot3.png

public static String uploadImg(MultipartFile file, int width, int height,			boolean whFixed, boolean borderRadius, boolean needCompress,			long maxSize) throws SvcSuspendedException {		PropertiesInjectUtil propertiesInjectUtil = SpringContextHolder				.getBean("propertiesInjectUtil");		String fileName = file.getOriginalFilename();		int suffixIndex = fileName.lastIndexOf('.');		if (suffixIndex == -1) {			throw new SvcSuspendedException("上传失败!文件没有后缀名,不允许上传!");		}		String name = fileName.substring(0, suffixIndex);		if (StringUtils.isBlank(name)) {			throw new SvcSuspendedException("上传失败!该文件名没有文件名,不允许上传!");		}		String suffix = file.getContentType();		if (!UploadUtil.acceptImg.contains(suffix)) {			throw new SvcSuspendedException("请上传正确的图片格式,目前仅支持jpg格式");		}		if (file.getSize() > maxSize * 1000) {			throw new SvcSuspendedException("上传图片不能大于" + maxSize + "K");		}		try {			BufferedImage srcBufferImage = ImageIO.read(file.getInputStream());			if (whFixed) {				if (srcBufferImage.getWidth() != width						|| srcBufferImage.getHeight() != height) {					throw new SvcSuspendedException("图片长度必须为" + width							+ "像素,高度必须为" + height + "个像素");				}			}			InputStream is = null;			if (borderRadius) {				if (!BORDER_RADIUS_ACCEPT_IMG_TYPE.contains(suffix)) {					throw new SvcSuspendedException("仅PNG格式图片支持圆角");				}				BufferedImage rounded = makeRoundedCorner(srcBufferImage, 30);				ByteArrayOutputStream baos = new ByteArrayOutputStream();				ImageIO.write(rounded, "png", baos);				is = new ByteArrayInputStream(baos.toByteArray());			} else {				is = file.getInputStream();			}			String filePath = propertiesInjectUtil.getUpload_img_url();			String path = UploadUtil.upload(is, fileName, filePath,					RequestUtils.getWebpath() + "/upload/images");			if (needCompress) {				String dest = UploadUtil.generateLocalRandomPath(fileName,						filePath);				ImageUtil.compressImg(80, path, dest);				String url = UploadUtil.convertLocalPathToUrl(dest,						RequestUtils.getWebpath() + "/upload/images");				return url;			}			return path;		} catch (IOException e) {			log.error("Errors when read or update.", e);			throw new SvcSuspendedException("上传图片失败!");		} 		catch (MagickException e) {			log.error("Errors when compress images.", e);			throw new SvcSuspendedException("图片压缩失败!");		} 	}

makeRoundedCorner方法:

public static BufferedImage makeRoundedCorner(BufferedImage image,			int cornerRadius) {		int w = image.getWidth();		int h = image.getHeight();		BufferedImage output = new BufferedImage(w, h,				BufferedImage.TYPE_4BYTE_ABGR);		Graphics2D g2 = (Graphics2D) output.getGraphics();		// g2.setBackground(Color.WHITE);		// g2.clearRect(0, 0, w, h);		// This is what we want, but it only does hard-clipping, i.e. aliasing		// g2.setClip(new RoundRectangle2D ...)		// so instead fake soft-clipping by first drawing the desired clip shape		// in fully opaque white with antialiasing enabled...		g2.setComposite(AlphaComposite.Src);		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,				RenderingHints.VALUE_ANTIALIAS_ON);		g2.setColor(Color.WHITE);		Shape s = new RoundRectangle2D.Float(0, 0, w, h, cornerRadius,				cornerRadius);		g2.fill(s);		// ... then compositing the image on top,		// using the white shape from above as alpha source		g2.setComposite(AlphaComposite.SrcAtop);		g2.drawImage(image, 0, 0, null);		g2.setComposite(AlphaComposite.Src);		g2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING,				RenderingHints.VALUE_COLOR_RENDER_DEFAULT);		g2.setBackground(Color.WHITE);		g2.setColor(Color.WHITE);		for (int x = 0; x <= w; x++) {			for (int y = 0; y <= h; y++) {				if (!s.contains(x, y)) {					if (x < w / 2) {						if (y < h / 2) {							g2.drawRect(x, y, 1, 1);						} else {							g2.drawRect(x, y - 1, 1, 1);						}					} else {						if (y < h / 2) {							g2.drawRect(x - 1, y, 1, 1);						} else {							g2.drawRect(x - 1, y - 1, 1, 1);						}					}				}			}		}		g2.dispose();		return output;	}

转载于:https://my.oschina.net/jeeker/blog/424773

你可能感兴趣的文章
657. Insert Delete GetRandom O(1)
查看>>
Java编程资料
查看>>
duilib 设计界面 初体验(附超链接开发)
查看>>
jvm内存区域
查看>>
IOS--常用控件--UIScrollView
查看>>
能够使开发和调试更为方便的java日志框架
查看>>
冷门Javascript API——element.insertAdjacentHTML
查看>>
绘制希尔伯特曲线
查看>>
LOJ 572 「LibreOJ Round #11」Misaka Network 与求和——min_25筛
查看>>
test
查看>>
[精华][推荐]CAS SSO单点登录服务端客户端实例
查看>>
「hadoop」ssh
查看>>
pulseaudio备注
查看>>
PAT1064 Complete Binary Search Tree (30)(BST)
查看>>
FPGA负数的右移 计算
查看>>
ti processor sdk linux am335x evm /bin/create-sdcard.sh hacking
查看>>
转 Redis 总结精讲 看一篇成高手系统-4
查看>>
第一节:神经网络和深度学习
查看>>
HTML5_1
查看>>
node-webkit连接mysql
查看>>