How to check if a Bitmap is empty (blank) on Android

对着背影说爱祢 提交于 2019-11-26 21:10:15

问题


How can I check if a Bitmap object is completely blank, i.e. all its pixels are transparent, without a x-y loop on every pixel?


回答1:


You can check your Bitmap instance (in the example myBitmap) against an empty one with:

Bitmap emptyBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), myBitmap.getConfig());
if (myBitmap.sameAs(emptyBitmap)) {
    // myBitmap is empty/blank
}



回答2:


This is not an efficient way to solve the problem but it does its purpose cause I didn't find a solution even in docs

see while the Bitmap is null it's width or height can't be fetched and it raises a crash so here is may solve your problem

       try{
                   int check=bitmap.getWidth();

              // the bitmap is valid & not null or empty 

               }catch (Exception w){
                  // the bitmap, not valid eighter null or empty

               }

hope this helps :)



来源:https://stackoverflow.com/questions/28767466/how-to-check-if-a-bitmap-is-empty-blank-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!