android - 如何在android项目的res文件夹中获取图像的像素

标签 android getpixel

安卓新手。我认为这里的任何问题都与我的不同。

我已将图像加载到我的 res 文件夹中。我把它们放在一个可绘制的文件夹中。如何获取 res.drawable 文件夹中名为 bb.png 的图像的像素?

我需要一个简单的解释,说明如何将图像文件放入变量中,以及我需要使用什么“getPixel(...)”命令。我不需要显示图像,只需从中获取像素数组,并检查像素是黑色还是白色。感谢任何帮助,谢谢!

迈克

最佳答案

其实很简单!

位图 bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);

一旦您有了一个 Bitmap 对象,就会有几个选项。

bm.getPixel(x,y) 将返回一个 int 对应于 Color 中的 int > 类,例如 Color.BLACKColor.WHITE

此外,bm.copyPixelsToBuffer(Buffer destination) 会将所有像素复制到一个 Buffer 对象中,您可以逐个像素地搜索该对象。

查看文档以获取更多详细信息。

Bitmap Documentation

Color Documentation

这是一个示例代码 fragment ,假设您的/res/drawable 文件夹中有一张名为“image”的图像。

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
int pixelColor = bm.getPixel(10,10); //Get the pixel at coordinates 10,10

if(pixelColor == Color.BLACK) {
  //The pixel is black
}

else if(pixelColor == Color.WHITE) {
  //The pixel was white
}

显然,您应该小心获取像素。确保像素存在,并且坐标不大于图像。要获取 Bitmap 的尺寸,只需分别使用 bm.getHeight()bm.getWidth()

关于android - 如何在android项目的res文件夹中获取图像的像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13314198/

相关文章:

android - 丢失 keystore 密码,但仍有 private_key.pepk : Google signin app

c# - 在 Visual Studio 2015 中调试 native Android 库时断点不起作用

android - Webview 仅在第一次加载

c++ - 使用 BitBlt 获取另一个窗口的单个像素

c# - 使用 GetPixel() 查找彩色区域的最大宽度和最大高度相交的像素

c++ - 从 IDirect3DSurface9 获取像素?

android - 多行 AutoCompleteTextView 不接受滚动触摸

android - Firebase Analytics 自定义事件参数

C# - 用于 Windows 窗体应用程序位图的 SetPixel 和 GetPixel 的更快替代方案

android - 如何在TextView上显示bmp颜色值