天天看點

基于Spring Boot實作圖檔上傳/加水印一把梭操作

文章共 537字,閱讀大約需要 2分鐘 !

概述

很多網站的圖檔為了版權考慮都加有水印,尤其是那些圖檔類網站。自己正好最近和圖檔打交道比較多,是以就探索了一番基于 Spring Boot這把利器來實作從 圖檔上傳 → 圖檔加水印 的一把梭操作!

注: 本文首發于 My Personal Blog:程式羊 ,歡迎光臨 小站

本文内容腦圖如下:

搭建 Spring Boot基礎工程

過程不再贅述了,這裡給出 pom中的關鍵依賴:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>
    </dependencies>           

編寫檔案上傳服務

  • 主要就是編寫 ImageUploadService 服務

裡面僅一個上傳圖檔的方法:

uploadImage

方法

/**
     * 功能:上傳圖檔
     * @param file 檔案
     * @param uploadPath 伺服器上上傳檔案的路徑
     * @param physicalUploadPath  伺服器上上傳檔案的實體路徑
     * @return 上傳檔案的 URL相對位址
     */
    public String uploadImage( MultipartFile file, String uploadPath, String physicalUploadPath ) {

        String filePath = physicalUploadPath + file.getOriginalFilename();

        try {
            File targetFile=new File(filePath);
            FileUtils.writeByteArrayToFile(targetFile, file.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return uploadPath + "/" + file.getOriginalFilename();
    }
}           

編寫圖檔加水印服務

  • 編寫 ImageWatermarkService 服務

裡面就一個主要的

watermarkAdd

方法,代碼後面寫有詳細解釋

@Service
public class ImageWatermarkService {

    /**
     * imgFile 圖像檔案
     * imageFileName 圖像檔案名
     * uploadPath 伺服器上上傳檔案的相對路徑
     * realUploadPath 伺服器上上傳檔案的實體路徑
     */
    public String watermarkAdd( File imgFile, String imageFileName, String uploadPath, String realUploadPath ) {

        String imgWithWatermarkFileName = "watermark_" + imageFileName;
        OutputStream os = null;

        try {
            Image image = ImageIO.read(imgFile);

            int width = image.getWidth(null);
            int height = image.getHeight(null);

            BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);  // ①
            Graphics2D g = bufferedImage.createGraphics();  // ②
            g.drawImage(image, 0, 0, width,height,null);  // ③

            String logoPath = realUploadPath + "/" + Const.LOGO_FILE_NAME;  // 水印圖檔位址
            File logo = new File(logoPath);        // 讀取水印圖檔
            Image imageLogo = ImageIO.read(logo);

            int markWidth = imageLogo.getWidth(null);    // 水印圖檔的寬度和高度
            int markHeight = imageLogo.getHeight(null);

            g.setComposite( AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, Const.ALPHA) );  // 設定水印透明度
            g.rotate(Math.toRadians(-10), bufferedImage.getWidth()/2, bufferedImage.getHeight()/2);  // 設定水印圖檔的旋轉度

            int x = Const.X;
            int y = Const.Y;

            int xInterval = Const.X_INTERVAL;
            int yInterval = Const.Y_INTERVAL;

            double count = 1.5;
            while ( x < width*count ) {  // 循環添加多個水印logo
                y = -height / 2;
                while( y < height*count ) {
                    g.drawImage(imageLogo, x, y, null);  // ④
                    y += markHeight + yInterval;
                }
                x += markWidth + xInterval;
            }

            g.dispose();

            os = new FileOutputStream(realUploadPath + "/" + imgWithWatermarkFileName);
            JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os); // ⑤
            en.encode(bufferedImage); // ⑥

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return uploadPath + "/" + imgWithWatermarkFileName;
    }

}           

代碼思路解釋如下:

可以對照代碼中的标示數字和下面的解釋進行了解:

① 建立緩存圖檔

② 建立繪圖工具

③ 将原圖繪制到緩存圖檔

④ 将水印logo繪制到緩存圖檔

⑤ 建立圖像編碼工具類

⑥ 編碼緩存圖像生成目标圖檔

可見思路清晰易懂!

編寫 圖檔上傳/處理 控制器

我們在該控制器代碼中将上述的 圖檔上傳服務 和 圖檔加水印服務 給用起來:

@RestController
public class WatermarkController {

    @Autowired
    private ImageUploadService imageUploadService;

    @Autowired
    private ImageWatermarkService watermarkService;

    @RequestMapping(value = "/watermarktest", method = RequestMethod.POST)
    public ImageInfo watermarkTest( @RequestParam("file") MultipartFile image ) {

        ImageInfo imgInfo = new ImageInfo();

        String uploadPath = "static/images/";  // 伺服器上上傳檔案的相對路徑
        String physicalUploadPath = getClass().getClassLoader().getResource(uploadPath).getPath();  // 伺服器上上傳檔案的實體路徑

        String imageURL = imageUploadService.uploadImage( image, uploadPath, physicalUploadPath );
        File imageFile = new File(physicalUploadPath + image.getOriginalFilename() );

        String watermarkAddImageURL = watermarkService.watermarkAdd(imageFile, image.getOriginalFilename(), uploadPath, physicalUploadPath);

        imgInfo.setImageUrl(imageURL);
        imgInfo.setLogoImageUrl(watermarkAddImageURL);
        return imgInfo;
    }
}           

實際實驗與效果展示

我們用 Postman工具來輔助我們發出

localhost:9999/watermarktest

請求,進行圖檔上傳的操作:

之後我們再去項目的資源目錄下檢視上傳的原圖 和 加完水印後圖檔的效果如下:

喔唷,這水印 Logo是不是打的有點多...

不過這下終于不用害怕别人對您的圖檔侵權啦 !

後記

由于能力有限,若有錯誤或者不當之處,還請大家批評指正,一起學習交流!