I have a Play framework 2.2 app and I want to resize and crop images (like profile picture). Is there a good Scala library that I can use to do this? And if not which Java library should I use (considering performance, quality...). An example would be appreciated.
I've used Thumbnaiator https://code.google.com/p/thumbnailator/ in Java before. Works really well and is pure Java so doesn't require any native dependencies.
If you just need to resize images, you don't need any addition library/dependencies. Just use the Java BufferedImage
class so be enough.
// Target size
val width = 100
val height = 100
// Load image from disk
val originalImage: BufferedImage = ImageIO.read(new File("test-data/test.jpg"))
// Resize
val resized = originalImage.getScaledInstance(width, height, Image.SCALE_DEFAULT)
// Saving Image back to disck
val bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
bufferedImage.getGraphics.drawImage(resized, 0, 0, null)
ImageIO.write(bufferedImage, "JPEG", new File("test-data/resized.jpg"))
You'll need these imports:
import java.awt.image.BufferedImage
import java.io.File
import javax.imageio.ImageIO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With