1. 有关java的awt或者swing实现在图片上添加文字的处理(像素和字符串长度单位转换的问题)
先用java.awt.Graphics2D.getFont()取得当前正在使用的字体f,java.awt.Graphics2D.getFontRenderContext()取得当前正在使用的渲染上下文frc
然后再用Rectangle2D rect=f.getStringBounds(str,frc)就可以得到你渲染这些文字所占据的矩形,使用rect.getWidth(), rect,getHeight()就可以知道这个矩形的大小了。
反过来通过像素大小决定字符串的长度没有现成的方法。如果你的字体是等宽的可以先用上面的方法求出一个字符的大小然后自己算,不然就只能先试试一个字符,再试试两个字符……直到超过你要的长度就返回的办法了(看起来效率比较低,不过实际使用的时候不是很影响性能)
2. Java如何读取BMP的每个像素点,输出到一个二维数组
楼上的基本正确,
问题一:
int[] rgb = new int[3];最好用二维数组
int[] rgb = new int[3][width*height]
问题二:
rgb[0] = (pixel & 0xff0000 ) >> 16 ;
rgb[1] = (pixel & 0xff00 ) >> 8 ;
rgb[2] = (pixel & 0xff );
会把数组内的值覆盖,获得就是最后像素点的RGB值;
我写了一个希望可以帮助到你
package imageReadAndWrite;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* JPG File reader/writer. Uses native com.sun libraries (which may deprecate at
* any time)
*
*
* @author PhoenixZJG
* @version 1.0
*/
public class JPGFile implements xxxFile {
private int[] ints = null;
private byte bytes[] = null; // bytes which make up binary PPM image
private double doubles[] = null;
private int[][] imageRGB = null;
private String filename = null; // filename for PPM image
private int height = 0;
private int width = 0;
/**
* Read the PPM File.
*
* @throws FileNotFoundException
* if the directory/image specified is wrong
* @throws IOException
* if there are problems reading the file.
*/
public JPGFile(String filename) throws FileNotFoundException, IOException {
this.filename = filename;
readImage();
}
/**
* Get the height of the PPM image.
*
* @return the height of the image.
*/
public int getHeight() {
return height;
}
/**
* Get the width of the PPM image.
*
* @return the width of the image.
*/
public int getWidth() {
return width;
}
/**
* Get the data as byte array. Data is of any type that has been read from
* the file (usually 8bit RGB)
*
* @return The data of the image.
*/
public byte[] getBytes() {
return bytes;
}
/**
* Get the data as double array. Data is of any type that has been read from
* the file (usually 8bit RGB put into an 64bit double)
*
* @return The data of the image.
*/
public double[] getDouble() {
return doubles;
}
/**
* Get the data as double array. Data is of any type that has been read from
* the file (usually 8bit RGB put into an 64bit double)
*
* @return The data of the image.
*/
public int[] getInt() {
return ints;
}
/**
* Get the data as integer array. Data is of any type that has been read from
* the file (usually 8bit RGB put into an 64bit double)
*
* @return The data of the image.
*/
public int[][] getImageRGB() {
return imageRGB;
}
/**
* Write to <code>fn</code> file the <code>data</code> using the
* <code>width, height</code> variables. Data is assumed to be 8bit RGB.
*
* @throws FileNotFoundException
* if the directory/image specified is wrong
* @throws IOException
* if there are problems reading the file.
*/
public static void writeImage(String fn, int[] data, int width, int height)
throws FileNotFoundException, IOException {
FileOutputStream fOut = new FileOutputStream(fn);
JPEGImageEncoder jpeg_encode = JPEGCodec.createJPEGEncoder(fOut);
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
image.setRGB(0, 0, width, height, data, 0, width);
jpeg_encode.encode(image);
fOut.close();
}
/**
* Read the image from the specified file.
*
* @throws FileNotFoundException
* pretty obvious
* @throws IOException
* filesystem related problems
*/
private void readImage() throws FileNotFoundException, IOException {
FileInputStream fIn = new FileInputStream(filename);
JPEGImageDecoder jpeg_decode = JPEGCodec.createJPEGDecoder(fIn);
BufferedImage image = jpeg_decode.decodeAsBufferedImage();
width = image.getWidth();
height = image.getHeight();
int[] rgbdata = new int[width * height];
image.getRGB(0, 0, width, height, rgbdata, 0, width);
ints = rgbdata;
bytes = new byte[rgbdata.length];
doubles = new double[rgbdata.length];
imageRGB = new int[3][rgbdata.length];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) (rgbdata[i] & 0xFF);
doubles[i] = (double) (rgbdata[i]);
imageRGB[0][i] = (rgbdata[i] & 16711680) >> 16;
imageRGB[1][i] = (rgbdata[i] & 65280) >> 8;
imageRGB[2][i] = (rgbdata[i] & 255);
}
}
}
上述代码可以复制,粘贴使用,有方法的注视,getImageRGB() 就可以获得所有像素的RGB值,你就可以在其他方法里处理这个二维数组,得到你想要的平均值了,处理后的值,记得把值逆运算,再转换到Int[]里,就可以用,writeImage()输出图像了。