java版泰囧神情的生成器代码,不用PS也能恶搞
java版泰囧表情的生成器代码,不用PS也能恶搞。
下载图片后放在F盘,直接运行即可
import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public final class ImageUtils { private static final String PICTRUE_FORMATE_JPG = "jpg"; private ImageUtils() {} public static void pressText(String targetImg, String pressText1,String pressText2, String pressText3, int fontSize, Color color) { try { File file = new File(targetImg); File dstFile = new File(targetImg + "_dst.jpg"); Image image = ImageIO.read(file); int width = image.getWidth(null); int height = image.getHeight(null); BufferedImage bufferedImage = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB); Graphics2D g = bufferedImage.createGraphics(); g.drawImage(image, 0, 0, width, height, null); g.setFont(new Font("微软雅黑", Font.BOLD, fontSize)); g.setColor(color); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,1)); int x = 135; int y = 163; int width_1 = fontSize * getLength(pressText1); int height_1 = fontSize; int widthDiff = width - width_1; int heightDiff = height - height_1; if (x < 0) { x = widthDiff / 2; } else if (x > widthDiff) { x = widthDiff; } if (y < 0) { y = heightDiff / 2; } else if (y > heightDiff) { y = heightDiff; } g.drawString(pressText1, x, y + height_1); x = 135; y = 355; width_1 = fontSize * getLength(pressText1); height_1 = fontSize; widthDiff = width - width_1; heightDiff = height - height_1; if (x < 0) { x = widthDiff / 2; } else if (x > widthDiff) { x = widthDiff; } if (y < 0) { y = heightDiff / 2; } else if (y > heightDiff) { y = heightDiff; } g.drawString(pressText2, x, y + height_1); x = 135; y = 550; width_1 = fontSize * getLength(pressText1); height_1 = fontSize; widthDiff = width - width_1; heightDiff = height - height_1; if (x < 0) { x = widthDiff / 2; } else if (x > widthDiff) { x = widthDiff; } if (y < 0) { y = heightDiff / 2; } else if (y > heightDiff) { y = heightDiff; } g.drawString(pressText3, x, y + height_1); g.dispose(); ImageIO.write(bufferedImage, PICTRUE_FORMATE_JPG, dstFile); } catch (Exception e) { e.printStackTrace(); } } public static int getLength(String text) { int textLength = text.length(); int length = textLength; for (int i = 0; i < textLength; i++) { if (String.valueOf(text.charAt(i)).getBytes().length > 1) { length++; } } return (length % 2 == 0) ? length / 2 : length / 2 + 1; } public static void main(String[] args) throws IOException { String taijiongPath = "F:/taijiong/taijiong.jpg"; pressText(taijiongPath, "你想泡妞吗?", "赶快打扮一下!","哈哈哈哈!打扮你也泡不到!", 18, Color.WHITE); } }
- 4楼Gwanwlw前天 09:49
- 不错哦,,能否讲解一下大致思路?我没学过java,只学了c++,这个用c++应该能实现吧??
- Re: software0116昨天 12:21
- 和生成验证码原理差不多
- 3楼kbj15924天前 19:36
- 你好,如果我想把这个放到网页里,应该怎么做呢?
- Re: software01164天前 21:22
- 我做一个网页版的,上传上去,你自己看.
- 2楼LIU0052304天前 17:56
- 哈哈,学会了,谢谢分享!
- 1楼Wentasy4天前 11:00
- 想法不错。