圖片處理成灰色的方法

public Bitmap geBitmap(byte[] byt) {// 圖片灰暗處理

  Bitmap bitmap;

  bitmap = BitmapFactory.decodeByteArray(byt, 0, byt.length);
  int width = bitmap.getWidth(); // 獲取位圖的寬
  int height = bitmap.getHeight(); // 獲取位圖的高
  int[] pixels = new int[width * height]; // 通過位圖的大小創建像素點數組
  bitmap.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 grey = pixels[width * i + j];
    int red = ((grey & 0x00FF0000) >> 16);
    int green = ((grey & 0x0000FF00) >> 8);
    int blue = (grey & 0x000000FF);
    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, Config.RGB_565);
  result.setPixels(pixels, 0, width, 0, 0, width, height);

  return result;

 }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章