C#-用白色字节填充图像字节以填充512 x 512

时间:2019-05-07 15:27:59

标签: c# .net image byte digital-persona-sdk

我正在使用Digital Persona SDK扫描wsq格式的指纹,要重新申请,我需要512 x 512图像,该SDK仅导出357 x 392图像。

sdk提供了一种以wsq格式压缩从设备捕获的图像并返回我可以写入磁盘的字节数组的方法。

-我尝试为512 x 512图像分配262144的缓冲区。

-用每个像素的白色像素数据填充新缓冲区,使其值为255。

-将原始图像缓冲区复制到新图像缓冲区中。原始图像无需居中,但重要的是要确保复制时不会破坏图像数据。

总而言之,我尝试将旧图像复制到新图像的右上角。

DPUruNet.Compression.Start();
DPUruNet.Compression.SetWsqBitrate(95, 0);

Fid capturedImage = captureResult.Data;

//Fill the new buffer with white pixel data each byte to value 255.
byte[] bytesWSQ512 = new byte[262144];
for (int i = 0; i < bytesWSQ512.Length; i++)
{
    bytesWSQ512[i] = 255;
}

//Compress capturedImage and get bytes (357 x 392)
byte[] bytesWSQ = DPUruNet.Compression.CompressRaw(capturedImage.Views[0].Width, capturedImage.Views[0].Height, 500, 8, capturedImage.Views[0].RawImage, CompressionAlgorithm.COMPRESSION_WSQ_NIST);

//Copy the original image buffer into the new image buffer
for (int i = 0; i < capturedImage.Views[0].Height; i++)
{
    for (int j = 0; j < capturedImage.Views[0].Width; j++)
    {
        bytesWSQ512[i * bytesWSQ512.Length + j ] = bytesWSQ[i * capturedImage.Views[0].Width + j];
    }
}
//Write bytes to disk
File.WriteAllBytes(@"C:\Users\Admin\Desktop\bytesWSQ512.wsq", bytesWSQ512);
DPUruNet.Compression.Finish();

运行该代码段时,出现IndexOutOfRangeException,我不知道循环或新数组的索引计算是否正确。

这是我要做的事情的代表。

image

1 个答案:

答案 0 :(得分:0)

如果有人试图实现类似目标或填充原始图像,我希望这会有所帮助。

DPUruNet.Compression.
DPUruNet.Compression.SetWsqBitrate(75, 0);
Fid ISOFid = captureResult.Data;

byte[] paddedImage = PadImage8BPP(captureResult.Data.Views[0].RawImage, captureResult.Data.Views[0].Width, captureResult.Data.Views[0].Height, 512, 512, 255);
byte[] bytesWSQ512 = Compression.CompressRaw(512, 512, 500, 8, paddedImage, CompressionAlgorithm.COMPRESSION_WSQ_NIST);

调整图像大小(填充)的方法是:

public byte[] PadImage8BPP(byte[] original, int original_width, int original_height, int desired_width, int desired_height, byte pad_color)
    {
        byte[] canvas_8bpp = new byte[desired_width * desired_height];

        for (int i = 0; i < canvas_8bpp.Length; i++)
            canvas_8bpp[i] = pad_color; //Fill background.  Note this type of fill will fail histogram checks.

        int clamp_y_begin = 0;
        int clamp_y_end = original_height;
        int clamp_x_begin = 0;
        int clamp_x_end = original_width;

        int pad_y = 0;
        int pad_x = 0;

        if (original_height > desired_height)
        {
            int crop_distance = (int)Math.Ceiling((original_height - desired_height) / 2.0);
            clamp_y_begin = crop_distance;
            clamp_y_end = original_height - crop_distance;
        }
        else
        {
            pad_y = (desired_height - original_height) / 2;
        }

        if (original_width > desired_width)
        {
            int crop_distance = (int)Math.Ceiling((original_width - desired_width) / 2.0);
            clamp_x_begin = crop_distance;
            clamp_x_end = original_width - crop_distance;
        }
        else
        {
            pad_x = (desired_width - original_width) / 2;
        }

        //We traverse the captured image (either whole image or subset)
        for (int y = clamp_y_begin; y < clamp_y_end; y++)
        {
            for (int x = clamp_x_begin; x < clamp_x_end; x++)
            {
                byte image_pixel = original[y * original_width + x];
                canvas_8bpp[(pad_y + y - clamp_y_begin) * desired_width + pad_x + x - clamp_x_begin] = image_pixel;
            }
        }

        return canvas_8bpp;
    }