简体   繁体   English

在Java中从[0,255]个灰度值的数组创建灰度位图

[英]Creating grayscale bitmap from array of [0,255] gray values in java

I have an array int[] shadesOfGray size n*n containing values from [0,255]. 我有一个数组int[] shadesOfGray大小n*n包含[0,255]中的值。 Is it possible to create 8-bit grayscale bitmap from this array? 是否可以从该数组创建8位灰度位图?

Eg, if int[] shadesOfGray = {255, 255, 255, 128, 128, 128, 0, 0, 0} 例如,如果int[] shadesOfGray = {255, 255, 255, 128, 128, 128, 0, 0, 0}

then the intensity of the corresponding pixels in bitmap would be: 那么位图中相应像素的强度为:

255 255 255
128 128 128
0    0   0 

I have tried something like this: 我已经尝试过这样的事情:

   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;
    }

Since I want 8-bit .bmp, I am copying values in buffer and then writing buffer into file. 由于我要使用8位.bmp,因此我要复制缓冲区中的值,然后将缓冲区写入文件中。 Problem is for values >= 128; 问题是值> = 128; byte is considered to have negative value. 字节被认为具有负值。 Is there a way in Java to get over this? Java中有没有办法解决这个问题?

Your procedure can look something like this: 您的过程如下所示:

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) {
        ...
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM