I'm trying to get the XY coordinates of the pixels in a string in Java, but I can't figure out how.

I'll try to explain more clearly.

Let's say I want to do something like this:

paint.setTextSize(40);
canvas.drawText("hello", 100, 100, paint);

But instead of actually drawing that text, I want a list of coordinates corresponding to all the pixels that would be in that text, which can be relative to the beginning of the text or the canvas, doesn't matter.

Is this at all possible? Can't find anything about it with Google.

Thanks.

有帮助吗?

解决方案

I doubt whether is is even possible without great effort.

For one this depends on what font you are using.

The only way I can imagine it, is by drawing the text internally on a BufferedImage and then check each pixel for its color.

Say, draw it on a white background with black text color.

Pseudo code:

for each pixel row{
    for each pixel column{
        if(pixel.getColor() == (new Color()).black)
           save coordinates
    }
}

其他提示

I think this sdhould answer your question: Calculate text size according to width of text area

You can determine text sizes, but indivudual pixels are dependding of typeface and parameters like weight etc.

In case you need boundaries of individual characters, you can work with substrings

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top