Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
619 views
in Technique[技术] by (71.8m points)

android - compare two images is same or not

I know how to compare two string is same or not.this is coding for compare two strings TextView t,t1;

String s,s1;
s=t.getText().toString();
s1=t1.setText().toString();
if(s.equals(s1)){
   t.setText("equal");
}
else{
   t.setText("not equal");
}

i need the coding for compare two images are same or not.please give me early

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Check that the height matches, if not return false. Then, check if the width matches, and if not, return false. Then check each pixel until you find one that doesn't match. When you do, return false. If every pixel matches, return true.

Pseudocode

bool imagesAreEqual(Image i1, Image i2)
{
    if (i1.getHeight() != i2.getHeight) return false;
    if (i1.getWidth() != i2.getWidth) return false;

    for (int y = 0; y < i1.getHeight(); ++y)
       for (int x = 0; x < i1.getWidth(); ++x)
            if (i1.getPixel(x, y) != i2.getPixel(x, y)) return false;

    return true;
}

In reality, you probably want to treat the image as a two dimensional array if you can, and just compare bytes. I don't know the Android image API, but getPixel might be slow.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...