Created
September 22, 2022 03:50
-
-
Save ponkotuy/0195b2460c0bc5c40d235f233bf7d142 to your computer and use it in GitHub Desktop.
良さげなサムネイル画像作るだけのコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sun.awt.image.ToolkitImage | |
import java.awt.image.{AreaAveragingScaleFilter, BufferedImage, FilteredImageSource, ImageObserver} | |
import java.awt.{Image, Toolkit, image} | |
import java.io.{ByteArrayOutputStream, File} | |
import java.nio.file.{Files, Path} | |
import javax.imageio.{IIOImage, ImageIO, ImageWriteParam} | |
case class ThumbnailGenerator(width: Int, height: Int) { | |
private[this] val filter = AreaAveragingScaleFilter(width, height) | |
def gen(path: Path): Array[Byte] = { | |
val reader = ImageIO.getImageReadersBySuffix("jpg").next() | |
reader.setInput(ImageIO.createImageInputStream(Files.newInputStream(path))) | |
val metadata = reader.getImageMetadata(0) | |
val image = reader.read(0) | |
val producer = new FilteredImageSource(image.getSource, filter) | |
val thumbnail = Toolkit.getDefaultToolkit.createImage(producer) | |
val baos = new ByteArrayOutputStream() | |
val ios = ImageIO.createImageOutputStream(baos) | |
val writer = ImageIO.getImageWritersByFormatName("jpeg").next() | |
writer.setOutput(ios) | |
val iwParam = writer.getDefaultWriteParam | |
iwParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT) | |
iwParam.setCompressionQuality(0.70f) | |
val ir = thumbnail.asInstanceOf[ToolkitImage].getImageRep | |
ir.reconstruct(ImageObserver.ALLBITS) | |
writer.write(null, new IIOImage(ir.getOpaqueRGBImage, null, metadata), iwParam) | |
baos.toByteArray | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment