有什么办法可以检查位图是否相同?有人可以帮我吗?比较Android中的位图图像
回答
取决于你如何定义相同。如果你的意思是完全相同的文件,你可以做一个md5sum的文件。对于我猜想的每种类型的文件,这都是一样的。
由于您专门对位图文件进行了区分,因此您可能会对尺寸不同的文件感兴趣。这有点困难。如果它们的大小相同但不完全相同(但看起来非常相似),则可以比较每个单独的像素,并且如果足够多的像素(阈值1)在颜色(阈值2)内足够接近,则可以声明他们是一样的。
您可以getPixel(int,int)
得到的颜色,看到this page
Bitmap类有方法“sameAs的”您可以使用该方法来比较两个位图
应该是这样的:
public boolean equals(Bitmap bitmap1, Bitmap bitmap2) {
ByteBuffer buffer1 = ByteBuffer.allocate(bitmap1.getHeight() * bitmap1.getRowBytes());
bitmap1.copyPixelsToBuffer(buffer1);
ByteBuffer buffer2 = ByteBuffer.allocate(bitmap2.getHeight() * bitmap2.getRowBytes());
bitmap2.copyPixelsToBuffer(buffer2);
return Arrays.equals(buffer1.array(), buffer2.array());
}
内存不足异常 – Trikaldarshi 2013-06-28 12:09:48
@Photon,我没有遇到过这种方法的任何异常 – Exterminator13 2013-07-04 09:22:59
内存不足异常即使在galaxy S3上 – Trikaldarshi 2013-07-04 11:06:24
这个问题似乎很老,但我今天在这个问题上花了一些时间,这里是我没有。
private static boolean compare(Bitmap b1, Bitmap b2) {
if (b1.getWidth() == b2.getWidth() && b1.getHeight() == b2.getHeight()) {
int[] pixels1 = new int[b1.getWidth() * b1.getHeight()];
int[] pixels2 = new int[b2.getWidth() * b2.getHeight()];
b1.getPixels(pixels1, 0, b1.getWidth(), 0, 0, b1.getWidth(), b1.getHeight());
b2.getPixels(pixels2, 0, b2.getWidth(), 0, 0, b2.getWidth(), b2.getHeight());
if (Arrays.equals(pixels1, pixels2)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
您可以简化语句以返回Arrays.equals(pixels1,pixels2);代替“if(Arrays.equals(pixels1,pixels2)){ return true; } else { return false; }” – ademar111190 2014-07-29 19:42:31
对于小于12的API,主要问题是对于大文件分辨率,我们得到OutOfMemory
错误。我解决它通过(在本例中10)随地吐痰位图成块,然后通过字节比较它们:
private boolean compareBitmaps(Bitmap bitmap1, Bitmap bitmap2)
{
if (Build.VERSION.SDK_INT > 11)
{
return bitmap1.sameAs(bitmap2);
}
int chunkNumbers = 10;
int rows, cols;
int chunkHeight, chunkWidth;
rows = cols = (int) Math.sqrt(chunkNumbers);
chunkHeight = bitmap1.getHeight()/rows;
chunkWidth = bitmap1.getWidth()/cols;
int yCoord = 0;
for (int x = 0; x < rows; x++)
{
int xCoord = 0;
for (int y = 0; y < cols; y++)
{
try
{
Bitmap bitmapChunk1 = Bitmap.createBitmap(bitmap1, xCoord, yCoord, chunkWidth, chunkHeight);
Bitmap bitmapChunk2 = Bitmap.createBitmap(bitmap2, xCoord, yCoord, chunkWidth, chunkHeight);
if (!sameAs(bitmapChunk1, bitmapChunk2))
{
recycleBitmaps(bitmapChunk1, bitmapChunk2);
return false;
}
recycleBitmaps(bitmapChunk1, bitmapChunk2);
xCoord += chunkWidth;
}
catch (Exception e)
{
return false;
}
}
yCoord += chunkHeight;
}
return true;
}
private boolean sameAs(Bitmap bitmap1, Bitmap bitmap2)
{
// Different types of image
if (bitmap1.getConfig() != bitmap2.getConfig())
return false;
// Different sizes
if (bitmap1.getWidth() != bitmap2.getWidth())
return false;
if (bitmap1.getHeight() != bitmap2.getHeight())
return false;
int w = bitmap1.getWidth();
int h = bitmap1.getHeight();
int[] argbA = new int[w * h];
int[] argbB = new int[w * h];
bitmap1.getPixels(argbA, 0, w, 0, 0, w, h);
bitmap2.getPixels(argbB, 0, w, 0, 0, w, h);
// Alpha channel special check
if (bitmap1.getConfig() == Bitmap.Config.ALPHA_8)
{
final int length = w * h;
for (int i = 0; i < length; i++)
{
if ((argbA[i] & 0xFF000000) != (argbB[i] & 0xFF000000))
{
return false;
}
}
return true;
}
return Arrays.equals(argbA, argbB);
}
private void recycleBitmaps(Bitmap bitmap1, Bitmap bitmap2)
{
bitmap1.recycle();
bitmap2.recycle();
bitmap1 = null;
bitmap2 = null;
}
- 1. 位图图像比较
- 2. Android - 比较缩略图位图和正常图像位图
- 3. 比较Android中的两个图像
- 4. 比较javascript中的图像
- 5. 比较SQL中的图像
- 6. Javascript比较图像
- 7. 比较图像matlab
- 8. iOS图像比较
- 9. 在gerrit中比较图像
- 10. 两幅图像的图像比较
- 11. 图片比较android
- 12. Android ImageButton图像与包含图像的变量比较
- 13. 比较GIF图像像素
- 14. 在比较图像之前匹配图像位置
- 15. android实时图像识别/比较
- 16. 标志的图像比较
- 17. 算法的图像比较
- 18. 如何比较图像(缓存图像)?
- 19. 比较图像(形状sihlohette图像)
- 20. c#位图块比较
- 21. 比较画布中的位图
- 22. 比较两幅图像
- 23. 使用SIFT比较图像
- 24. 我如何比较图像
- 25. 硒/ JAVA图像比较
- 26. 比较和匹配图像
- 27. 使用systemC比较图像
- 28. OSX:比较两个图像
- 29. 图像比较算法
- 30. 使用imagequick比较图像
注意,sameAs的方法没有出台,直到API V12(即安卓3.1),所以这是不是真的有帮助为旧版本。 – Ithildin 2011-07-05 06:56:55
minSdk 14所有的东西 – Blundell 2014-06-23 08:36:34
minSdk 19所有的东西 – ADev 2017-09-11 13:32:38