传入文件的全路径 给图片和视频其中一帧转换为base64码返回前端反显
/**
* 生成缩略图并返回base64
*
* @param originalImagePath 全路径
* @param width 尺寸
* @param height 尺寸
* @param ext 类型
* @return
* @throws IOException
*/
public String generateThumbnail(String originalImagePath, int width, int height, String ext) throws IOException {
File file = new File(originalImagePath);
if (!file.exists()) {
return "[{'file':true}]";
}
String base64Thumbnail = "";
// 判断文件类型
String fileType = getFileExtension(originalImagePath);
if (fileType.equalsIgnoreCase("jpg") || fileType.equalsIgnoreCase("png") || fileType.equalsIgnoreCase("jpeg")) {
// 如果是图片文件
base64Thumbnail = generateImageThumbnail(originalImagePath,ext, width, height);
} else if (fileType.equalsIgnoreCase("mp4") || fileType.equalsIgnoreCase("avi") || fileType.equalsIgnoreCase("mov")) {
// 如果是视频文件
base64Thumbnail = generateVideoThumbnail(originalImagePath,ext, width, height);
} else {
// 其他类型文件
base64Thumbnail = "[{'file':true}]";
}
return base64Thumbnail;
}
/**
* 判断文件类型
* @param filePath
* @return
*/
private String getFileExtension(String filePath) {
int dotIndex = filePath.lastIndexOf(".");
if (dotIndex == -1) {
return "";
}
return filePath.substring(dotIndex + 1);
}
/**
* 生成图片缩略图的Base64编码字符串
* @param originalImagePath 原始图像文件路径
* @param width 缩略图目标宽度
* @param height 缩略图目标高度
* @return 缩略图的Base64编码字符串
* @throws IOException 当读取或写入文件时发生错误
*/
private String generateImageThumbnail(String originalImagePath,String ext, int width, int height) throws IOException {
BufferedImage originalImage = ImageIO.read(new File(originalImagePath));
// 指定尺寸缩放图像
BufferedImage resizedImage = resizeImage(originalImage, width, height);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(resizedImage, "jpg", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();
// 使用Base64编码将缩略图的字节数组转换为字符串
return "data:image/" + ext + ";base64," + Base64.getEncoder().encodeToString(imageInByte);
}
/**
* 将缩略图转换为 Base64 格式
* @param image
* @return
*/
private String convertImageToBase64(BufferedImage image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(image, "jpg", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();
String base64Image = "data:image/" + "jpg" + ";base64," + Base64.getEncoder().encodeToString(imageInByte);
baos.close();
return base64Image;
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
/**
* 缩放图像
*
* @param originalImage 原始图像对象
* @param width 目标宽度
* @param height 目标高度
* @return 缩放后的图像对象
*/
private BufferedImage resizeImage(BufferedImage originalImage, int width, int height) {
Image tmp = originalImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = resizedImage.createGraphics();
g2d.drawImage(tmp, 0, 0, null);
g2d.dispose();
return resizedImage;
}
private String generateVideoThumbnail(String originalVideoPath,String ext, int width, int height) throws IOException {
String thumbnailBase64 = "";
// 使用 JavaCV 提取视频的第20帧作为缩略图
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(originalVideoPath);
try {
grabber.start();
int ftp = grabber.getLengthInFrames();
// 标记变量,用于跳出循环
int flag = 0;
Frame frame = null;
// 设置要读取的帧数
while (flag <= ftp) {
frame = grabber.grabImage();
// 当找到第20帧时,跳出循环
if ((flag > 20) && (frame != null)) {
break;
}
flag++;
}
BufferedImage frameImage = (BufferedImage) frameToBufferedImage(frame);
// 调整图像大小
BufferedImage resizedImage = resizeImage(frameImage, width, height);
// 将缩略图转换为 Base64 格式
thumbnailBase64 = convertImageToBase64(resizedImage);
grabber.stop();
} catch (Exception e) {
e.printStackTrace();
} finally {
grabber.close();
}
return thumbnailBase64;
}
/**
* 将Frame对象转换为BufferedImage对象
* @param frame 原始的Frame对象
* @return 转换后的BufferedImage对象
*/
private static RenderedImage frameToBufferedImage(Frame frame) {
// 创建Java2DFrameConverter对象,用于将Frame转换为BufferedImage
Java2DFrameConverter converter = new Java2DFrameConverter();
return converter.getBufferedImage(frame);
}
前端vue
<el-image @click="点击放大的方法" :src="后端返回的base码"
style="cursor: pointer; width: 25px; height: 25px;"></el-image>
**注意后端返回base码一定要拼接 data:image/**