Android/Java code to calculate the average color of an image


private int calculateAverageColor(android.graphics.Bitmap bitmap1, int pixelSpacing) {
        int R = 0;
        int G = 0;
        int B = 0;
        int n = 0;
        int res = 1;
        if (bitmap1 != null) {
            int height = bitmap1.getHeight();
            int width = bitmap1.getWidth();
            int[] pixels = new int[width * height];
            bitmap1.getPixels(pixels, 0, width, 0, 0, width, height);
            for (int i = 0; i < pixels.length; i += pixelSpacing) {
                int color = pixels[i];
                R += Color.red(color);
                G += Color.green(color);
                B += Color.blue(color);
                n++;
            }
            res = Color.rgb(R / n, G / n, B / n);
        }
        return res;
    }

Comments

Popular posts from this blog