본문 바로가기
IT 개발

android bitmap 이미지 색반전

by 로보찌빠냥 2023. 5. 3.
반응형

안드로이드를 개발하다 보면 여러가지 이유로 비트맵의 색을 반전시켜야 할 때가 있습니다. 이미지 뷰에서 가져온 이미지를 ocr등에 인식률을 높이기 위한 전처리로 흑백 처리 이미지 반전등은 기본적으로 많이 사용하는 방법입니다.

 

bitmap 이미지 반전 시키기

bitmap형태의 데이터를 색반전 하기 위해서는 아래와 같은 펑션을 만들어서 사용하면 간편합니다.

 public Bitmap invertImage(Bitmap src) {
  // create new bitmap with the same attributes(width,height)
  //as source bitmap
  Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
  // color info
  int A, R, G, B;
  int pixelColor;
  // image size
  int height = src.getHeight();
  int width = src.getWidth();

  // scan through every pixel
  for (int y = 0; y < height; y++)
  {
   for (int x = 0; x < width; x++)
   {
    // get one pixel
    pixelColor = src.getPixel(x, y);
    // saving alpha channel
    A = Color.alpha(pixelColor);
    // inverting byte for each R/G/B channel
    R = 255 - Color.red(pixelColor);
    G = 255 - Color.green(pixelColor);
    B = 255 - Color.blue(pixelColor);
    // set newly-inverted pixel to output image
    bmOut.setPixel(x, y, Color.argb(A, R, G, B));
   }
  }

  // return final bitmap
  return bmOut;
 }

 

자세한 내용과 전체 앱 소스 코드는 아래 링크를 확인해 주세요

출처 : https://shaikhhamadali.blogspot.com/2013/06/gray-scale-image-in-imageview-android.html

반응형

댓글