Last active
December 17, 2015 12:48
-
-
Save kazuhito-m/5611980 to your computer and use it in GitHub Desktop.
画像に文字を書く。※標準で、短く、どこでもいける感じに?
This file contains hidden or 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 java.awt.Color; | |
import java.awt.Graphics; | |
import java.awt.Rectangle; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import javax.imageio.ImageIO; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class ImageStringDrawTest { | |
@Test | |
public final void test() { | |
BufferedImage img = null; | |
try { | |
String text = "へもかわ"; | |
File gif = new File("logo.gif"); | |
img = ImageIO.read(gif); | |
Graphics g = img.createGraphics(); | |
Rectangle rectText = g.getFontMetrics().getStringBounds(text, g) | |
.getBounds(); | |
int x = (img.getWidth() - rectText.width) / 2; | |
g.setColor(Color.RED); | |
g.drawString(text, x, 56); | |
if (!ImageIO.write(img, "gif", gif)) { | |
System.out.println("書き込みに失敗しました"); | |
} | |
} catch (Exception e) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment