Windows Phone 8.1一行等效于getPixels&来自Android的createBitmap?

时间:2014-09-03 12:21:13

标签: c# windows-phone-8.1

我使用android-jhlabs library将一些代码从Android应用移植到Windows Phone 8.1运行时应用。

代码非常简单,基本上如下所示(在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);

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

我已将此功能转换为C#,但有一个小问题:

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

通过文档,我看到很多不同的位图类:WriteableBitmap,BitmapImage,BitmapEncoder和BitmapDecoder。我没有找到适用于Windows Phone 8.1的getPixel类,但有一个适用于Windows应用商店应用。即便如此,它只能获得一个像素。

createBitmap()命令也是如此。我找不到任何等价物。

我的问题是,是否有一行等效于Windows Phone 8.1的getPixels()和createBitmap()命令?是否最好使用BitmapDecoder

1 个答案:

答案 0 :(得分:1)

您必须使用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)
    }
}