Java BufferedImage Operation findavg(BufferedImage bimg)

Here you can find the source of findavg(BufferedImage bimg)

Description

findavg

License

Apache License

Declaration

public static double findavg(BufferedImage bimg) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.awt.image.BufferedImage;

public class Main {
    static int ALPHA = 3;
    static int RED = 0;
    static int GREEN = 1;
    static int BLUE = 2;

    public static double findavg(BufferedImage bimg) {
        double avg = 0;
        int avgarry[] = { 0, 32, 64, 128, 160, 192, 224, 255 };
        int max = 0;

        int maxCols = bimg.getWidth();
        int maxRows = bimg.getHeight();
        int[][][] pixel_rgb = BufferedImagetoPixel3DRGB(bimg);

        for (int row = 0; row < maxRows; row++) {
            for (int col = 0; col < maxCols; col++) {
                max = (pixel_rgb[row][col][RED] + pixel_rgb[row][col][BLUE] + pixel_rgb[row][col][GREEN]);
                avg = (avg + max / 3) / 2;
            }/* ww  w  .  j  av a2 s . c o m*/
        }
        // System.out.println("Average of Image " + avg);
        for (int i = 1; i < 8; i++) {
            if (avg < avgarry[i]) {
                avg = avgarry[i - 1];
                // System.out.println("Average of Image -final " + avg);
                return (avg);
            }
        }
        // System.out.println("Average of Image -final " + avg);
        return (avg);
    }

    public static int[][][] BufferedImagetoPixel3DRGB(BufferedImage bimg) {

        // System.out.println("Loaded Image : Image Type " + bimg.getType());
        int imgCols = bimg.getWidth();
        int imgRows = bimg.getHeight();
        // System.out.println("Image Rows " + imgRows + " Cols " + imgCols);
        int[] pixel = new int[imgCols * imgRows];
        pixel = bimg.getRGB(0, 0, imgCols, imgRows, pixel, 0, imgCols);

        // Create the One Dimensional array of type int to be populated with
        // pixel data, one int value
        // per pixel, with four color and alpha bytes per int value.

        int[][][] pixel_rgb = new int[imgRows][imgCols][4];
        for (int row = 0; row < imgRows; row++) {
            for (int col = 0; col < imgCols; col++) {
                int element = row * imgCols + col;
                // Alpha data
                pixel_rgb[row][col][ALPHA] = (pixel[element] >> 24) & 0xFF;
                // Red data
                pixel_rgb[row][col][RED] = (pixel[element] >> 16) & 0xFF;
                // Green data
                pixel_rgb[row][col][GREEN] = (pixel[element] >> 8) & 0xFF;
                // Blue data
                pixel_rgb[row][col][BLUE] = (pixel[element]) & 0xFF;
            }
        }
        return pixel_rgb;
    }
}

Related

  1. ensureIntRGB(final BufferedImage img)
  2. ensureRGBAImage(BufferedImage img)
  3. fadeImageByShape(BufferedImage bimg, Shape arbshape, double alpha, int rule)
  4. fadeImages(BufferedImage source1, BufferedImage source2, BufferedImage target, int relX, int targetX)
  5. fakeAOI(final BufferedImage pImage, final Rectangle pSourceRegion)
  6. findColor(BufferedImage image, int startX, int startY, int dirX, int dirY, int colorIndex)
  7. findDifference(BufferedImage img2, BufferedImage img1)
  8. findDominantColor(BufferedImage paramBufferedImage)
  9. findLegacyColorModel(BufferedImage image)