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

C# - Padding image bytes with white bytes to fill 512 x 512

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

sdk 提供了一种以 w​​sq 格式压缩从设备捕获的图像的方法和 return 我可以写入磁盘的字节数组。

-我尝试为 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 时,我不知道循环或新数组的索引计算是否正确。

这是我正在尝试做的事情的表示。

如果有人正在尝试实现类似的效果或填充原始图像,我希望这会有所帮助。

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