{"id":2672,"date":"2015-11-02T22:22:00","date_gmt":"2015-11-02T22:22:00","guid":{"rendered":"https:\/\/www.htmlgoodies.com\/uncategorized\/create-high-quality-thumbnails-using-the-imgscalr-library\/"},"modified":"2015-11-02T22:22:00","modified_gmt":"2015-11-02T22:22:00","slug":"create-high-quality-thumbnails-using-the-imgscalr-library","status":"publish","type":"post","link":"https:\/\/www.htmlgoodies.com\/java\/create-high-quality-thumbnails-using-the-imgscalr-library\/","title":{"rendered":"Create High Quality Thumbnails using the imgscalr Library"},"content":{"rendered":"
Just about any Web or mobile app that works with images needs to display thumbnails. In fact, we saw such an app in the Display a Thumbnail using the Vaadin Application FileUpload Control<\/a> tutorial. That app employed the Image.getScaledInstance() method using the SCALE_SMOOTH algorithm. The Java 2D team has been encouraging developers to move away from Image.getScaledInstance() to more modern APIs due to its poor performance. Native Java offers many alternatives, but sadly, none of them can generate as high quality thumbnails as the Image.getScaledInstance() + SCALE_AREA_AVERAGING combo. Therefore, if you need to quickly resize or manipulate images using the most optimal methods, then you should strongly consider using a specialized library like imgscalr. As the name implies, it was created for one purpose and one purpose only: to resize images quickly, easily, and well. In today’s article, we’ll learn how to use imgscalr to resize, crop, pad, and even rotate images.<\/p>\n Imgscalr<\/a> may be downloaded from their website under Download<\/a> section. It is available at no cost under the Apache 2 License. All Download bundles include the library JAR, source code and Javadocs.<\/p>\n The only jar that your project needs to reference is the imgscalr-lib-x.x.jar where x.x is the exact version number. At the time of this writing, version 4.2 is most current.<\/p>\n Image resizing is achieved via several overloaded versions of the static Scalr.resize() method. The simplest accepts two arguments: the BufferedImage and targetSize int. That will resize a given image while maintaining its original proportion to a width and height no bigger than the targetSize:<\/p>\n For more fine-grained control over how our image is scaled, other attributes such as quality and filtering can be set via additional parameters:<\/p>\n Here is what each of the above parameters specifies:<\/p>\n By default imgscalr always honors the image’s original proportions above all else. If either of the target dimensions are wrong, imgscalr will re-calculate proper dimensions honoring the primary dimension of the image first, based on the image’s orientation. To force imgscalr to resize an image into new dimensions, use the FIT_EXACT Scalr Mode.<\/p>\n Since all of imgscalr’s method’s are static, you can add a static import for the entire library to remove all of the Scalr.” object prefixes in your code. Here is the previous example rewritten with a static import:<\/p>\n Imgscalr can also be utilized to pad, crop, and rotate images.<\/p>\n For instance, the following code invokes the pad() method to add a black border of 2 pixels around a resized image:<\/p>\nDownload and Installation<\/h2>\n
Resing Images<\/h2>\n
File image = new File(\"C:\\Users\\Public\\Pictures\\Sample Pictures\\mypicture.jpg\");\nBufferedImage img = ImageIO.read(image); \/\/ load image\n\/\/resize to 150 pixels max\nBufferedImage thumbnail = Scalr.resize(image, 150);\n<\/pre>\n
BufferedImage thumbnail = Scalr.resize(image,\n Scalr.Method.SPEED,\n Scalr.Mode.FIT_TO_WIDTH,\n 150,\n 100,\n Scalr.OP_ANTIALIAS);\n<\/pre>\n
\n
Reducing Source Code<\/h2>\n
import static org.imgscalr.Scalr.*;\n \nBufferedImage thumbnail = resize(image,\n Method.SPEED,\n Mode.FIT_TO_WIDTH,\n 150,\n 100,\n OP_ANTIALIAS);\n<\/pre>\n
Other Useful Methods<\/h2>\n
import org.imgscalr.Scalr.*;\nimport java.awt.Color;\npublic static BufferedImage createThumbnail(BufferedImage img) {\n \/\/ Target width of 500x500 is used\n img = resize(img, 500);\n return pad(img, 2, Color.BLACK);\n}\n<\/pre>\n