Skip to content

Instantly share code, notes, and snippets.

@LiewJunTung
Created August 2, 2019 05:42
Show Gist options
  • Save LiewJunTung/72feac8c57d14f5360c0c81679dee09c to your computer and use it in GitHub Desktop.
Save LiewJunTung/72feac8c57d14f5360c0c81679dee09c to your computer and use it in GitHub Desktop.
package com.netvirta.ci_image_resize_plugin
import javax.imageio.ImageIO
import java.awt.image.BufferedImage;
import java.io.File
import javax.imageio.stream.ImageInputStream
class CiImageResize {
fun resize(inputJpgFilePath: String, outputJpgFilePath: String, outputWidth: Int, outputHeight: Int) {
val fileInputStream = File(inputJpgFilePath).inputStream()
val originalImage = ImageIO.read(fileInputStream)
val type = if (originalImage.type == 0){
BufferedImage.TYPE_INT_ARGB
} else {
originalImage.type
}
val resizedImage = resizeImage(originalImage, type, outputWidth, outputHeight)
val outputFile = File(outputJpgFilePath)
outputFile.parentFile.mkdirs()
ImageIO.write(resizedImage, "jpg", outputFile)
fileInputStream.close()
}
private fun resizeImage(originalImage: BufferedImage, type: Int, outputWidth: Int, outputHeight: Int): BufferedImage{
val resizedImage = BufferedImage(outputWidth, outputHeight, type)
val g = resizedImage.createGraphics()
g.drawImage(originalImage, 0, 0, outputWidth, outputHeight, null)
g.dispose()
return resizedImage
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment