Commit b27f5e7c authored by shiyu's avatar shiyu

媒体工具类

parent 7077b92f
package com.wwdz.ch.core.entity;
import com.xxdxxs.entity.Entity;
import lombok.Data;
@Data
public class ImageInfo implements Entity {
private int height;
private int width;
public String formartString() {
return this.width + "x" + this.height;
}
}
package com.wwdz.ch.core.util; package com.wwdz.ch.core.util;
import com.wwdz.ch.core.consts.CommConsts;
import com.wwdz.ch.core.entity.ImageInfo;
import com.xxdxxs.utils.CommonUtils;
import com.xxdxxs.utils.StringUtils; import com.xxdxxs.utils.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -10,6 +13,7 @@ import java.io.ByteArrayInputStream; ...@@ -10,6 +13,7 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL;
public class MediaUtil { public class MediaUtil {
...@@ -36,6 +40,36 @@ public class MediaUtil { ...@@ -36,6 +40,36 @@ public class MediaUtil {
} }
public static ImageInfo getImageSize(InputStream inputStream) {
try {
BufferedImage bufferedImage = ImageIO.read(inputStream);
ImageInfo imageInfo = new ImageInfo();
imageInfo.setHeight(bufferedImage.getHeight());
imageInfo.setHeight(bufferedImage.getWidth());
return imageInfo;
} catch (Exception e) {
logger.error("获取图片尺寸出错 : {}" ,e);
}
return null;
}
public static ImageInfo getImageSize(String imageUrl) {
try {
URL url = new URL(imageUrl);
InputStream inputStream = url.openStream();
BufferedImage bufferedImage = ImageIO.read(inputStream);
ImageInfo imageInfo = new ImageInfo();
imageInfo.setHeight(bufferedImage.getHeight());
imageInfo.setWidth(bufferedImage.getWidth());
return imageInfo;
} catch (Exception e) {
logger.error("获取图片尺寸出错 : {}" ,e);
}
return null;
}
/** /**
* 将BufferedImage转换为InputStream * 将BufferedImage转换为InputStream
* *
...@@ -52,4 +86,11 @@ public class MediaUtil { ...@@ -52,4 +86,11 @@ public class MediaUtil {
} }
return null; return null;
} }
public static void main(String[] args) {
String vedioUrl = "https://cdn.wanwudezhi.com/static/web-static/video/53596700878a572ca4fae35243aaa819.mp4";
String url = getImageByCutVedio(vedioUrl);
ImageInfo imageInfo = getImageSize(url);
System.out.println(imageInfo.formartString());
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment