지금 Bitmap 클래스를 이용해서 칼라이미지의 내부 데이터에 접근해서
흑백이미지로 변환하려고 작업하는 중이에요.
public Bitmap toYPlane() {
int width = mBmp.getWidth();
int height = mBmp.getHeight();
IntBuffer dst = IntBuffer.allocate(width*height);
for(int r = 0; r < height; ++r) {
for(int c = 0; c < width; ++c) {
int color = mBmp.getPixel(c, r);
double y = 0.2999*Color.red(color) + 0.587*Color.green(color) + 0.114*Color.blue(color);
dst.put(Color.rgb((int)y, (int)y, (int)y));
}
}
dst.rewind();
mBmp.copyPixelsFromBuffer(dst);
return mBmp;
}
이렇게 작업하고 있는데 내부 데이터에 접근은 가능하고 쓰여지기도 하는데,
r, g, b에 같은 값을 넣어주면 흑백이미지로 변할줄 알았는데 그렇게 안되더라구요.
이렇게 말고 다른 방식이 있는건가요.
그리고 지금은 한 픽셀을 표현하려고 4byte (int) 를 잡고 있는데,
한 픽셀을 1byte (char) 로 표현하게끔 (0~255) 까지 표현하게끔 변환하고 싶은데
Bitmap Config 를 보니까
Bitmap.Config | ALPHA_8 | |
Bitmap.Config | ARGB_4444 | |
Bitmap.Config | ARGB_8888 | |
Bitmap.Config | RGB_565 |
이렇게 밖에 없던데...........
Bitmap 클래스 자체에서 8bit Bitmap 이미지를 만드는건 없는건가요......
흑백변환 해보신 분들 도와주세요~~~
저는 직접 imageData에 접근해서 흑백변환하는 방법을 질문한 거였어요 ㅎㅎ
위에 방법으로 해서는 이미지가 이상하게 출력되구요,
직접 imageData에 따로 pixel을 담았다가, 그 pixel을 이용해서 CreateBitmap해서 하니까 잘 동작하네요 ㅎㅎ
해결했습니다~~~
필요하신 분들 참고하세요
// imageDir 파일을 읽어 mImageData에 이미지 데이터를 채워줌
public boolean Initialize(String imageDir) {
Bitmap bmp;
if((bmp = BitmapFactory.decodeFile(imageDir)) == null)
return false;
mWidth = bmp.getWidth();
mHeight = bmp.getHeight();
mStride = mWidth+(4-mWidth%4); // stride 4의 배수로 맞춤
mImageData = new int[mStride*mHeight];
for(int y = 0; y < mHeight; ++y) {
for(int x = 0; x < mWidth; ++x) {
mImageData[y*mStride+x] = bmp.getPixel(x, y);
}
}
return true;
}
// mImageData를 화면에 출력할 수 있게 Bitmap으로 만들어서 리턴
public Bitmap getBitmap() {
return Bitmap.createBitmap(mImageData, 0, mStride, mWidth, mHeight, Bitmap.Config.ARGB_8888);
}
public void toYPlane() {
for(int y = 0; y < mHeight; ++y) {
for(int x = 0; x < mWidth; ++x) {
int color = mImageData[y*mStride+x];
// double yy = 0.2999*Color.red(color) + 0.587*Color.green(color) + 0.114*Color.blue(color);
double yy = (Color.red(color) + Color.green(color) + Color.blue(color))/3.0;
mImageData[y*mStride+x] = (int)Color.rgb((int)yy, (int)yy, (int)yy);
}
}
}
이렇게 하니까 흑백으로 나오네요