简体   繁体   English

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

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

I'm using Digital Persona SDK to scan fingerprints in wsq format, for requeriment I need 512 x 512 image, the SDK only export 357 x 392 image. 我正在使用Digital Persona SDK扫描wsq格式的指纹,对于requeriment我需要512 x 512图像,SDK只导出357 x 392图像。

The sdk provide a method to compress captured image from device in wsq format and return a byte array that I can write to disk. sdk提供了一种方法,以wsq格式压缩来自设备的捕获图像,并返回一个可以写入磁盘的字节数组。

-I've tried to allocate a buffer of 262144 for 512 x 512 image. - 我试图为512 x 512图像分配262144的缓冲区。

-Fill the new buffer with white pixel data each byte to value 255. - 使用白色像素数据填充新缓冲区,每个字节值为255。

-Copy the original image buffer into the new image buffer. - 将原始图像缓冲区复制到新的图像缓冲区中。 The original image doesn't need to be centered but it's important to make sure to copy without corrupting the image data. 原始图像不需要居中,但确保在不破坏图像数据的情况下进行复制非常重要。

To summarize I've tried to copy the old image into the upper right corner of the new image. 总结一下,我试图将旧图像复制到新图像的右上角。

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();

When running that snippet I get IndexOutOfRangeException, I don't know if the loop or the calculation of indexes for new array are right. 运行该代码片段时,我得到IndexOutOfRangeException,我不知道新数组的循环或索引计算是否正确。

Here is a representation of what I'm trying to do. 这是我正在尝试做的事情的代表。

图片

If someone is trying to achieve something like this or padding a raw image, I hope this will help. 如果有人试图达到这样的效果或填充原始图像,我希望这会有所帮助。

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

And the method to resize (pad) the image is: 并且调整(填充)图像的方法是:

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

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

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