24

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?

lorenzo-s
  • 16,603
  • 15
  • 54
  • 86

2 Answers2

45

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
}
lorenzo-s
  • 16,603
  • 15
  • 54
  • 86
  • @Jeyaseelan Erhm, worked for at least 29 people. Maybe you can give me some details, so I can help you? What you tried, what's your code, where's your bitmap from? – lorenzo-s Jul 16 '18 at 10:25
  • Yes @lorenzo-s i am sry for negative comment i am doing a digital signature where if the user signs i have to take that and convert it as bitmap if user dont sign and leave the surface empty at that time the empty canvas is coming in the bitmap i mean white image will come if i check your code what you doing is you are creating a bitmap with one bitmap and comparing with same bitmap what happens here is i get white image in empty bitmat and my bitmap also so it fails here for me – Jeyaseelan Jul 18 '18 at 05:05
  • i get this error java.lang.IllegalArgumentException: can't create mutable bitmap with Config.HARDWARE – Faisal Mohammad Dec 04 '19 at 08:04
0

You can do this very easy but it depends on the application. If you have an application that prompts the user for a drawing input, like signature or anything similar, you will usually have an ArrayList of Paths which are drawn to the Canvas of that View. You can do a check when you want to return the BitMap look to see if the ArrayList of Paths is bigger than 0 and return the BitMap if so, or else return null.