[英]Scala image resize and crop
I have a Play framework 2.2 app and I want to resize and crop images (like profile picture). 我有一个Play framework 2.2应用程序,我想调整大小和裁剪图像(如个人资料图片)。 Is there a good Scala library that I can use to do this?
有一个很好的Scala库可以用来做这个吗? And if not which Java library should I use (considering performance, quality...).
如果不是我应该使用哪个Java库(考虑性能,质量......)。 An example would be appreciated.
一个例子将不胜感激。
https://github.com/sksamuel/scrimage是一个Scala图像库,它使用了一些众所周知的Java库。
I've used Thumbnaiator https://code.google.com/p/thumbnailator/ in Java before. 我之前在Java中使用过Thumbnaiator https://code.google.com/p/thumbnailator/ 。 Works really well and is pure Java so doesn't require any native dependencies.
工作得很好,是纯Java,所以不需要任何本机依赖。
If you just need to resize images, you don't need any addition library/dependencies. 如果您只需要调整图像大小,则不需要任何附加库/依赖项。 Just use the Java
BufferedImage
class so be enough. 只需使用Java
BufferedImage
类即可。
// 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.