2021与蓝度共同重构项目,服务端
liuhaonan
2022-11-09 061346cefe87c5612367a7910d165302e0330b02
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.sandu.common.file;
 
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.springframework.context.annotation.Configuration;
 
 
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
 
@Configuration
public class FileToThumbnail {
    /**
     * <p>Title: thumbnailImage</p>
     * <p>Description: 根据图片路径生成缩略图 </p>
     *
     * @param imagePath 原图片路径
     * @param w         缩略图宽
     * @param h         缩略图高
     * @param prevfix   生成缩略图的前缀
     * @param force     是否强制按照宽高生成缩略图(如果为false,则生成最佳比例缩略图)
     */
    public void getThumbnailForPic(String imagePath, int w, int h, String prevfix, boolean force) throws IOException {
        File imgFile = new File(imagePath);
        if (imgFile.exists()) {
            // ImageIO 支持的图片类型 : [BMP, bmp, jpg, JPG, wbmp, jpeg, png, PNG, JPEG, WBMP, GIF, gif]
            String types = Arrays.toString(ImageIO.getReaderFormatNames());
            String suffix = null;
            // 获取图片后缀
            if (imgFile.getName().indexOf(".") > -1) {
                suffix = imgFile.getName().substring(imgFile.getName().lastIndexOf(".") + 1);
            }
            // 类型和图片后缀全部小写,然后判断后缀是否合法
            if (suffix == null || types.toLowerCase().indexOf(suffix.toLowerCase()) < 0) {
                System.out.println("Sorry, the image suffix is illegal. the standard image suffix is {}." + types);
                return;
            }
            System.out.println("target image's size, width:{" + w + "}, height:{" + h + "}.");
            Image img = ImageIO.read(imgFile);
            if (!force) {
                // 根据原图与要求的缩略图比例,找到最合适的缩略图比例
                int width = img.getWidth(null);
                int height = img.getHeight(null);
                if ((width * 1.0) / w < (height * 1.0) / h) {
                    if (width > w) {
                        h = Integer.parseInt(new java.text.DecimalFormat("0").format(height * w / (width * 1.0)));
                    }
                } else {
                    if (height > h) {
                        w = Integer.parseInt(new java.text.DecimalFormat("0").format(width * h / (height * 1.0)));
                    }
                }
            }
            BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            Graphics g = bi.getGraphics();
            g.drawImage(img, 0, 0, w, h, Color.LIGHT_GRAY, null);
            g.dispose();
            String p = imgFile.getPath();
            // 将图片保存在原目录并加上前缀
            ImageIO.write(bi, suffix, new File(p.substring(0, p.lastIndexOf(File.separator)) + File.separator + prevfix + imgFile.getName()));
        }
    }
 
 
    /**
     * 通过PDFbox生成文件的缩略图
     *
     * @param filePath:文件路径
     * @param outPath:输出图片路径
     * @throws IOException
     */
    public String getThumbnailForPdf(String filePath, String outPath) throws IOException {
        // 利用PdfBox生成图像
        PDDocument pdDocument = PDDocument.load(new File(filePath));
        PDFRenderer renderer = new PDFRenderer(pdDocument);
        // 构造图片
        BufferedImage img_temp = renderer.renderImageWithDPI(0, 30, ImageType.RGB);
        ImageIO.write(img_temp, "png", new File(outPath));
        pdDocument.close();
        return outPath;
    }
 
    /**
     * @param sourcePath 保存路径
     * @return String 保存路径
     * @description //生成视频缩略图的URL地址
     */
    public String getThumbnailForVideo(InputStream inputStream, String sourcePath) throws IOException {
        File targetFile = new File(sourcePath);
 
        FFmpegFrameGrabber ff = new FFmpegFrameGrabber(inputStream);
        ff.start();
        // 视频总帧数
        int videoLength = ff.getLengthInFrames();
        Frame f = null;
        int i = 0;
        while (i < videoLength) {
            // 过滤前20帧,因为前20帧可能是全黑的
            // 这里看需求,也可以直接根据帧数取图片
            f = ff.grabFrame();
            if (i > 20 && f.image != null) {
                break;
            }
            i++;
        }
        int owidth = f.imageWidth;
        int oheight = f.imageHeight;
        // 对截取的帧进行等比例缩放
        int width = 300;
        int height = (int) (((double) width / owidth) * oheight);
        Java2DFrameConverter converter = new Java2DFrameConverter();
        BufferedImage fecthedImage = converter.getBufferedImage(f);
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        bi.getGraphics().drawImage(fecthedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH),
                0, 0, null);
        ImageIO.write(bi, "png", targetFile);
        // 查看stop源码会自动关流
        ff.stop();
        return sourcePath;
 
    }
 
 
}