Windows Phone 8.1 one line equivalent for getPixels & createBitmap from Android?

a 夏天 提交于 2019-12-13 17:54:44

问题


I'm porting over some code from an Android app using the android-jhlabs library to a Windows Phone 8.1 runtime app.

The code is quite simple and basically looks like the following (in Java):

PointillizeFilter filter = new PointillizeFilter();
filter.setEdgeColor(Color.BLACK);
filter.setScale(10f);
filter.setRandomness(0.1f);
filter.setAmount(0.1f);
filter.setFuzziness(0.1f);
filter.setTurbulence(10f);
filter.setGridType(PointillizeFilter.SQUARE);
int[] src = AndroidUtils.bitmapToIntArray(artWork);
src = filter.filter(src, width, height);
Bitmap destImage = Bitmap.createBitmap(src, width, height, Config.ARGB_8888);

In Java:

public static int[] bitmapToIntArray(Bitmap bitmap){
    final int bitmapWidth = bitmap.getWidth();
    final int bitmapHeight = bitmap.getHeight();

    int[] colors = new int[bitmapWidth *  bitmapHeight];
    bitmap.getPixels(colors, 0, bitmapWidth, 0, 0, bitmapWidth, bitmapHeight);

    return colors;
}

I've converted this function to C#, with a slight problem:

 public static int[] bitmapToIntArray(BitmapImage bitmapImage)
    {
        int bitmapWidth = bitmapImage.PixelWidth;
        int bitmapHeight = bitmapImage.PixelHeight;

        int[] colors = new int[bitmapWidth * bitmapHeight];

         // how to convert line below?
        //bitmap.getPixels(colors, 0, bitmapWidth, 0, 0, bitmapWidth, bitmapHeight);

        return colors;
    }

Looking through the documentation, I see a lot of different classes for bitmaps: WriteableBitmap, BitmapImage, BitmapEncoder and BitmapDecoder. I did not find a getPixel class for Windows Phone 8.1, but there is one for Windows Store apps. Even there, it's only for getting one pixel.

Same goes the createBitmap() command. I couldn't find any equivalent.

My question is, is there a one line equivalent for the getPixels() and createBitmap() command for Windows Phone 8.1? Would it be best to use the BitmapDecoder?


回答1:


You must use BitmapDecoder:

StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

var pixelData = await decoder.GetPixelDataAsync();
var pixels = pixelData.DetachPixelData();

var width = decoder.OrientedPixelWidth;
var height = decoder.OrientedPixelHeight;

for (var i = 0; i < height; i++)
{
    for (var j = 0; j < width; j++)
    {
        byte r = pixels[(i * height + j) * 4 + 0]; //red
        byte g = pixels[(i * height + j) * 4 + 1]; //green
        byte b = pixels[(i * height + j) * 4 + 2]; //blue (rgba)
    }
}


来源:https://stackoverflow.com/questions/25644187/windows-phone-8-1-one-line-equivalent-for-getpixels-createbitmap-from-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!