java 图片视频校验,图片缩放
时间:2022-09-16 04:00:00
图片验证分辨率
/** * 获取远程图片属性 * * * @param path * @return * @throws Exception * @author * @date 2021/12/23 15:06 * @version 1.0.0 */
public static FilePictureAttributeDTO pictureAttribute(String path) {
FilePictureAttributeDTO res = null; FileInputStream inputStream = null; try {
long l = System.currentTimeMillis(); if (StringUtils.isBlank(path)) {
return res; } File file = new File(path); long l1 = System.currentTimeMillis(); inputStream = new FileInputStream(file); BufferedImage bi = ImageIO.read(inputStream); log.info("pictureAttribute_url-ImageIO.read耗时:{}", System.currentTimeMillis() - l1); res = new FilePictureAttributeDTO().setWidth(bi.getWidth()).setHeight(bi.getHeight()).setSize(file.length());
log.info("pictureAttribute_url-返回:{},总耗时:{}", JSON.toJSONString(res), System.currentTimeMillis() - l);
} catch (Exception ex) {
log.error("{} - pictureAttribute_url-获取图片像素异常:{}", path, ex);
} finally {
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return res;
}
/** * 获取远程图片属性 * * * @param url * @return * @throws Exception * @author * @date 2021/12/23 15:06 * @version 1.0.0 */
public static FilePictureAttributeDTO pictureAttribute_url(String url) {
FilePictureAttributeDTO res = null;
File tempFile = null;
FileOutputStream fileOutputStream = null;
try {
if (StringUtils.isBlank(url)) {
return res;
}
long l = System.currentTimeMillis();
URL remoteUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) remoteUrl.openConnection();
connection.setRequestMethod("GET");
InputStream inputStream = connection.getInputStream();
tempFile = File.createTempFile(UUID.randomUUID().toString().replaceAll("-", ""), url.substring(url.lastIndexOf(".")));
fileOutputStream = new FileOutputStream(tempFile);
IoUtil.copy(inputStream, fileOutputStream);
log.info("pictureAttribute_url-下载到本地远程图片耗时:{}", System.currentTimeMillis() - l);
long l1 = System.currentTimeMillis();
BufferedImage bi = ImageIO.read(tempFile);
log.info("pictureAttribute_url-ImageIO.read耗时:{}", System.currentTimeMillis() - l1);
res = new FilePictureAttributeDTO().setWidth(bi.getWidth()).setHeight(bi.getHeight()).setSize(tempFile.length());
log.info("pictureAttribute_url-返回:{},总耗时:{}", JSON.toJSONString(res), System.currentTimeMillis() - l);
} catch (Exception ex) {
log.error("{} - pictureAttribute_url-获取图片像素异常:{}", url, ex);
} finally {
if (null != fileOutputStream) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != tempFile) {
tempFile.delete();
}
}
return res;
}
@Data
@Accessors(chain = true)
public class FilePictureAttributeDTO {
/** * 宽 */
private Integer width;
/** * 高 */
private Integer height;
/** * 大小 */
private Long size;
/** * 格式 */
private String format;
}
视频分辨率帧数大小校验
/** * 视频属性获取 * * * @param path 本地视频地址 * @return * @author * @date 2021/12/23 14:31 * @version 1.0.0 */
public static FileVideoAttributeDTO videoAttribute(String path) {
FileVideoAttributeDTO res = null;
if (StringUtils.isBlank(path)) {
return res;
}
FileChannel fc = null;
try {
long l = System.currentTimeMillis();
File source = new File(path);
Encoder encoder = new Encoder();
it.sauronsoftware.jave.MultimediaInfo m = encoder.getInfo(source);
FileInputStream fis = new FileInputStream(source);
fc = fis.getChannel();
if (null == m.getVideo()) {
throw new BusinessException(500, "解析视频video为空");
}
res = new FileVideoAttributeDTO().setVideoTime(m.getDuration() / 1000)
.setHeight(m.getVideo().getSize().getHeight())
.setWidth(m.getVideo().getSize().getWidth())
.setFrameRate(m.getVideo().getFrameRate())
.setBitRate(m.getVideo().getBitRate())
.setFormat(m.getFormat())
.setSize(fc.size())
;
log.info("videoAttribute获取视频属性返回:{},耗时:{}", JSON.toJSONString(res), System.currentTimeMillis() - l);
return res;
// BigDecimal fileSize = new BigDecimal(fc.size());
// size = fileSize.divide(new BigDecimal(1048576), 2, RoundingMode.HALF_UP) + "MB";
} catch (Exception e) {
log.error("{} - videoAttribute获取视频属性异常:{}", path, e);
} finally {
if (null != fc) {
try {
fc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return res;
}
/** * 视频属性获取 * * * @param url 远程视频地址 * @return * @author * @date 2021/12/23 14:31 * @version 1.0.0 */
public static FileVideoAttributeDTO videoAttribute_url(String url) {
FileVideoAttributeDTO res = null;
if (StringUtils.isBlank(url)) {
return res;
}
FileChannel fc = null;
FileOutputStream fileOutputStream = null;
File tempFile = null;
InputStream inputStream = null;
try {
long l = System.currentTimeMillis();
URL remoteUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) remoteUrl.openConnection();
connection.setRequestMethod("GET");
inputStream = connection.getInputStream();
tempFile = File.createTempFile(UUID.randomUUID().toString().replaceAll("-", ""), url.substring(url.lastIndexOf(".")));
fileOutputStream = new FileOutputStream(tempFile);
IoUtil.copy(inputStream, fileOutputStream);
log.info("videoAttribute_url-下载远程视频至本地耗时:{}", (System.currentTimeMillis() - l));
long l3 = System.currentTimeMillis();
Encoder encoder = new Encoder();
it.sauronsoftware.jave.MultimediaInfo m = encoder.getInfo(tempFile);
log.info("encoder.getInfo解析视频耗时:{}", (System.currentTimeMillis() - l3));
FileInputStream fis = new FileInputStream(tempFile);
fc = fis.getChannel();
if (null == m.getVideo()) {
throw new BusinessException(500, "解析视频video为空");
}
res = new FileVideoAttributeDTO().setVideoTime(m.getDuration() / 1000)
.setHeight(m.getVideo().getSize().getHeight())
.setWidth(m.getVideo().getSize().getWidth())
.setFormat(m.getFormat())
.setFrameRate(m.getVideo().getFrameRate())
.setBitRate(m.getVideo().getBitRate())
.setSize(fc.size())
;
log.info("videoAttribute获取视频属性返回:{},耗时:{}", JSON.toJSONString(res), System.currentTimeMillis() - l);
return res;
// BigDecimal fileSize = new BigDecimal(fc.size());
// size = fileSize.divide(new BigDecimal(1048576), 2, RoundingMode.HALF_UP) + "MB";
} catch (Exception e) {
log.error("{} - videoAttribute获取视频属性异常:{}", url, e);
} finally {
if (null != fc) {
try {
fc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != fileOutputStream) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != tempFile) {
tempFile.delete();
}
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return res;
}
@Data @Accessors(chain = true) public class FileVideoAttributeDTO { /** * 时长(s) */ private Long videoTime; /** * 宽 */ private Integer width; /** * 高 */ private Integer height; /** * 大小 */ private Long size; /** * 格式 */ private String format; /** * 帧数 */ private Float frameRate; /** *
比特率 */ private Integer bitRate; }
<dependency>
<groupId>com.github.vip-zpf</groupId>
<artifactId>jave</artifactId>
<version>1.0.9</version>
</dependency>
图片缩放
public static void main(String[] args) { thumbnail("D:\\70380f81-4752-4cde-bb0f-51101df6bbc8.jpg"); thumbnail("D:\\3131321.png"); thumbnail("D:\\eee.jpg"); thumbnail("D:\\long.jpg"); thumbnail("D:\\zyun.jpg"); thumbnail("D:\\微信图片_20211228110440.jpg"); } /** * 等比例压缩 *
* * @param path * @return * @author * @date 2021/12/24 17:48 * @version 1.0.0 */
public static String thumbnail(String path) { OutputStream os = null; String newPath = null; try { newPath = "D:\\thumb\\" + UUID.randomUUID().toString().replace("-", "") + path.substring(path.lastIndexOf(".")); File file = new File(newPath); os = new FileOutputStream(file); Image image = ImageIO.read(new FileInputStream(path)); int width = image.getWidth(null);// 获得原图的宽度 int height = image.getHeight(null);// 获得原图的高度 System.err.println("原来width:" + width + ",height:" + height); BigDecimal rate = bl(Long.valueOf(height), Long.valueOf(width), 1080L, 1440L); // 计算缩略图最总的宽度和高度 int newWidth = BigDecimal.valueOf(