Created
January 5, 2018 05:40
-
-
Save nakaly/0718a4c2cd1477296878c66775ff7e8a to your computer and use it in GitHub Desktop.
BlurredImage
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 main.java.com.linecorp.nakaly; | |
import java.awt.Graphics2D; | |
import java.awt.Transparency; | |
import java.awt.image.BufferedImage; | |
import java.awt.image.BufferedImageOp; | |
import java.awt.image.ConvolveOp; | |
import java.awt.image.Kernel; | |
import java.io.ByteArrayOutputStream; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.UncheckedIOException; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.util.Base64; | |
import javax.imageio.ImageIO; | |
public class BlurredImage { | |
private static final int IMG_WIDTH = 100; | |
private static final int IMG_HEIGHT = 200; | |
private static final int RESIZE_RATIO = 5; | |
public static void main(String... args) throws IOException { | |
Path imageFilePath = Paths.get("/Users/ST20142/rhq/github.com/nakaly/sandbox/src/main/resource/", "line_login.png"); | |
BufferedImage bufferedImage = ImageIO.read(imageFilePath.toFile()); | |
float[] matrix = new float[400]; | |
for (int i = 0; i < 400; i++) | |
matrix[i] = 1.0f/400.0f; | |
Kernel kernel = new Kernel(3, 3, | |
new float[] { | |
1f/9f, 1f/9f, 1f/9f, | |
1f/9f, 1f/9f, 1f/9f, | |
1f/9f, 1f/9f, 1f/9f}); | |
BufferedImageOp op = new ConvolveOp( new Kernel(20, 20, matrix), ConvolveOp.EDGE_NO_OP, null ); | |
BufferedImage blurredImage = op.filter(bufferedImage, null); | |
int type = (blurredImage.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; | |
int width = blurredImage.getWidth() / RESIZE_RATIO; | |
int height = blurredImage.getHeight() / RESIZE_RATIO; | |
BufferedImage resizedImage = new BufferedImage(width, height, type); | |
Graphics2D g = resizedImage.createGraphics(); | |
g.drawImage(bufferedImage, 0, 0, width, height, null); | |
g.dispose(); | |
File outputfile = new File("blurred_image.png"); | |
ImageIO.write(resizedImage, "png", outputfile); | |
// final ByteArrayOutputStream os = new ByteArrayOutputStream(); | |
// try | |
// { | |
// ImageIO.write(blurredImage, "png", os); | |
// System.out.println(Base64.getEncoder().encodeToString(os.toByteArray())); | |
// } | |
// catch (final IOException ioe) | |
// { | |
// throw new UncheckedIOException(ioe); | |
// } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment