자바로 작은 도구를 만들어야합니다. 화면상의 이미지에 텍스트 (단일 문자)를 렌더링하고 지정된 사각형 내 모든 흰색 및 검은 색 픽셀을 계산하는 작업이 있습니다.왜 오프 스크린 이미지 렌더링이 작동하지 않습니까?
/***************************************************************************
* 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;
}
probllem는 raster.getSample는 항상 0
이내가 무슨 일을 했는가 반환입니까?