我有灰色像素但不知道创建灰度图像
问问题
2194 次
2 回答
4
在 Java 中动态创建灰度图像(来自http://technojeeves.com/joomla/index.php/free/89-create-grayscale-image-on-the-fly-in-java)
import java.awt.*;
import java.awt.color.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
public class BMP {
/**
* (Testing only)
*/
public static void main(String[] args) throws Exception {
BMP.makeMeCross(args[0]);
}
/**
*
* @param filename The file name for the 'crosshair' image
*
* @throws IOException If the file could not be written to by ImageIO
*/
public static void makeMeCross(String filename) throws IOException {
int sz = 101;
byte[] buffer = new byte[sz * sz];
for (int i = 0; i < sz; i++) {
for (int j = 0; j < sz; j++) {
// Make a 'crosshair' pattern
buffer[(i * sz) + j] = ((j == 50) || (i == 50)) ? (byte) 255 : 0;
}
}
ImageIO.write(BMP.getGrayscale(101, buffer), "PNG", new File(filename));
}
/**
*
* @param width The image width (height derived from buffer length)
* @param buffer The buffer containing raw grayscale pixel data
*
* @return THe grayscale image
*/
public static BufferedImage getGrayscale(int width, byte[] buffer) {
int height = buffer.length / width;
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
int[] nBits = { 8 };
ColorModel cm = new ComponentColorModel(cs, nBits, false, true,
Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
SampleModel sm = cm.createCompatibleSampleModel(width, height);
DataBufferByte db = new DataBufferByte(buffer, width * height);
WritableRaster raster = Raster.createWritableRaster(sm, db, null);
BufferedImage result = new BufferedImage(cm, raster, false, null);
return result;
}
}
于 2011-06-09T13:01:06.317 回答
2
为此目的,有一个 javax.swing.GrayFilter 值得一看。它可以这样使用:
Icon icon = myLabel.getIcon();
Image img = gc.createCompatibleImage(icon.getIconWidth(), icon.getIconHeight(), Transparency.TRANSLUCENT);
icon.paintIcon(this, img.getGraphics(), 0, 0);
img = GrayFilter.createDisabledImage(img);
myLabel.setIcon(new ImageIcon(img));
于 2011-08-28T23:41:10.070 回答