Hello, everyone. I'm brother Qiang.
I wonder if you have ever used java to process pictures in your work. After graduating from college, brother Qiang was engaged in server-side WEB development and rarely contacted image processing. Most of those who have contact with pictures are the uploading and downloading of pictures. Therefore, I have little contact with the technologies related to Java processing pictures.
However, Bing dwen dwen, who was able to draw an ice pier with characters, was attracted by the technology used in the article. Among them, Java's native image processing class: Java awt. Graphics2d and image Getscaledinstance is used to obtain the image thumbnail. Here is a brief introduction:
java.awt.Graphics2D resizes the picture.
Graphics2D is a basic class provided by the Java platform that can render two-dimensional shapes, text and images. The following is a simple example of image resizing using Graphics2D.
/** * Picture zoom * * @param srcImagePath Picture path * @param targetWidth Target width * @return * @throws IOException */ public static BufferedImage resizeImage(String srcImagePath, int targetWidth) throws IOException { Image srcImage = ImageIO.read(new File(srcImagePath)); int targetHeight = getTargetHeight(targetWidth, srcImage); BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = resizedImage.createGraphics(); graphics2D.drawImage(srcImage, 0, 0, targetWidth, targetHeight, null); graphics2D.dispose(); return resizedImage; } /** * Calculates the proportional height based on the specified width * * @param targetWidth Target width * @param srcImage Picture information * @return */ private static int getTargetHeight(int targetWidth, Image srcImage) { int targetHeight = srcImage.getHeight(null); if (targetWidth < srcImage.getWidth(null)) { targetHeight = Math.round((float)targetHeight / ((float)srcImage.getWidth(null) / (float)targetWidth)); } return targetHeight; }
Resized pictures can be saved in the following ways.
BufferedImage image = resizeImage("/Users/darcy/Downloads/bingdundun.jpeg", 200); File file = new File("/Users/darcy/Downloads/bingdundun_resize.jpg"); ImageIO.write(image, "jpg", file);
Using image Getscaledinstance resizes the picture.
This is another way for Java Native functions to adjust the picture size. Using this way to adjust the picture size is simple and convenient, the generated picture quality is also good, and the code is relatively simple, but this method is not efficient.
/** * Picture zoom * * @param srcImagePath Picture path * @param targetWidth Target width * @return * @throws IOException */ public static BufferedImage resizeImage2(String srcImagePath, int targetWidth) throws IOException { Image srcImage = ImageIO.read(new File(srcImagePath)); int targetHeight = getTargetHeight(targetWidth, srcImage); Image image = srcImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_DEFAULT); BufferedImage bufferedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB); bufferedImage.getGraphics().drawImage(image, 0, 0, null); return bufferedImage; } // getTargetHeight is the same as Java awt. Sample code in graphics2d
Next, zoom the Bing dwen dwen of the 416 x 500 to 200 x 240.
I don't know if you will feel confused after seeing the above code, and the code is very lengthy and not humanized enough. No wonder few people use Java to process pictures.
So is there any other elegant, concise and easy-to-use way to deal with picture thumbnails?
How to use one line of code to deal with pictures
Adhering to the principle of open source first, after a search by brother Qiang. Hey, hey, I really found it. That's it: thumbnail, a Java library that specializes in thumbnails.
Thumbnail can be used to perform fairly complex thumbnail processing tasks in a simple step.
For example, creating JPEG thumbnails of image files in the directory and adjusting them to the maximum size of 640 pixels x 480 pixels while retaining the aspect ratio of the original image can be performed in the following ways:
Thumbnails.of(new File("path/to/directory").listFiles()) .size(640, 480) .outputFormat("jpg") .toFiles(Rename.PREFIX_DOT_THUMBNAIL);
Look at similar effects:
It's good. It's really done to generate picture thumbnails in one line of code.
Moreover, we do not need to access the Image I/O API and manually operate BufferedImages through objects. Thumbnailator does all this for us. We just need to import the Jar package of thumbnailator and add the above code. Users of Maven project can add dependencies in the following ways:
<dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</artifactId> <version>[0.4, 0.5)</version> </dependency>
Come on, present the open source address on GitHub: GitHub - coobird/thumbnailator: Thumbnailator - a thumbnail generation library for Java
Write at the end
Interested partners can download it for use. But thank you for that article: Bing dwen dwen, I draw a block of ice with characters.
Really, sometimes thinking and inspiration are more important than the tools we use.