안드로이드(Android) 비트맵의 픽셀값을 읽어와 화면에 그리기
이번 예제는 ImageView 레이아웃에 비트맵을 그리는 것이 아니라 Canvas 에 그린다.
그리고 새로운 픽셀값 저장을 위한 메모리 공간을 만들고 거기에 읽어들인 Bitmap
픽셀값을 넣는다. 복사한 픽셀값으로 비트맵을 만들어 보여주는것이다.
이 과정은 createColors 에서 수행되는데 처음 로딩될 때 읽어들인 비트맵의
픽셀값을 하나하나씩 읽어들인다.
// 픽셀값 넣을 배열을 만들고 읽어온 이미지 픽셀값을 넣는다
int[] colors = new int[mST * mHeight];
for (int y = 0; y < mHeight; y++) {
for (int x = 0; x < mWidth; x++) {
colors[y * mST + x] = mBitmap.getPixel(x, y);
Log.d("BitmapView","X="+x+",Y="+y);
}
}
위 소스는 전체 비트맵을 읽어들여 사용하기 때문에 시간이 오래걸리고 비효율적이다.
특정 위치의 비트맵에 대한 정보를 가져오기 위해 유용하다. 그럼 전체 비트맵을
리턴받은 픽셀값이 저장된 배열 int[] colors 을 Bitmap.createBitmap 파라미터로 넘겨
다시 비트맵을 생성한다.
mBitmaps[0] = Bitmap.createBitmap(
colors, 0, mST, mWidth, mHeight, Bitmap.Config.ARGB_8888);
전체 소스는 다음과 같다
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
public class Main extends Activity {
private Bitmap mBitmap;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.helicopter_medical);
setContentView(new BitmapView(this));
}
public class BitmapView extends View{
private Bitmap mViewBitmap;
private int[] mColors;
private Paint mPaint;
private int mWidth = 0;
private int mHeight = 0;
private int mST = 0;
public BitmapView(Context context){
super(context);
setFocusable(true);
mColors = createColors();
int[] colors = mColors;
mViewBitmap = Bitmap.createBitmap(colors, 0, mST, mWidth, mHeight, Bitmap.Config.ARGB_8888);
mPaint = new Paint();
mPaint.setDither(true);
}
@Override
protected void onDraw(Canvas canvas) {
int winWidth = getWidth();
int winHeight = getHeight();
// 이미지 그리기
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(mViewBitmap, winWidth / 2 - mWidth, winHeight / 2 - mHeight, null);
canvas.translate(0, mViewBitmap.getHeight());
super.onDraw(canvas);
}
private int[] createColors(){
int[] colors = null;
mWidth = mBitmap.getWidth();
mHeight = mBitmap.getHeight();
mST = mWidth + 10; // 넓이보다 10을 크게 설정
colors = new int[mST * mHeight];
for (int y = 0; y < mHeight; y++) {
for (int x = 0; x < mWidth; x++) {
colors[y * mST + x] = mBitmap.getPixel(x, y);
}
}
return colors;
}
}
}
|
비트맵을 읽어들이는 또 다른 방법 - 2 |
이것은 픽셀값만 저장된 데이터로 비트맵을 생성하는 예를 보여준것이며 영상처리
프로그램할 때 쓰일 만한 예제인것같다. 하지만 위의 방법은 이미지를 분석하거나
특정픽셀값을 변경하여 이미지에 특별한 효과를 주는등의 작업만 가능하다. 왜냐하면
전체 이미지의 픽셀을 읽어들여 새로 만든 메모리에 집어넣는 과정이 오래걸리기
때문에 비 효율적이다. 그래서 Bitmap 클래스에서는 getPixels 라는 함수를 제공한다.
for 문을 돌려 하나하나씩 집어넣지 않고 픽셀값 저장할 메모리 객체를 파라미터로 넘기면된다
비트맵을 읽어서 픽셀값을 저장하는 곳이다. 미리생성한 colors를 getPixels 의 파라미터로 넘긴다 위의 예제처럼 번거롭게 for문을 돌려 일일이 집어넣지 않아도 된다.
colors = new int[mWidth * mHeight]; mBitmap.getPixels(colors, 0, mWidth, 0, 0, mWidth, mHeight);
읽어온 픽셀값을 다시 셋팅하고 화면에 그린다. 여기서 이미지 확대/축소, 픽셀값을 조절하여
영상처리등을 할수 있을 것이다.
Bitmap mViewBitmap; int[] colors = createColors(); mViewBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888); mViewBitmap.setPixels(colors, 0, mWidth, 0, 0, mWidth, mHeight);전체소스는 다음과 같다
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
public class Main extends Activity {
private Bitmap mBitmap;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.helicopter_medical);
setContentView(new BitmapView(this));
}
public class BitmapView extends View{
private Bitmap mViewBitmap;
private int mWidth = 0;
private int mHeight = 0;
public BitmapView(Context context){
super(context);
setFocusable(true);
int[] colors = createColors();
// 픽셀작업
// 이미지 그리기
mViewBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
mViewBitmap.setPixels(colors, 0, mWidth, 0, 0, mWidth, mHeight);
}
@Override
protected void onDraw(Canvas canvas) {
int winWidth = getWidth();
int winHeight = getHeight();
// 이미지 그리기
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(mViewBitmap, winWidth / 2 - mWidth, winHeight / 2 - mHeight, null);
canvas.translate(0, mViewBitmap.getHeight());
super.onDraw(canvas);
}
private int[] createColors(){
// 픽셀값 넣을 배열을 만들고 읽어온 이미지 픽셀값을 넣는다
int[] colors = null;
mWidth = mBitmap.getWidth();
mHeight = mBitmap.getHeight();
colors = new int[mWidth * mHeight];
mBitmap.getPixels(colors, 0, mWidth, 0, 0, mWidth, mHeight);
return colors;
}
}
}
'안드로이드 개발' 카테고리의 다른 글
| 안드로이드(Android) XML 을 사용하여 이미지 버튼 배경 로딩하기 (0) | 2014.10.13 |
|---|---|
| 안드로이드(Android) SeekBar 로 배경색 변경하기 (0) | 2014.10.09 |
| 안드로이드(Android) android.util.DisplayMetrics 이용해 해상도를 측정하는 코드 (0) | 2014.09.27 |
| 안드로이드(Android) 미리 정의된 테마를 AndroidManifest.xml 적용하기 (0) | 2014.09.25 |
| 안드로이드(Android) 옵션메뉴셋팅과 다이얼로그 띄우기 (3) | 2014.09.12 |
| 안드로이드(android) 에뮬레이터 지니모션(Genymotion) 네트워크 에러 날 때 (0) | 2014.04.20 |
| 지니모션(Genymotion) 에서 안드로이드(android) 실행을 위한 Virtual devices 생성 (0) | 2014.04.01 |
| 안드로이드(Android) 와 제니모션(Genymotion) 를 연결해서 테스트하기 (0) | 2014.03.31 |
