Comparing Bitmap images in Android
Depending on how you define the same. If you mean the exact same file, you can do an md5sum of the files. That will be the same for every type of file I guess.
Because you specifically make the distinction for bitmap files, you might be interested in files that differ in size. That's a bit harder. If they are the same size, but not completely the same (but look really much like eachother) you can compare each separate pixel, and if enough pixels (threshold 1) are close enough to each other in color (threshold 2) you can declare them as being the same.
You can getPixel(int,int)
to get the color, see this page
android compare 2 images and highlight difference
With the help of digital color meter I detected there is very slight difference between colors of visually looking similar images quite similar what @taarraas also suggested. so I took a threshold value and solved it like this.
private static final int threashold = 10;
void findDifference(Bitmap firstImage, Bitmap secondImage)
{
if (firstImage.getHeight() != secondImage.getHeight() || firstImage.getWidth() != secondImage.getWidth())
Toast.makeText(this, "Images size are not same", Toast.LENGTH_LONG).show();
boolean isSame = true;
for (int i = 0; i < firstImage.getWidth(); i++)
{
for (int j = 0; j < firstImage.getHeight(); j++)
{
int pixel = firstImage.getPixel(i,j);
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
int pixel2 = secondImage.getPixel(i,j);
int redValue2 = Color.red(pixel2);
int blueValue2 = Color.blue(pixel2);
int greenValue2 = Color.green(pixel2);
if (Math.abs(redValue2 - redValue) + Math.abs(blueValue2 - blueValue) + Math.abs(greenValue2 - greenValue) <= threashold)
// if (firstImage.getPixel(i,j) == secondImage.getPixel(i,j))
{
}
else
{
differentPixels.add(new Pixel(i,j));
secondImage.setPixel(i,j, Color.YELLOW); //for now just changing difference to yello color
isSame = false;
}
}
}
imgOutput.setImageBitmap(secondImage);
}
Image Comparison in Android.
Updated Answer
In order to answer the question in your comment, there are various approaches - it depends what you are actually trying to achieve... do you need to detect images that have been rotated relative to each other for example, or blurred, or smoothed, or tampered with. Some methods are...
Perceptual Hashing - you create a hash for all your images and calculate the distance between images. See here and also the comment about pHash
.
Mean Colour - you calculate the mean (or average) colour of your images and compare the means - this method is quite simple.
RMSE or similar - you calculate the Root Mean Squre Error for all pixels and look for a low value to indicate images are similar. This method and all the ones above are easily done with ImageMagick. See, and vote for, Kurt's (@KurtPfeifle) excellent, thorough answer here.
Features - you find shapes and features in your image and compare those - try Googling "SIFT"
.
Original Answer
It's not a problem of your code, it is a fundamental issue of information loss. If you resize an image down to a smaller size, in general you will lose information since the smaller image cannot contain the same amount of information as a larger one. There are many things that could be going on...
Colour Loss
Imagine you have a lovely big 1000x1000 image with a smooth gradient and correspondingly millions of colours like this:
If you now resize it down to an image of 32x32, it can now only contain 1,024 colours as a maximum, so when you resize it up again you might get something like this:
And now you can see that banding has happened - where the colours have clumped together into the smaller number of colours that the smaller image can hold.
Format
When you resize an image, the program that does it may change from a true-colour 24 bits per pixel image to a palettised image with just 256, or fewer, colours. The image may look the same, but it won't necessarily compare identically.
Also related to format, you may resize an image and usea different image format that cannot contain all the features of the original and they will be lost when you resize up again afterwards. For example, your original image may be a PNG
with transparency, and you may have resized it down to a JPEG
which cannot contain transparency, and then resize back up to a PNG
and it will be lost.
Resolution/Detail
If you have some sharp text, or details in your image, they may be lost. Say you start with this:
and resize down and then back up again, you may get this because the smaller image cannot hold the details. Again, this will mean that your comparison will fail to spot that they are the same image.
Likewise with simple lines..
and downsized and re-upsized
Android Studio - How to compare the image within a Drawable to other Drawables in R?
From a previous question that very similar here.
Try this
if(drawable.getConstantState().equals(getResources().getDrawable(R.drawable.monkey).getConstantState())){
//Do your work here
}
Android compare imageView with image
Thanks Morrison, that was it.
First
final ImageView test = (ImageView) findViewById(R.id.imageview1);
final Bitmap bmap = ((BitmapDrawable)test.getDrawable()).getBitmap();
Drawable myDrawable = getResources().getDrawable(R.drawable.red);
final Bitmap myLogo = ((BitmapDrawable) myDrawable).getBitmap();
Next
if(bmap.sameAs(myLogo))
{
do sthng
}
Related Topics
How to Move the Layout Up When the Soft Keyboard Is Shown Android
The Import Android.Support.V7 Cannot Be Resolved
Android Autocompletetextview with Custom Adapter Filtering Not Working
How to Take Camera Capture Without a Preview from a Service or Thread
Android: How to Change Checkbox Size
Android - Switch Actionbar Back Button to Navigation Button
Android - Get Time of Chronometer Widget
Tabhost with Fragments and Fragmentactivity
Updating the List View When the Adapter Data Changes
Android Material: Status Bar Color Won't Change
How to Limit the Height of Spinner Drop Down View in Android
Programmatically Set Android Phone's Background
How to Draw a Route, Along an Existing Road, Between Two Points
Getting the Battery Current Values for the Android Phone
How to Move Bluetooth Activity into a Service
Android Speech Recognition Continuous Service
Why Do I Get Com.Google.Android.Gms.Common.Api.Apiexception: 10: