当前位置:网站首页>Processing picture class library

Processing picture class library

2022-06-25 09:38:00 String-int

POM.XML

<!-- https://mvnrepository.com/artifact/net.coobird/thumbnailator -->
        <!-- Processing picture class library -->
        <dependency>
            <groupId>net.coobird</groupId>
            <artifactId>thumbnailator</artifactId>
            <version>0.4.14</version>
        </dependency>

JAVA

package com.example.demo;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import net.coobird.thumbnailator.name.Rename;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class TestImg {
    
    public static void main(String[] args) throws IOException {
    
        //size( Width ,  Height )
        /* *  If the horizontal ratio of the picture 200 Small , High ratio 300 Small , unchanged  *  If the horizontal ratio of the picture 200 Small , High ratio 300 Big , The height is reduced to 300, The scale of the picture is the same  *  If the horizontal ratio of the picture 200 Big , High ratio 300 Small , Horizontal reduction to 200, The scale of the picture is the same  *  If the horizontal ratio of the picture 200 Big , High ratio 300 Big , The picture is scaled down , Horizontal 200 Or as high as 300 */
//  Scene application 
        /** *1. Specify the size to zoom  */

        Thumbnails.of("D:\\project\\suining\\processingimgs\\src\\main\\resources\\static\\images/junma.jpg")
                .size(200, 300)
                .toFile("C:\\Users\\Administrator\\Desktop\\tu/junma.jpg");

// Thumbnails.of("D:\\project\\suining\\processingimgs\\src\\main\\resources\\static\\images/0.1.jpeg")
// .size(2560, 2048)
// .toFile("C:\\Users\\Administrator\\Desktop\\tu/0.1.jpeg");

        /** * 2. Single picture is scaled equally  */
// File file = new File("D:\\\\project\\\\suining\\\\processingimgs\\\\src\\\\main\\\\resources\\\\static\\\\images/0.2.jpg");
// // 3.0 It's a double Type of number , Zoom ratio , Greater than 1 Just get bigger , Less than 1 Is to shrink 
// Thumbnails.of(new FileInputStream(file)).scale(0.1).toFile(new File("C:\\\\Users\\\\Administrator\\\\Desktop\\\\tu/0.2.jpg"));

        /** * 3. Not in proportion , Specify the size to zoom  */
        //keepAspectRatio(false)  The default is true Scaled ,false Do not scale 
// Thumbnails.of("D:\\\\project\\\\suining\\\\processingimgs\\\\src\\\\main\\\\resources\\\\static\\\\images/0.4.jpeg")
// .size(200, 300)
// .keepAspectRatio(false)
// .toFile("C:\\\\\\\\Users\\\\\\\\Administrator\\\\\\\\Desktop\\\\\\\\tu/0.4.jpg");

        /** * 4. Generate thumbnails in batch  */
        //C:\Users\Administrator\Desktop\tu  Is the folder to generate thumbnails , The pictures in the folder will be generated into thumbnails ,png It's the format of the picture 
        Thumbnails.of(new File("C:\\Users\\Administrator\\Desktop\\tu").listFiles()).scale(0.2).outputFormat("jpg")
                .toFiles(Rename.PREFIX_DOT_THUMBNAIL);

        /** * 5.  Picture quality control , The picture size remains the same  */
// File fromPic =new File("D:\\project\\suining\\processingimgs\\src\\main\\resources\\static\\images/0.5.jpeg");
// File toPic =new File("C:\\Users\\Administrator\\Desktop\\tu/0.5.jpeg");
// //outputQuality It is used to control the image quality 
// Thumbnails.of(fromPic).scale(1f).outputQuality(0.25f).toFile(toPic);
//
            /** * *  failed !!!!!! * 6. Watermark the picture  * fromPic It's the original picture ,waterPic Is a watermark image ,toPic Is the generated picture  */

// File fromPic =new File("D:\\project\\suining\\processingimgs\\src\\main\\resources\\static\\images/junma.jpg");
// File waterPic =new File("C:\\Users\\Administrator\\Desktop\\tu/thumbnail.0.4.jpg.png");
// File toPic =new File("C:\\Users\\Administrator\\Desktop\\tu/haha.jpg");
// Thumbnails.of(fromPic).scale(0.8)
// .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(waterPic), 0.5f)
// .outputQuality(0.8f).toFile(toPic);
//
watermark( Location , Watermark , transparency )
// Thumbnails.of("images/a380_1280x1024.jpg")
// .size(1280, 1024)
// .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("images/watermark.png")), 0.5f)
// .outputQuality(0.8f)
// .toFile("c:/a380_watermark_bottom_right.jpg");
//
// Thumbnails.of("images/a380_1280x1024.jpg")
// .size(1280, 1024)
// .watermark(Positions.CENTER, ImageIO.read(new File("images/watermark.png")), 0.5f)
// .outputQuality(0.8f)
// .toFile("c:/a380_watermark_center.jpg");

        /** * 7. Rotate the picture  */
// File fromPic =new File("D:\\project\\suining\\processingimgs\\src\\main\\resources\\static\\images/junma.jpg");
// File toPic =new File("C:\\Users\\Administrator\\Desktop\\tu/thumbnail.0.4.jpg");
// Thumbnails.of(fromPic).scale(0.5).rotate(90).toFile(toPic);

        /** *  Image clipping  */
// File fromPic =new File("D:\\project\\suining\\processingimgs\\src\\main\\resources\\static\\images/junma.jpg");
// File toPic =new File("C:\\Users\\Administrator\\Desktop\\tu/thumbnail.0.4.jpg");
// Thumbnails.of(fromPic).sourceRegion(Positions.CENTER, 300, 300).scale(1.0).toFile(toPic);
//
// //sourceRegion()
//
// // Picture center 400*400 Region 
// Thumbnails.of("D:\\project\\suining\\processingimgs\\src\\main\\resources\\static\\images/junma.jpg")
// .sourceRegion(Positions.CENTER, 400,400)
// .size(200, 200)
// .keepAspectRatio(false)
// .toFile("C:\\Users\\Administrator\\Desktop\\tu/a380_region_center.jpg");
//
// // Bottom right of the picture 400*400 Region 
// Thumbnails.of("D:\\project\\suining\\processingimgs\\src\\main\\resources\\static\\images/junma.jpg")
// .sourceRegion(Positions.BOTTOM_RIGHT, 400,400)
// .size(200, 200)
// .keepAspectRatio(false)
// .toFile("C:\\Users\\Administrator\\Desktop\\tu/a380_region_bootom_right.jpg");
//
// // Specify coordinates 
// Thumbnails.of("D:\\project\\suining\\processingimgs\\src\\main\\resources\\static\\images/junma.jpg")
// .sourceRegion(600, 500, 400, 400)
// .size(200, 200)
// .keepAspectRatio(false)
// .toFile("C:\\Users\\Administrator\\Desktop\\tu/a380_region_coord.jpg");

//  Images uploaded by some apps may be of high quality , But when users browse the list , I don't want to show the original picture , Because the bandwidth requirements are high ,
//  This can reduce the picture quality ( As mentioned above outputQuality), With outputstream The way of output stream response Show the browser 
//  narrow scale(0.5)、  quality outputQuality(0.1)
//  narrow 0.5, quality 0.1, The output image size is only 6.2KB 了 , Greatly accelerated web The speed of browsing pictures 



    }
}

package com.example.demo;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.name.Rename;

import java.io.File;
import java.io.IOException;

public class ThumbnailatorTest {
    
    public static void main(String[] args) throws IOException {
    
// File directory=new File("C:\\Users\\Administrator\\Desktop\\tu");
// Thumbnails.of(directory.listFiles())
// .size(200, 200)
// .outputFormat("jpeg")
// .asFiles(Rename.PREFIX_DOT_THUMBNAIL);

        Thumbnails.of(new File("C:\\Users\\Administrator\\Desktop\\tu").listFiles())
                .size(300, 200).keepAspectRatio(false).outputFormat("jpg")
               .toFiles(Rename.PREFIX_DOT_THUMBNAIL);
    }
}

package com.example.demo;

import com.sun.istack.internal.logging.Logger;
import jdk.management.resource.internal.inst.InitInstrumentation;
import net.coobird.thumbnailator.Thumbnailator;
import net.coobird.thumbnailator.Thumbnails;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

@RestController
public class TestController {
    

    @RequestMapping("test")
    public void getImg(HttpServletRequest request, HttpServletResponse response) throws IOException {
    

        Thumbnails.of(new File("D:\\project\\suining\\processingimgs\\src\\main\\resources\\static\\images/0.1.jpeg"))
                .size(300,200).outputFormat("jpg").toOutputStream(response.getOutputStream());
    }
}

原网站

版权声明
本文为[String-int]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250827593304.html