Java tutorial
//package com.java2s; /* * Copyright 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.graphics.Bitmap; import android.graphics.Color; public class Main { /** * Find the difference between two bitmaps using average of per-pixel differences. * * @param a first {@link android.graphics.Bitmap}. * @param b second {@link android.graphics.Bitmap}. * @return the difference. */ public static double calcDifferenceMetric(Bitmap a, Bitmap b) { if (a.getWidth() != b.getWidth() || a.getHeight() != b.getHeight()) { throw new IllegalArgumentException("Bitmap dimensions for arguments do not match a=" + a.getWidth() + "x" + a.getHeight() + ", b=" + b.getWidth() + "x" + b.getHeight()); } // TODO: Optimize this in renderscript to avoid copy. int[] aPixels = new int[a.getHeight() * a.getWidth()]; int[] bPixels = new int[aPixels.length]; a.getPixels(aPixels, /*offset*/0, /*stride*/a.getWidth(), /*x*/ 0, /*y*/0, a.getWidth(), a.getHeight()); b.getPixels(bPixels, /*offset*/0, /*stride*/b.getWidth(), /*x*/ 0, /*y*/0, b.getWidth(), b.getHeight()); double diff = 0; for (int i = 0; i < aPixels.length; i++) { int aPix = aPixels[i]; int bPix = bPixels[i]; diff += Math.abs(Color.red(aPix) - Color.red(bPix)); // red diff += Math.abs(Color.green(aPix) - Color.green(bPix)); // green diff += Math.abs(Color.blue(aPix) - Color.blue(bPix)); // blue } diff /= (aPixels.length * 3); return diff; } }