Java 为什么我的屏幕外图像渲染不';不行?
我必须用Java创建一个小工具。 我有一个任务,将文本(单个字母)渲染到屏幕图像,并计算指定矩形内的所有白色和黑色像素Java 为什么我的屏幕外图像渲染不';不行?,java,image,Java,Image,我必须用Java创建一个小工具。 我有一个任务,将文本(单个字母)渲染到屏幕图像,并计算指定矩形内的所有白色和黑色像素 /*************************************************************************** * Calculate black to white ratio for a given font and letters *************************************************
/***************************************************************************
* 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;
}
/***************************************************************************
*计算给定字体和字母的黑白比例
**************************************************************************/
专用静态双计算因子(最终字体,
最终地图(重量){
最终字符[]字符=新字符[1];
双因子=0.0;
对于(最终Map.Entry:charWeights.entrySet()){
最终缓冲区图像=新缓冲区图像(高度、宽度、,
BuffereImage.TYPE_INT_ARGB);
chars[0]=entry.getKey();
最终图形=image.getGraphics();
graphics.setFont(字体);
图形.设置颜色(颜色.黑色);
绘图字符(字符,0,1,0,0);
最终双倍比率=CalculateBrackratio(image.getRaster());
因子+=(比率*条目.getValue());
}
返回因子/charWeights.size();
}
/***************************************************************************
*计数比率光栅
**************************************************************************/
专用静态双计算光栅(最终光栅){
final int maxX=raster.getMinX()+raster.getWidth();
final int maxY=raster.getMinY()+raster.getHeight();
int blackCounter=0;
int白计数器=0;
对于(int indexY=graster.getMinY();indexY
问题是raster.getSample始终返回0
我做错了什么?也许字符根本没有被画到图像上。如果我没记错的话,.drawChars()方法会绘制Y基线。因此,我认为您必须将字体高度添加到Y值。也许字符根本没有被绘制到图像中。如果我没记错的话,.drawChars()方法会绘制Y基线。因此,我认为必须将字体高度添加到Y值。如果我没有弄错的话,您可以在x=0,Y=0处绘制字符,其中x,Y是“此图形上下文坐标系中第一个字符[…]的基线” 因为基线位于字符的底部,所以可以在图像上方绘制它们。 使用x=0,y=高度。
另外,正确的构造函数是:
buffereImage(int-width,int-height,int-imageType)
:您可以反转宽度和高度。如果我没有弄错的话,您可以在x=0,y=0处绘制字符,其中x,y是“此图形上下文坐标系中第一个字符[…]的基线”
因为基线位于字符的底部,所以可以在图像上方绘制它们。
使用x=0,y=高度。另外,正确的构造器是:
buffereImage(int-width,int-height,int-imageType)
:您反转了宽度和高度。好的,PhiLho的nas Waverick的答案是正确的。
此外,我必须清除背景并将字体颜色更改为黑色:)
好的,PhiLho的回答是对的。 此外,我必须清除背景并将字体颜色更改为黑色:)
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);