This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
Hi there
Say I have a Z shaped white mask and I want to crop an image to this shape. How do I do this in java?
Both images are 2560x1440, but the Z shaped mask has transparent bits.
My attempt in scala but using Java's awt library:
val imageBuff = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
val imageB = imageBuff.createGraphics()
imageB.drawImage(image, 0, 0, null)
val maskBuffered = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
val maskB = mask01Buffered.createGraphics()
maskB.drawImage(scaledmask, 0, 0, null)
def applyMask(imageBuffered: BufferedImage, maskBuffered: BufferedImage): BufferedImage = {
val imagePixels = heroBuffered.getRGB(0, 0, width, height, null, 0, width)
val maskPixels = maskBuffered.getRGB(0, 0, width, height, null, 0, width)
for (i <- 0 to maskPixels.length - 1)
{
val color = imagePixels(i)
val mask = maskPixels(i)
imagePixels(i) = mask | color
}
imageBuffered.setRGB(0, 0, width, height, imagePixels, 0, width)
imageBuffered
}
val maskApplied = applyMask(imageBuff, maskBuffered)
val scaledForeground: Image = maskApplied.getScaledInstance((width * 1.08).toInt, (height * 1.08).toInt, Image.SCALE_DEFAULT)
val finalImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
val f = finalImage.createGraphics
val newX: Int = (0 - width * 0.08).toInt
val newY: Int = (0 - height * 0.08).toInt
f.drawImage(scaledForeground, newX, newY, null)
This creates an image where I get the original image showing through the transparent bits of the mask and then the white of the mask overs the rest of the image, but what I really want is the other way around. I have tried having imagePixels(i) = color | mask
instead but I get the same result.
Does anyone have any other ideas?
Many thanks, J
Subreddit
Post Details
- Posted
- 2 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/javahelp/co...