Created
August 2, 2019 05:42
-
-
Save LiewJunTung/72feac8c57d14f5360c0c81679dee09c 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
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