开发者

Why does my offscreen image rendering doesn't work?

I have to create a little tool in Java. I have a task to render a text (single letter) to an ofscreen i开发者_开发百科mage and the count all the white and black pixels within a specified rectangle.

/***************************************************************************
 * Calculate black to white ratio for a given font and letters
 **************************************************************************/
private static double calculateFactor(final Font font,
        final Map<Character, Double> charWeights) {

    final char[] chars = new char[1];
    double factor = 0.0;

    for (final Map.Entry<Character, Double> entry : charWeights.entrySet()) {
        final BufferedImage image = new BufferedImage(height, width,
                BufferedImage.TYPE_INT_ARGB);
        chars[0] = entry.getKey();
        final Graphics graphics = image.getGraphics();
        graphics.setFont(font);
        graphics.setColor(Color.black);
        graphics.drawChars(chars, 0, 1, 0, 0);

        final double ratio = calculateBlackRatio(image.getRaster());
        factor += (ratio * entry.getValue());

    }
    return factor / charWeights.size();
}
/***************************************************************************
 * Count ration raster
 **************************************************************************/
private static double calculateBlackRatio(final Raster raster) {

    final int maxX = raster.getMinX() + raster.getWidth();
    final int maxY = raster.getMinY() + raster.getHeight();
    int blackCounter = 0;
    int whiteCounter = 0;

    for (int indexY = raster.getMinY(); indexY < maxY; ++indexY) {
        for (int indexX = raster.getMinX(); indexX < maxX; ++indexX) {

            final int color = raster.getSample(indexX, indexY, 0);
            if (color == 0) {
                ++blackCounter;
            } else {
                ++whiteCounter;
            }
        }
    }
    return blackCounter / (double) whiteCounter;
}

the probllem is that raster.getSample always returns 0.

What did I do wrong ?


If I am not mistaken, you draw chars at x=0, y=0 where x, y are "The baseline of the first character [...] in this graphics context's coordinate system." Since the baseline is at the bottom of the chars, you draw them above the image. Use x=0, y=height.
Also, the correct constructor is: BufferedImage(int width, int height, int imageType): you inverted width and height.


Perhaps the char isn't draw to the image at all. If I recall correctly the .drawChars() method draws to the Y-baseline. So you I think you must add the font height to the Y value.


OK PhiLho's nas Waverick's answers were right. Additionally I had to clear the background and change font color to black :)

final Graphics graphics = image.getGraphics();
            graphics.setFont(font);
            graphics.setColor(Color.white);
            graphics.fillRect(0, 0, width, height);
            graphics.setColor(Color.black);
            graphics.drawChars(chars, 0, 1, 0, height);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜