效果动图展示

代码部分

@RestController
@RequestMapping("file")
public class ImageController extends BaseController {private static final long serialVersionUID = 1L;private static BufferedImage intputimage;private static BufferedImage outimage;private static String filename;private static Integer maxrgb;@AutowiredEnvironment environment;@AutowiredSessionStrategy sessionStrategy;@AutowiredImageUtils imageutils;@RequestMapping(value = "/getimagecode", method = RequestMethod.GET)public void getImageCode(HttpServletRequest request,HttpServletResponse response){try {response.setContentType("image/jpeg");ImageCode imagecode = imageutils.getImageCode();sessionStrategy.setAttribute(new ServletWebRequest(request),Constants.session_key,imagecode);ImageIO.write(imagecode.getImage(),"JPEG",response.getOutputStream());} catch (IOException e) {e.printStackTrace();}}@RequestMapping(value = "/pasebackground/{keys:\\d+}/{types:\\d?}/{rgbs}/{checked}", method = RequestMethod.GET)public void pasebackground(@PathVariable("keys") Integer keys, @PathVariable("types") Integer types,@PathVariable("rgbs") String rgbs,@PathVariable("checked") Boolean checked,HttpServletResponse response) {if (intputimage == null)throw new RuntimeException("请上传图片文件");try {response.setContentType("image/png");ImageIO.write((outimage = getImage(null,(outimage!=null && checked?outimage:intputimage),keys,types,rgbs)),"PNG",response.getOutputStream());} catch (IOException e) {e.printStackTrace();}}@RequestMapping(value = "/paseliangdu/{keys:\\d+}/{checked}", method = RequestMethod.GET)public void paseliangdu(@PathVariable("keys") Integer keys, @PathVariable("checked") Boolean checked,HttpServletResponse response) {if (intputimage == null)throw new RuntimeException("请上传图片文件");try {response.setContentType("image/png");ImageIO.write((outimage = liangdu(null,(outimage!=null && checked?outimage:intputimage),keys)),"PNG",response.getOutputStream());} catch (IOException e) {e.printStackTrace();}}@RequestMapping(value = "/download/{filename}", method = RequestMethod.GET)public void testDownload(HttpServletResponse resp, @PathVariable("filename") String name) throws IOException {Boolean isimage = Objects.equals("this",name);String file_name = LocalTime.now().getSecond()+".PNG";if(filename!=null && filename.length()>0)file_name = URLEncoder.encode(isimage?filename:name,"UTF-8");if(isimage){if(outimage!=null && filename!=null){resp.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);resp.setHeader("Content-Disposition","attachment;filename=" + file_name);ImageIO.write(outimage,"PNG",resp.getOutputStream());}else{resp.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);resp.getWriter().println(JSONObject.toJSONString(ResultUtil.resultErr("没有上传图片")));}return;}resp.setContentType("application/octet-stream");resp.setHeader("Content-Disposition","attachment;filename=" + file_name);File file = new File(environment.getProperty("file.upload.path"),name);if(!file.exists()){resp.setStatus(404);return;}Files.copy(file.toPath(),resp.getOutputStream());}@RequestMapping(value = "/upload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)public ResultUtil<?> upload(HttpServletRequest request, HttpServletResponse response) {Map<String, MultipartFile> multipartfiles = ((MultipartHttpServletRequest) request).getFileMap();if (multipartfiles.isEmpty())throw new RuntimeException("文件不能为空");for (Map.Entry<String, MultipartFile> file : multipartfiles.entrySet()) {MultipartFile multipartFile = file.getValue();if (multipartFile.isEmpty())throw new RuntimeException("文件不能为空");filename = multipartFile.getOriginalFilename();try {if (multipartfiles.containsKey("header") && multipartFile.getContentType().matches("image/(jpeg|jpg|png|gif)")) {if (multipartFile.getSize() > 1024 * 1024 * 1)throw new RuntimeException("图片大小不能超过1MB");intputimage = ImageIO.read(multipartFile.getInputStream());return ResultUtil.resultOK("上传成功");}String filepath = environment.getProperty(multipartfiles.containsKey("header")?"upload.header.path":"file.upload.path");Path path = Paths.get(filepath, multipartFile.getOriginalFilename());if (!Files.exists(path.getParent()))Files.createDirectories(path.getParent());Files.write(path,multipartFile.getBytes(),StandardOpenOption.CREATE_NEW);} catch (IOException e) {e.printStackTrace();throw new RuntimeException(e.getMessage());}}return ResultUtil.resultOK();}/*** 调整图片的亮度* @param path* @param image* @param keys* @return*/public static BufferedImage liangdu(String path, BufferedImage image,int random,int... maxrgb) {BufferedImage images = null;try {if (path == null && image == null)throw new RuntimeException("path or image not null");BufferedImage imagess = (image == null ? ImageIO.read(new File(path)) : image);images = imagess.createGraphics().getDeviceConfiguration().createCompatibleImage(imagess.getWidth(),imagess.getHeight(),Transparency.TRANSLUCENT);//int R = ((maxrgb[0] >> 16) & 0xff), G = ((maxrgb[0] >> 8) & 0xff), B = (maxrgb[0] & 0xff);int[] rgbs = new int[imagess.getWidth() * imagess.getHeight()];imagess.getRGB(0, 0, imagess.getWidth(), imagess.getHeight(), rgbs, 0, imagess.getWidth());for (int i = 0; i < rgbs.length; i++) {int r = ((rgbs[i] >> 16) & 0xff), g = ((rgbs[i] >> 8) & 0xff), b = (rgbs[i] & 0xff);//if(maxrgb.length==2)//if (Math.abs(R - r) < maxrgb[1] && Math.abs(G - g) < maxrgb[1] && Math.abs(B - b) < maxrgb[1])continue;int R2 = r+random,G2 = g+random,B2 = b+random;if(R2 > 230 && G2 > 230 && B2 > 230)continue;rgbs[i] = ((255 & 0xff) << 24) | ((clamp(R2) & 0xff) << 16) | ((clamp(G2) & 0xff) << 8) | (clamp(B2) & 0xff);}images.setRGB(0,0,images.getWidth(),images.getHeight(),rgbs,0,images.getWidth());} catch (Exception e) {e.printStackTrace();}return images;}private static int clamp(int r) {return r>255?255:r<0?0:r;}/*** * @param path 图片的路径* @param image 加载到内存的图片* @param type 请求处理图片的类型* @param keys 图片的容差值* @param rgbvalue 图片的RGB数值* @return 返回处理后的图片*/public static BufferedImage getImage(String path, BufferedImage image,int keys, int type,String rgbvalue) {BufferedImage images2 = null;try {Map<Integer, Integer> countmaps = new HashMap<>();if (path == null && image == null)throw new RuntimeException("path or image not null");BufferedImage images = image == null ? ImageIO.read(new File(path)) : image;int[] rgbs = new int[images.getWidth() * images.getHeight()];images.getRGB(0, 0, images.getWidth(), images.getHeight(), rgbs, 0, images.getWidth());for (int rgb : rgbs) {if (!countmaps.containsKey(rgb)) {countmaps.put(rgb, 0);continue;}countmaps.put(rgb, countmaps.get(rgb) + 1);}Integer maxvalue = countmaps.values().stream().reduce(0, (x, y) -> x > y ? x : y);Integer maxrgb = countmaps.keySet().stream().filter(x -> Objects.equals(countmaps.get(x), maxvalue)).findFirst().orElse(null);//System.err.println(maxrgb + " : " + maxvalue);Graphics2D graphics = images.createGraphics();graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);images2 = graphics.getDeviceConfiguration().createCompatibleImage(images.getWidth(), images.getHeight(),Transparency.TRANSLUCENT);int R = ((maxrgb >> 16) & 0xff), G = ((maxrgb >> 8) & 0xff), B = (maxrgb & 0xff);for (int i = 0; i < images.getWidth(); i++) {for (int j = 0; j < images.getHeight(); j++) {int rgb = images.getRGB(i, j);int r = ((rgb >> 16) & 0xff), g = ((rgb >> 8) & 0xff), b = (rgb & 0xff);if(type==0){if (!(Math.abs(R - r) < keys && Math.abs(G - g) < keys && Math.abs(B - b) < keys))images2.setRGB(i, j, rgb);}else if(type==2){if(rgbvalue==null || rgbvalue.length()<1 || !rgbvalue.matches("(\\d+,\\d+,\\d+)"))throw new RuntimeException("rgbvalue有误");String[] rgb_value = rgbvalue.split(",",3);int R2 = Integer.parseInt(rgb_value[0]),G2 = Integer.parseInt(rgb_value[1]),B2 = Integer.parseInt(rgb_value[2]);if (Math.abs(R - r) < keys && Math.abs(G - g) < keys && Math.abs(B - b) < keys){images2.setRGB(i,j,new Color(R2,G2,B2).getRGB());}else images2.setRGB(i,j,rgb);}// images.setRGB(i,j,((toumin*255/10)<<24) | (rgb &// 0x00ffffff));}}} catch (Exception e) {e.printStackTrace();}return images2;}

Springboot2.0实现在线图片处理(自动去背景、换背景色等功能)相关推荐

  1. 【转载】Linux命令-自动挂载文件/etc/fstab功能详解[转]

    博客园 首页 新随笔 联系 订阅 管理 随笔 - 322  文章 - 0  评论 - 19 Linux命令-自动挂载文件/etc/fstab功能详解[转]     一./etc/fstab文件的作用 ...

  2. Python图片批量自动抠图去背景

    图片批量自动抠图去背景 今天发现个好东西啊,叫片刻抠图(pickwant.com),是一个在线对图片自动抠图去除背景的网站.只要上传图片,就可以自动把背景去掉把目标对象抠出来. 不管是动物.汽车或各种 ...

  3. 图片去背景,在线图片去底色工具

    图片去背景,在线图片去底色工具 BgRemover - 在线图片去底工具 - 将纯色背景的图片转换为背景透明的图片 http://www.aigei.com/bgremover 美图图片在线制作 ht ...

  4. 这是一篇优雅的Springboot2.0使用手册

    最近再研究springboot的原理?颇有收获,现在让我分享一下springboot如何使用吧~ 想要解锁更多新姿势?请访问我的博客 啥是Springboot 和书上理解的不同,我认为Springbo ...

  5. 系统接口502异常_基于SpringBoot2.0的后台权限管理系统

    简介 基于SpringBoot2.0的后台权限管理系统界面简洁美观敏捷开发系统架构.核心技术采用Spring.MyBatis.Shiro没有任何其它重度依赖. 互联网云快速开发框架,微服务分布式代码生 ...

  6. asp.net中上传图片并生成小图片,自动添加水印的代码 .

    asp.net中上传图片并生成小图片,自动添加水印的代码 分类: .Net(C#) 2010-03-22 15:28 242人阅读 评论(0) 收藏 举报 /// 上传图片生成缩略图及水印 (来自:h ...

  7. SpringBoot2.0 基础案例(10):整合Mybatis框架,集成分页助手插件

    一.Mybatis框架 1.mybatis简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获 ...

  8. 20万数据 sql 快还是 java快?_基于SpringBoot2.0开发的,轻量级的,前后分离Java开发平台...

    项目说明 MintLeaf-Fast是一个基于SpringBoot2.0开发的,轻量级的,前后端分离的Java快速开发平台 开箱即用,节省开发时间,提升开发效率,能够快速开发项目并交付的接私活利器 支 ...

  9. ImageMagick将多张图片拼接成一张图片_不会PS也没关系!我想向你推荐这18款免费好用的在线图片处理工具...

    提到图片处理,很多小伙伴的第一反应都是PS软件.然而也会有人吐槽,PS安装特别麻烦,并不好用! 有没有不装PS就搞定各种图片的方法呢?三顿之前给大家分享过PPT中强大的图片处理功能. 其实还有更方便的 ...

最新文章

  1. [转]Extending the User Interface in Outlook 2010
  2. fatal error RC1004: unexpected end of file found处理方法
  3. 面试题:1 到 1000 之间有多少个 7?
  4. C语言试题二十九之编写函数int function(int lim,int aa[max])求出小于或等于lim的所有素数并放在aa数组中,该函数返回所求的素数的个数。
  5. 阿里云:构建全球企业内外安全网络最佳实践
  6. matlab2c使用c++实现matlab函数系列教程-diag函数
  7. 热文:if(a==1且a==2且a==3),有没有可能为true?
  8. PYG解密小组的Visual Assist X插件完全卸载
  9. Echarts地图,省市区县,行政代码及地图坐标
  10. OpManager Plus-IT基础设施监控
  11. Git 之 多人协同开发工作流
  12. CSDN博客放阿里妈妈广告代码的方法
  13. 激活 出现无法访问制定设备,路径或文件。您可能没有合适的权
  14. 不吸电子烟也请别吸电子咖啡!我们向雪加电子咖啡发起了挑战
  15. python3 实现自动生成入账记录表
  16. 选手机壳要擦亮双眼,不会遮挡激光对焦传感器的才是好壳子!
  17. Octotree 下载安装
  18. 网络安全行业与就业-网络安全事件分类
  19. MJ评《贫民窟的百万富翁》-满分10分
  20. 拉卡拉支付助力企业数字化转型

热门文章

  1. 如何给客户进行价值塑造?说一万句话,不如讲一个故事
  2. 显示器 如何切换输入源
  3. 关于ASO优化的这些你懂了吗?
  4. Windows 11关闭系统更新的方法有哪些?
  5. Codeforces Round #715 (Div. 2) A. Average Height
  6. 【LogiSim】便利的仿真软件,有了这个我又对硬件有了兴趣~
  7. Mysql 允许IP地址访问
  8. 转载-大数据管理神器:Ambari自定义stack和服务二次开发详细教程
  9. 802.11无线局域网的安全机制
  10. 模电学习笔记(上交郑老师)25.深度负反馈放大电路分析