搜尋

首頁  >  問答  >  主體

java - 如何将一张普通图片转成64级灰度图片?

如何将一张普通图片转成64级灰度图片?
在Java或者Android平台上。

黄舟黄舟2804 天前635

全部回覆(2)我來回復

  • 大家讲道理

    大家讲道理2017-04-18 10:49:23

        public static Bitmap convertGreyImg(Bitmap img) {
            int width = img.getWidth();         //获取位图的宽
            int height = img.getHeight();       //获取位图的高
    
            int[] pixels = new int[width * height]; //通过位图的大小创建像素点数组
    
            img.getPixels(pixels, 0, width, 0, 0, width, height);
            int alpha = 0xFF << 24;
            for (int i = 0; i < height; i++) {
                for (int j = 0; j < width; j++) {
                    int original = pixels[width * i + j];
                    int red = ((original & 0x00FF0000) >> 16);
                    int green = ((original & 0x0000FF00) >> 8);
                    int blue = (original & 0x000000FF);
    
                    int grey = (int) ((float) red * 0.3 + (float) green * 0.59 + (float) blue * 0.11);
                    grey = alpha | (grey << 16) | (grey << 8) | grey;
                    pixels[width * i + j] = grey;
                }
            }
            Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
            result.setPixels(pixels, 0, width, 0, 0, width, height);
            return result;
        }

    參考資料:Android實現圖片相似度

    回覆
    0
  • ringa_lee

    ringa_lee2017-04-18 10:49:23

    去看下Android的這個類別ColorMatrix
    Android的矩陣(一):ColorMatrix

    回覆
    0
  • 取消回覆