Android 获取桌面壁纸亮度

2018-12-24  本文已影响0人  kongjn
public class WallPaperUtils {

    private static final String TAG = WallPaperUtils.class.getSimpleName();

    private Handler mHandler = new Handler(Looper.getMainLooper());
    private Context mContext;

    private LoadWallPaperCallback loadWallPaperCallback;

    public WallPaperUtils(Context context) {
        mContext = context;
        init(context);
    }

    private BroadcastReceiver wallPaperReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            LogUtils.i(TAG, "onReceive:" + action);
            if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
                doWallpaperWork();
            }
        }
    };

    private void init(Context context) {
        mContext = context;
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_WALLPAPER_CHANGED);
        context.registerReceiver(wallPaperReceiver, intentFilter);
    }

    public void getWallPagerBright(LoadWallPaperCallback loadWallPaperCallback) {
        this.loadWallPaperCallback = loadWallPaperCallback;
        doWallpaperWork();
    }


    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            LogUtils.i(TAG, "start doWallpaperWork");
            Drawable wallpaper = mContext.getWallpaper();
            if (wallpaper != null) {
                Bitmap      scaleBitmap = scaleMatrix(drawable2bitmap(wallpaper), 0.05f, 0.05f);
                final int   brightJava  = getBright(scaleBitmap);
                final float saturation  = getSaturation(scaleBitmap);
                LogUtils.i(TAG, "brightJava:" + brightJava);
                LogUtils.i(TAG, "saturation:" + saturation);
                if (loadWallPaperCallback != null) {
                    loadWallPaperCallback.onWallPaperBrightChanged(brightJava);
                }
            }
            LogUtils.i(TAG, "doWallpaperWork end");
        }
    };

    private void doWallpaperWork() {
        mHandler.post(runnable);
    }

    private Bitmap drawable2bitmap(Drawable drawable) {
        BitmapDrawable bd = (BitmapDrawable) drawable;
        return bd.getBitmap();
    }

    private static Bitmap scaleMatrix(Bitmap bitmap, float scaleX, float scaleY) {
        LogUtils.i(TAG, "scaleMatrix start");
        try {
            int    w      = bitmap.getWidth();
            int    h      = bitmap.getHeight();
            float  scaleW = scaleX;
            float  scaleH = scaleY;
            Matrix matrix = new Matrix();
            // 长和宽放大缩小的比例
            matrix.postScale(scaleW, scaleH);
            Bitmap scaleBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, false);
            LogUtils.i(TAG, "scaleMatrix normal end");
            return scaleBitmap;

        } catch (Exception e) {
            e.printStackTrace();
        }
        LogUtils.i(TAG, "scaleMatrix error end");
        return bitmap;
    }


    /**
     * 获取图片亮度 范围(0~255)
     *
     * @param bm Bitmap
     * @return Bright
     */
    private int getBright(Bitmap bm) {
        LogUtils.i(TAG, "getBright start");
        if (bm == null) {
            return -1;
        }
        int width  = bm.getWidth();
        int height = bm.getHeight();
        int r, g, b;
        int count  = 0;
        int bright = 0;
        count = width * height;
        int[] buffer = new int[width * height];

        bm.getPixels(buffer, 0, width, 0, 0, width, height);
        LogUtils.i(TAG, "width:" + width + ",height:" + height);
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                //bm.getPixel(i, j);
                int localTemp = buffer[j * width + i];
                r = (localTemp >> 16) & 0xff;
                g = (localTemp >> 8) & 0xff;
                b = localTemp & 0xff;
                bright = (int) (bright + 0.299 * r + 0.587 * g + 0.114 * b);
            }
        }
        LogUtils.i(TAG, "getBright end");
        return bright / count;
    }

    /**
     * 获取图片饱和度Saturation 范围(0~1)
     *
     * @param bm Bitmap
     * @return Saturation
     */
    private float getSaturation(Bitmap bm) {
        LogUtils.i(TAG, "getSaturation start");
        if (bm == null) {
            return -1;
        }
        int   width      = bm.getWidth();
        int   height     = bm.getHeight();
        int   r, g, b;
        int   count      = 0;
        float saturation = 0;
        count = width * height;
        int[] buffer = new int[width * height];

        bm.getPixels(buffer, 0, width, 0, 0, width, height);
        LogUtils.i(TAG, "width:" + width + ",height:" + height);
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                int localTemp = buffer[j * width + i];
                r = (localTemp >> 16) & 0xff;
                g = (localTemp >> 8) & 0xff;
                b = localTemp & 0xff;

                int max = Math.max(Math.max(r, g), b);
                int min = Math.min(Math.min(r, g), b);


                saturation += max == 0 ? 0 : (max - min) / (float) max;

            }
        }
        LogUtils.i(TAG, "getSaturation end");
        return saturation / (float) count;
    }

    /**
     * 获取图片色相Hue 范围(0~1)
     *
     * @param bm Bitmap
     * @return Hue
     */
    private float getHue(Bitmap bm) {
        LogUtils.i(TAG, "getHue start");
        if (bm == null) {
            return -1;
        }
        int   width  = bm.getWidth();
        int   height = bm.getHeight();
        int   r, g, b;
        int   count  = 0;
        float hue    = 0;
        float hueSum = 0;
        count = width * height;
        int[] buffer = new int[width * height];

        bm.getPixels(buffer, 0, width, 0, 0, width, height);
        LogUtils.i(TAG, "width:" + width + ",height:" + height);
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                int localTemp = buffer[j * width + i];
                r = (localTemp >> 16) & 0xff;
                g = (localTemp >> 8) & 0xff;
                b = localTemp & 0xff;

                int max = Math.max(Math.max(r, g), b);
                int min = Math.min(Math.min(r, g), b);

                if (r == max) {
                    hue = (max - min) == 0 ? 0 : ((g - b) / (float) (max - min));
                }
                if (g == max) {
                    hue = 2 + ((max - min) == 0 ? 0 : ((b - r) / (float) (max - min)));

                }
                if (b == max) {
                    hue = 4 + ((max - min) == 0 ? 0 : ((r - g) / (float) (max - min)));

                }
                hue = (hue / 6f);

                if (hue < 0) {
                    hue = (hue / 360f + 1);
                }
                hueSum += hue;
            }
        }
        LogUtils.i(TAG, "getHue end");
        return hueSum / (float) count;
    }


    public interface LoadWallPaperCallback {

        /**
         * 墙纸亮度的回调
         *
         * @param bright 亮度
         */
        void onWallPaperBrightChanged(int bright);
    }
}
上一篇下一篇

猜你喜欢

热点阅读