简体   繁体   中英

UWP convert RAW 8 Bit to PNG

I am developing an app for UWP, we have a scanner connected that provide images in RAW8Bit, we are looking to convert RAW8Bit to PNG files. We manage to do that by converting to Bitmap first, but we need another way to convert directly from RAW to PNG

You should sure the image width and height and the byte list format is Bgra8.

The Bgra8 means that a pixel is 8 bit and the first byte is blue color...

You can use BitmapEncoder to encode the byte list to png file.

            var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, file);
            encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint) width, (uint) height, 96,
                96, byteList);
            await encoder.FlushAsync();

Write the code to save to file.

    private async Task SaveToFileAsync(byte[] byteList, int width, int height, IStorageFile file)
    {
        using (var stream = (await file.OpenStreamForWriteAsync()).AsRandomAccessStream())
        {
            await ByteToPng(byteList, width, height, stream);
        }
    }

    private async Task ByteToPng(byte[] byteList, int width, int height, IRandomAccessStream file)
    {
        try
        {
            var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, file);
            encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint) width, (uint) height, 96,
                96, byteList);
            await encoder.FlushAsync();
        }
        catch (Exception e)
        {
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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