检测我的位图是黑白还是颜色图像

发布于 2025-02-09 09:51:52 字数 372 浏览 3 评论 0原文

我有一个图像URI,我使用以下代码从该URI获取bitmap

Bitmap bitmap = null;
    try {
       bitmap = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), uri);
    } catch (IOException e) {
        e.printStackTrace();
    }

现在我想检查bitmap是黑色和白色还是颜色图像。

(要么有黑色/白色图像或彩色图像)

我该怎么做? 我在Android上使用Java。

I have an image URI and I am getting the Bitmap from that URI using the code below:

Bitmap bitmap = null;
    try {
       bitmap = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), uri);
    } catch (IOException e) {
        e.printStackTrace();
    }

Now I want to check if the Bitmap is a black and white or a color image.

(There will either be a black/white image or a colored one)

How can I do this?
I am using Java on Android.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人心善变 2025-02-16 09:51:52

我在Kotlin中创建了一个扩展功能,您可以使用此功能或在Java中以相同逻辑制作类似的函数

fun Bitmap.isColored(): Boolean{
for (x in 0 until this.width){
    for (y in 0 until this.height){
        val color = this.getColor(x,y)
        val blue = color.blue()
        val red = color.red()
        val green = color.green()

        if(blue!=red || blue!=green || red!=green){
            return true
        }
    }
}

return false
}

来使用扩展功能,只需调用iScolored = yourbitmap.iscolored()

edit:java函数

@RequiresApi(Build.VERSION_CODES.Q)
boolean isColored(Bitmap bitmap){
    for (int x = 0; x< bitmap.getWidth(); x++){
        for (int y = 0 ;y < bitmap.getHeight(); y++){
            Color color = bitmap.getColor(x,y);
            float blue = color.blue();
            float red = color.red();
            float green = color.green();

            Log.d( "blue: " , Float.toString(blue));
            Log.d( "red: " , Float.toString(red));
            Log.d( "green: " , Float.toString(green));


            if(blue!=red || blue!=green || red!=green){
                return true;
            }
        }
    }

    return false;
}

编辑: Java的主要活动显示其使用

public class MainActivity2 extends AppCompatActivity {

@RequiresApi(api = Build.VERSION_CODES.Q)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    InputStream ins= getResources().openRawResource(
            getResources().getIdentifier(
                    "image",
                    "raw", getPackageName()
            )
    );

    Bitmap bitmap = BitmapFactory.decodeStream(ins);

    Log.d( "isColored: ", "" + isColored(bitmap));
}

@RequiresApi(Build.VERSION_CODES.Q)
boolean isColored(Bitmap bitmap){
    for (int x = 0; x< bitmap.getWidth(); x++){
        for (int y = 0 ;y < bitmap.getHeight(); y++){
            Color color = bitmap.getColor(x,y);
            float blue = color.blue();
            float red = color.red();
            float green = color.green();

            Log.d( "blue: " , Float.toString(blue));
            Log.d( "red: " , Float.toString(red));
            Log.d( "green: " , Float.toString(green));


            if(blue!=red || blue!=green || red!=green){
                return true;
            }
        }
    }

    return false;
}
}

I created an extension function in kotlin, you can use this or make a similar function in java with same logic

fun Bitmap.isColored(): Boolean{
for (x in 0 until this.width){
    for (y in 0 until this.height){
        val color = this.getColor(x,y)
        val blue = color.blue()
        val red = color.red()
        val green = color.green()

        if(blue!=red || blue!=green || red!=green){
            return true
        }
    }
}

return false
}

to use an extension function just call isColored = yourBitmap.isColored()

Edit: java function

@RequiresApi(Build.VERSION_CODES.Q)
boolean isColored(Bitmap bitmap){
    for (int x = 0; x< bitmap.getWidth(); x++){
        for (int y = 0 ;y < bitmap.getHeight(); y++){
            Color color = bitmap.getColor(x,y);
            float blue = color.blue();
            float red = color.red();
            float green = color.green();

            Log.d( "blue: " , Float.toString(blue));
            Log.d( "red: " , Float.toString(red));
            Log.d( "green: " , Float.toString(green));


            if(blue!=red || blue!=green || red!=green){
                return true;
            }
        }
    }

    return false;
}

Edit 2: Main activity in java showing its use

public class MainActivity2 extends AppCompatActivity {

@RequiresApi(api = Build.VERSION_CODES.Q)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    InputStream ins= getResources().openRawResource(
            getResources().getIdentifier(
                    "image",
                    "raw", getPackageName()
            )
    );

    Bitmap bitmap = BitmapFactory.decodeStream(ins);

    Log.d( "isColored: ", "" + isColored(bitmap));
}

@RequiresApi(Build.VERSION_CODES.Q)
boolean isColored(Bitmap bitmap){
    for (int x = 0; x< bitmap.getWidth(); x++){
        for (int y = 0 ;y < bitmap.getHeight(); y++){
            Color color = bitmap.getColor(x,y);
            float blue = color.blue();
            float red = color.red();
            float green = color.green();

            Log.d( "blue: " , Float.toString(blue));
            Log.d( "red: " , Float.toString(red));
            Log.d( "green: " , Float.toString(green));


            if(blue!=red || blue!=green || red!=green){
                return true;
            }
        }
    }

    return false;
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文