从 java 中的 [0,255] 个灰度值数组创建灰度位图
Creating grayscale bitmap from array of [0,255] gray values in java
我有一个数组 int[] shadesOfGray
大小 n*n
包含来自 [0,255] 的值。是否可以从该数组创建 8 位灰度位图?
例如,如果
int[] shadesOfGray = {255, 255, 255, 128, 128, 128, 0, 0, 0}
则位图中对应像素的强度为:
255 255 255
128 128 128
0 0 0
我试过这样的事情:
private static void generateBMPwithDistribution(int[] shadesOfGray, int sum, String path) throws IOException {
int dim = 100;
byte[] buffer = new byte[dim * dim];
for (int x = 0, i = 0; x < dim; x++)
for (int y = 0; y < dim; y++)
buffer[(x * dim) + y] = (byte) (shadesOfGray[i]); // problem
try {
ImageIO.write(getGrayscale(dim, buffer), "bmp", new File(path));
} catch (IOException e) {
...
}
}
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, buffer.length);
WritableRaster raster = Raster.createWritableRaster(sm, db, null);
BufferedImage result = new BufferedImage(cm, raster, false, null);
return result;
}
因为我想要 8 位 .bmp,所以我正在复制缓冲区中的值,然后将缓冲区写入文件。问题是值 >= 128;字节被认为具有负值。 Java 有办法克服这个问题吗?
您的程序可能如下所示:
private static void generateBMPwithDistribution(int[] shadesOfGray, int sum, String path) throws IOException {
int dim1 = (int)Math.sqrt(shadesOfGray.length), dim=100*dim1;
int[] buffer = new int[dim * dim];
int howManyTimes=dim/dim1;
for (int x = 0; x < dim; x++)
for (int y = 0; y < dim; y++) {
int valueToReplicate=shadesOfGray[(x + y*dim)/howManyTimes];
buffer[x + y*dim] = 0xff000000|(valueToReplicate<<16)|(valueToReplicate<<8)|valueToReplicate;
}
BufferedImage result=new BufferedImage(dim, dim, BufferedImage.TYPE_INT_RGB);
try {
ImageIO.write(result, "bmp", new File(path));
} catch (IOException e) {
...
}
}
我有一个数组 int[] shadesOfGray
大小 n*n
包含来自 [0,255] 的值。是否可以从该数组创建 8 位灰度位图?
例如,如果
int[] shadesOfGray = {255, 255, 255, 128, 128, 128, 0, 0, 0}
则位图中对应像素的强度为:
255 255 255
128 128 128
0 0 0
我试过这样的事情:
private static void generateBMPwithDistribution(int[] shadesOfGray, int sum, String path) throws IOException {
int dim = 100;
byte[] buffer = new byte[dim * dim];
for (int x = 0, i = 0; x < dim; x++)
for (int y = 0; y < dim; y++)
buffer[(x * dim) + y] = (byte) (shadesOfGray[i]); // problem
try {
ImageIO.write(getGrayscale(dim, buffer), "bmp", new File(path));
} catch (IOException e) {
...
}
}
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, buffer.length);
WritableRaster raster = Raster.createWritableRaster(sm, db, null);
BufferedImage result = new BufferedImage(cm, raster, false, null);
return result;
}
因为我想要 8 位 .bmp,所以我正在复制缓冲区中的值,然后将缓冲区写入文件。问题是值 >= 128;字节被认为具有负值。 Java 有办法克服这个问题吗?
您的程序可能如下所示:
private static void generateBMPwithDistribution(int[] shadesOfGray, int sum, String path) throws IOException {
int dim1 = (int)Math.sqrt(shadesOfGray.length), dim=100*dim1;
int[] buffer = new int[dim * dim];
int howManyTimes=dim/dim1;
for (int x = 0; x < dim; x++)
for (int y = 0; y < dim; y++) {
int valueToReplicate=shadesOfGray[(x + y*dim)/howManyTimes];
buffer[x + y*dim] = 0xff000000|(valueToReplicate<<16)|(valueToReplicate<<8)|valueToReplicate;
}
BufferedImage result=new BufferedImage(dim, dim, BufferedImage.TYPE_INT_RGB);
try {
ImageIO.write(result, "bmp", new File(path));
} catch (IOException e) {
...
}
}