文本形狀的圖像裁剪我需要在另一個圖像中剪出一個文本形狀的圖像。我認為最好用圖片來展示。文本圖像將始終是黑色的透明背景,并由此產生的剪除也應該有一個透明的背景。兩個輸入圖像也將是相同的大小。
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個贊
GlyphVector
Font
public GlyphVector layoutGlyphVector(FontRenderContext frc, char[] text, int start, int limit, int flags) {
Shape
getOutline()
Shape
Graphics
添加回答
舉報
0/150
提交
取消