【发布时间】:2015-07-12 05:36:53
【问题描述】:
我正在使用 getPixel() 方法返回位图中每个像素的 rgb 值。然后我使用 if() 语句将 rgb 值与我自己预定义的 rgb 值进行比较,然后在语句为真时执行代码。如下图:
for(int x=startX; x<w; x++){
for(int y=startY; y<h; y++){
int pixel = img.getPixel(x, y);
if(pixel == Color.rgb(255, 255, 255)); //some code
if(pixel == Color.rgb(255, 0, 0)); //some code
if(pixel == Color.rgb(255, 255, 0)); //some code
if(pixel == Color.rgb(120, 60, 0)); //some code
}
我的问题是,虽然前三个 if() 语句被执行,但第四个语句永远不会执行。我知道我的图像包含具有所有这些 rgb 值的像素,因为我自己制作了图像。我猜这个问题是因为颜色类中没有预定义的变量,rgb 值为(120,60,0)。所以基本上我想知道这个问题是否存在。也许比 getPixel() 更好的方法?我只需要第四个 if() 语句中的代码就可以执行。
编辑-更多信息:
我的图片有一个 png 扩展名。我通过以下方式将其加载到位图:
public static Bitmap loadBitmap(String filename, boolean transparency) {
InputStream inputStream = null;
try {
inputStream = MainActivity.assets.open(filename);
} catch (IOException e) {
e.printStackTrace();
}
BitmapFactory.Options options = new BitmapFactory.Options();
if (transparency) {
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
} else {
options.inPreferredConfig = Bitmap.Config.RGB_565;
}
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null,
options);
return bitmap;
}
图像是通过 assets 变量检索的,它是我的 MainActivity 类型 AssetManager 类中的静态变量。我通过声明 assets = getassets() 来初始化这个变量。我的图像位于 AssetManager 然后检索的资产文件夹中。
【问题讨论】:
-
您是如何创建和加载位图的?它有什么颜色密度?它存储的是什么类型的文件?
-
当 RGB_565 可能永远不会有 120、60 个值
-
什么是更好的选择?
-
要么使位图始终为 ARGB_8888,要么检查 r g b 分量不完全匹配,但 Math.abs(Color.red(pixel) - 120)
-
如果必须的话,我会使用 ARGB_8888,但出于记忆的原因,我想远离它。没有其他配置我可以使用吗?如果我不设置默认配置是什么?