Android image comparison (image similarity) code,

Source: Internet
Author: User

Android image comparison (image similarity) code,
Statement

This is a tool for image comparison in the past two days. Here, we will paste the core function code of the comparison for joint learning. If you have any shortcomings or shortcomings, please point out. Thank you. Lee, reproduced please indicate the source: http://blog.csdn.net/hnulwt/article/details/43668161

Preface

I compared images by comparing pixels one by one. Due to the memory size limit of android, we may need to consider the memory issue during image comparison, because loading large images may cause OOM. The vast majority of images are compressed to achieve the goal.
However, this article does not take into account the size of the image itself, because the image to be compared is not large enough. If there is a particularly large image that needs to be compared, then some optimization schemes may be considered. (currently, if the image is too large, you can continue to try to split the pixel values of each row to further reduce the value of int. However, if the image is too large and bitmap is obtained, OOM is the case. We know that we can use the image size method instead of directly loading the image to the memory. We can try this method, but we have not studied it yet, in this case, you can write a blog to further record how to optimize it.

Code explanation

This code mainly compares the pixels of bitmap through row-by-row acquisition, and returns the similarity of the image (if the two images are of different sizes, the returned size is different ).

Code
import java.text.DecimalFormat;import android.graphics.Bitmap;import android.graphics.BitmapFactory;public class BitmapCompare{    private static final String DIFFERENT_SIZE = "differentSize";    private static final String RESULT_FORMAT = "00.0%";    public static String similarity(String url1, String url2) {        Bitmap bm1 = BitmapFactory.decodeFile(url1);        Bitmap bm2 = BitmapFactory.decodeFile(url2);        return similarity(bm1, bm2);    }    public static String similarity(Bitmap bm1, Bitmap bm2) {        final int bm1Width = bm1.getWidth();        final int bm2Width = bm2.getWidth();        final int bmHeight = bm1.getHeight();        if (bmHeight != bm2.getHeight() || bm1Width != bm2Width)            return DIFFERENT_SIZE;        int[] pixels1 = new int[bm1Width];        int[] pixels2 = new int[bm2Width];        reset();        for (int i = 0; i < bmHeight; i++) {            bm1.getPixels(pixels1, 0, bm1Width, 0, i, bm1Width, 1);            bm2.getPixels(pixels2, 0, bm2Width, 0, i, bm2Width, 1);            comparePixels(pixels1, pixels2, bm1Width);        }        return percent(Count.sT, Count.sF + Count.sT);    }    private static void comparePixels(int[] pixels1, int[] pixels2, int length) {        for (int i = 0; i < length; i++) {            if (pixels1[i] == pixels2[i]) {                Count.sT++;            } else {                Count.sF++;            }        }    }    private static String percent(int divisor, int dividend) {        final double value = divisor * 1.0 / dividend;        DecimalFormat df = new DecimalFormat(RESULT_FORMAT);        return df.format(value);    }    private static void reset() {        Count.sT = 0;        Count.sF = 0;    }    private static class Count {        private static int sT;        private static int sF;    }}
Conclusion

In the image comparison process, the most violent way is to directly obtain the pixel value of the entire image, put it in the int [] array, and then compare it. Of course, this method has obvious disadvantages and does not need to be said. In the above Code, the bitmap. getPixel method still needs to be studied. If you do not understand it, please stamp: bitmap. getPixel () method learning. Other problems can be communicated with each other.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.