首页 > 编程语言 >82-java上传图片 并压缩

82-java上传图片 并压缩

时间:2022-10-16 10:02:30浏览次数:51  
标签:java srcHeight height deskHeight width deskWidth 82 上传 srcWidth


	private BufferedImage getNewImage(MultipartFile oldImage, double width, double height) throws IOException{
		/*srcURl 原图地址;deskURL 缩略图地址;comBase 压缩基数;scale 压缩限制(宽/高)比例*/

		ByteArrayInputStream bais = new ByteArrayInputStream(oldImage.getBytes());
		MemoryCacheImageInputStream mciis = new MemoryCacheImageInputStream(bais);
		Image src = ImageIO.read(mciis);
		double srcHeight = src.getHeight(null);
		double srcWidth = src.getWidth(null);
		double deskHeight = 0;//缩略图高
		double deskWidth  = 0;//缩略图宽
		if (srcWidth>srcHeight) {

			if (srcWidth>width) {
				if (width/height>srcWidth/srcHeight) {
					deskHeight = height;
					deskWidth = srcWidth/(srcHeight/height);
				}
				else {
					deskHeight = width/(srcWidth/srcHeight);
					deskWidth = width;
				}
			}
			else {

				if (srcHeight>height) {
					deskHeight = height;
					deskWidth = srcWidth/(srcHeight/height);
				}else {
					deskHeight=srcHeight;
					deskWidth=srcWidth;
				}

			}

		}
		else if(srcHeight>srcWidth)
		{
			if (srcHeight>(height)) {
				if ((height)/width>srcHeight/srcWidth) {
					deskHeight = srcHeight/(srcWidth/width);
					deskWidth = width;
				}else {
					deskHeight = height;
					deskWidth = (height)/(srcHeight/srcWidth);
				}
			}
			else {
				if (srcWidth>width) {
					deskHeight = srcHeight/(srcWidth/width);
					deskWidth = width;
				}else {
					deskHeight=srcHeight;
					deskWidth=srcWidth;
				}

			}

		}
		else if (srcWidth==srcHeight) {

			if (width>=(height)&&srcHeight>(height)) {
				deskWidth=(height);
				deskHeight=(height);
			}
			else if (width<=(height)&&srcWidth>width) {
				deskWidth=width;
				deskHeight=width;
			}
			else  if (width==(height)&&srcWidth<width) {
				deskWidth=srcWidth;
				deskHeight=srcHeight;
			}
			else {
				deskHeight=srcHeight;
				deskWidth=srcWidth;
			}

		}
		BufferedImage tag = new BufferedImage((int)deskWidth,(int)deskHeight,
				BufferedImage.TYPE_3BYTE_BGR);
		tag.getGraphics().drawImage(src, 0, 0, (int)deskWidth, (int)deskHeight, null); //绘制缩小后的图
		return tag;
	}

上传的MultipartFile

@RequestParam("picFile") MultipartFile file
String originalName = file.getOriginalFilename();
originalName = originalName.toLowerCase();
resp.setType(FilenameUtils.getExtension(originalName));

String id = DateUtils.format(new Date(),"yyyyMMddHHmmssSSS");

resp.setFileName(id + "." + resp.getType());

File path = FileUtils.getFile(imageRootDir);

FileUtils.forceMkdir(path);

File ordinalFile = FileUtils.getFile(path,resp.getFileName());

ImageIO.write(newImage, resp.getType(), ordinalFile);

标签:java,srcHeight,height,deskHeight,width,deskWidth,82,上传,srcWidth
From: https://blog.51cto.com/u_14816966/5760151

相关文章

  • Java注解(3):一个真实Elasticsearch案例
    学会了技术就要使用,否则很容易忘记,因为自然界压根就不存在什么代码、变量之类的玩意,这都是一些和生活常识格格不入的东西。只能多用多练,形成肌肉记忆才行。在一次实际的产......
  • 补:java中static的用法总结
    关于java在static中的用法,大致可以总结为以下三个模块:其一为:静态变量某些特定的数据在内存中只有一份,而且能被一个类的所有实例对象共享。可以使用类名.变量名的形式来访......
  • CF982D
    *1900不会做了。。考虑将所有数从小到大排序后顺次插入,用并查集维护。需要维护以下几个东西:区间的总数量,最长区间的长度,最长区间的个数。当最长区间的个数等于区间的总......
  • javascript: Webcam
     varstop=function(){varstream=video.srcObject;vartracks=stream.getTracks();for(vari=0;i<tracks.length;i++){vartrack=tra......
  • javascript 数组
    javascript数组文章目录​​javascript数组​​​​1.简介​​​​2.创建数组​​​​3.访问数组​​​​4.数组方法和属性​​​​5.创建新方法​​​​6.实例​​......
  • javascript class
    javascriptclass文章目录​​javascriptclass​​​​1.简介​​​​2.浏览器支持​​​​3.使用类​​​​4.类表达式​​​​5.类的方法​​​​6.严格模式"use......
  • Java身份证校验工具类
    Java身份证校验工具类工作中经常会涉及到身份证的校验,而且需求不同,有的需要校验最后一位校验位,有的不需要,这里参考了几篇文章及自己工作中用到的情况写了一个工具类。记录一......
  • 基于JavaEE的企业制度管理系统的设计与实现-计算机毕业设计源码+LW文档
    摘要:本企业制度管理系统是针对目前企业制度管理的实际需求,从实际工作出发,对过去的企业制度管理系统存在的问题进行分析,完善用户的使用体会。采用计算机系统来管理信息,取代人......
  • redis bitmap数据结构之java对等操作
    在之前的文章中,我们有说过bitmap,bitmap在很多场景可以应用,比如黑白名单,快速判定,登录情况等等。总之,bitmap是以其高性能出名。其基本原理是一位存储一个标识,其他衍生知......
  • JavaScript --- 随机点名抽奖系统
    1<!DOCTYPEhtml>2<htmllang="en">34<head>5<metacharset="UTF-8">6<metahttp-equiv="X-UA-Compatible"content="IE=edge">7<metan......