问题描述
我正在使用 android-jhlabs 库从 Android 应用移植一些代码 到 Windows Phone 8.1 运行时应用程序.
I'm porting over some code from an Android app using the android-jhlabs library to a Windows Phone 8.1 runtime app.
代码非常简单,基本上如下所示(在 Java 中):
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);
在 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#,但有一个小问题:
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;
}
查看文档,我看到很多不同的位图类:WriteableBitmap、BitmapImage、BitmapEncoder 和 BitmapDecoder.我没有找到适用于 Windows Phone 8.1 的 getPixel 类,但有一个适用于 Windows Store 应用程序.即使在那里,也只是为了得到一个像素.
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.
createBitmap() 命令也是如此.我找不到任何等价物.
Same goes the createBitmap() command. I couldn't find any equivalent.
我的问题是,Windows Phone 8.1 的 getPixels() 和 createBitmap() 命令是否有一行等效的命令?最好使用 BitmapDecoder?
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?
推荐答案
您必须使用 BitmapDecoder:
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)
}
}
这篇关于Windows Phone 8.1 一行等效于 getPixels &来自Android的createBitmap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!