文本形状的图像裁剪我需要在另一个图像中剪出一个文本形状的图像。我认为最好用图片来展示。文本图像将始终是黑色的透明背景,并由此产生的剪除也应该有一个透明的背景。两个输入图像也将是相同的大小。
3 回答
浮云间
TA贡献1829条经验 获得超4个赞
public static BufferedImage textEffect(BufferedImage image, BufferedImage text) {
if (image.getWidth() != text.getWidth() ||
image.getHeight() != text.getHeight())
{
throw new IllegalArgumentException("Dimensions are not the same!");
}
BufferedImage img = new BufferedImage(image.getWidth(),
image.getHeight(),
BufferedImage.TYPE_INT_ARGB_PRE);
for (int y = 0; y < image.getHeight(); ++y) {
for (int x = 0; x < image.getWidth(); ++x) {
int textPixel = text.getRGB(x, y);
int textAlpha = (textPixel & 0xFF000000);
int sourceRGB = image.getRGB(x, y);
int newAlpha = (int) (((textAlpha >> 24) * (sourceRGB >> 24)) / 255d);
int imgPixel = (newAlpha << 24) | (sourceRGB & 0x00FFFFFF);
int rgb = imgPixel | textAlpha;
img.setRGB(x, y, rgb);
}
}
return img;}
跃然一笑
TA贡献1826条经验 获得超6个赞
GlyphVectorFont
public GlyphVector layoutGlyphVector(FontRenderContext frc,
char[] text,
int start,
int limit,
int flags) {ShapegetOutline()
ShapeGraphics
添加回答
举报
0/150
提交
取消
