Interesting/ANDROID | Posted by hyena0 2010. 1. 21. 21:14

[ANDROID] color mask 예제



 [ANDROID] color mask 예제

  과거에 작성했던 컬러마스크 예제를 첨부합니다.

  지속적으로 SDK 가 업데이트 되는 바람에 커스터마이징을

  해야할 겁니다.

  현재 1.6 SDK 환경에서는 동작하지 않는 군요.

  G1 폰이 업데이트가 되질 않아서 SDK 업그레이드도 멈췄지요.

  bmptest 라는 프로젝트로 작성된 코드는 아래와 같습니다.

package com.google.android.bmptest;

import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.graphics.*;
import android.graphics.drawable.*;
import android.view.*;

public class bmptest extends Activity {
    /** Called when the activity is first created. */

 public int[]    mColors;
    @Override
    public void onCreate(Bundle icicle) {
     
        super.onCreate(icicle);
        LinearLayout linLayout = new LinearLayout(this);
        
           Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
               R.drawable.fedor);  //drawable 폴더에 fedor.bmp라는 파일이 있어야 합니다.
                                           // 원하는 파일을 넣고 파일명을 바꾸면 되겠죠.
        int width = bitmapOrg.getWidth();
        int height = bitmapOrg.getHeight();
        int newWidth = 300;
        int newHeight = 300; 
        //RGB 호출
        int[] colors = new int[width*height];
        int[][] rgbCol = new int[3][width*height];
        for(int i=0;i<2;i++){
         mColors = mask2Colors(bitmapOrg,i);
         rgbCol[i] = mColors;
        }
        for(int j=0;j<(width*height);j++){
         colors[j] = rgbCol[0][j]|rgbCol[1][j]|rgbCol[2][j]; 
        }
       
               
        // calculate the scale - in this case = 0.4f
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
       
        // createa matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight); 
        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(colors, 0, width,width,height,//STRIDE, WIDTH, HEIGHT,
                Bitmap.Config.RGB_565);
        Bitmap resizedBitmap1 =
         Bitmap.createBitmap(resizedBitmap, 0, 0,
                          width, height, matrix, true);
   
        // make a Drawable from Bitmap to allow to set the BitMap
        // to the ImageView, ImageButton or what ever

        BitmapDrawable bmd = new BitmapDrawable(resizedBitmap1);
       
        ImageView imageView = new ImageView(this);
       
        // set the Drawable on the ImageView
        imageView.setImageDrawable(bmd);
     
        // center the Image
        imageView.setScaleType(ImageView.ScaleType.CENTER);
            
        // add ImageView to the Layout
        linLayout.addView(imageView,
          new LinearLayout.LayoutParams(
                      LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT
                )
        );
       
        // set LinearLayout as ContentView
        setContentView(linLayout);
    }
   

  private int[] mask2Colors(Bitmap bitmapO, int sltColor) {
     int W,H,ST;
     int orgColor=0;
     int greenColor=0;
     //get Height and Width
     H=bitmapO.getHeight();
     W=bitmapO.getWidth();
     ST = W;
          
     int[] colors = new int[ST * H];
     int avrColor=0;
      //make mask
      int[] masks = {1,1,1,1,1,1,1,1,1};
      //Mask height, width
     int Mh = 3; int Mw =3;
      //blur
     int var=0;
            
     //get bitmap color
      for (int y = 0; y < H; y++) {
             for (int x = 0; x < W; x++) {
              if(sltColor == 0){
               orgColor = Color.red(bitmapO.getPixel(x, y));
              }
              if(sltColor == 1){
               orgColor = Color.green(bitmapO.getPixel(x, y));
              }
              if(sltColor == 2){
               orgColor = Color.blue(bitmapO.getPixel(x, y));
              }
              colors[y * ST + x] = orgColor;
             }
         }
      //calc. average value of colors
     for (int i=0;i<H*W;i++){
      avrColor += colors[i];
     }
     avrColor /= H*W;
     //mask the color
     //i는 계산될 높이 계산될 폭만큼의 반복범위임

     for (int i=0;i<(H-Mh)*(W-Mw);i++){
      
       //mask 계산
        var = colors[i]*masks[0]      + colors[i+1]*masks[1]      + colors[i+2]*masks[2]
             +colors[W+i]*masks[3]    + colors[W+i+1]*masks[4]    + colors[W+i+2]*masks[5]
             +colors[(W+1)+i]*masks[6]+ colors[(W+1)+i+1]*masks[7]+ colors[(W+1)+i+2]*masks[8];
        
        var = var/9; //1/9 효과를 나타냄
        //blur일 경우는 평균값 더하지 않기
        //var +=avrColor;
        colors[i]=var;
        var =0;//var 초기화       
     }
     
     //make argb style
        for (int y = 0; y < H; y++) {
            for (int x = 0; x < W; x++) {
             if(sltColor ==0){
              orgColor = colors[y * ST + x];
              orgColor = orgColor<<16;//red shift
              orgColor = 0x00FF0000 & orgColor;
             }
             if(sltColor ==1){
              orgColor = colors[y * ST + x];
              orgColor = orgColor<<8;//green shift
              orgColor = 0x0000FF00 & orgColor;
             }
             if(sltColor == 2){
              orgColor = colors[y * ST + x];
             }
             colors[y * ST + x] = orgColor;
            }
        }
      return colors;
    }
   }

  이상과 같습니다.

  주석을 보고 이해 안가는 부분이 있으시면 답글주세요.

 

 

 





[Android] 이미지 마스크 처리 - 엠보싱 효과

사진을 표시하는 예제는 앞에서 해보았다.

이 예제를 바탕으로 마스크 처리를 통해 이미지가 어떻게 변화되는지, 어떻게 처리할 것인지 고민해 보려고 한다.

앞서 봤듯이 안드로이드에서 BMP 처리가 1차원 배열로 되어 있으므로 평면 계산을 2차원으로 바꾸어도 되지만, 시간지연이 많이 발생하므로 1차원을 2차원으로 고려하여 미리 계산해 수식을 만들어 놓고 프로세싱되도록 구현하였다.

자세한 내용은 다음 포스팅에 하기로 하고 주요 코드는 아래와 같다.

private int[] maskColors(Bitmap bitmapO) {
     int W,H,ST;
     //get Height and Width
     H=bitmapO.getHeight();
     W=bitmapO.getWidth();
     ST = W;
     int[] colors = new int[ST * H];
     int avrColor=0;
     //embossing 효과
     //45도  {0,0,-1,0,0,0,1,0,0}
     //90도  {0,-1,0,0,0,0,0,1,0}
     //135도{-1,0,0,0,0,0,0,0,1}

     int[] masks = {-1,0,0,0,0,0,0,0,1};
     //Mask height, width
     int Mh = 3; int Mw =3;
     int var =0;//MASK 계산될 색의 픽셀값
            
     //get bitmap color
        for (int y = 0; y < H; y++) {
            for (int x = 0; x < W; x++) {
             colors[y * ST + x] = bitmapO.getPixel(x, y);
            }
        }
      //calc. average value of colors
     for (int i=0;i<H*W;i++){
      avrColor += colors[i];
     }
     avrColor /= H*W;
     //mask the color
     //i는 계산될 높이 계산될 폭만큼의 반복범위임
     for (int i=0;i<(H-Mh)*(W-Mw);i++){      
       //mask 계산
        var = colors[i]*masks[0]      + colors[i+1]*masks[1]      + colors[i+2]*masks[2]
             +colors[W+i]*masks[3]    + colors[W+i+1]*masks[4]    + colors[W+i+2]*masks[5]
             +colors[(W+1)+i]*masks[6]+ colors[(W+1)+i+1]*masks[7]+ colors[(W+1)+i+2]*masks[8];
        var +=avrColor; //마스크 값에 색의 평균값을 더함
        colors[i]=var; //계산된 마스크 값을 픽셀값으로 저장함
        var =0;//var 초기화       
     }
      return colors;
    }

해당 maskColors 함수를 Activity에서 호출하여 BMP 객체를 생성하여 넣어주면 아래와 같은 결과를 얻을 수 있다.
<<135도 엠보싱>>

<<90도 엠보싱>>

<<45도 엠보싱>>
Interesting/ANDROID | Posted by hyena0 2008. 11. 19. 01:25

[Android]이미지 회전하기




[안드로이드] 이미지 회전하기

안드로이드에서 이미지를 호출하여 회전하는 방법을 소개한다.
해당 소스코드는 직접작성한 것은 아니고, anddev.org 사이트에서 검색해서 얻은 결과이다.
매트릭스를 이용하여 이미지를 회전시키고 있다.

아래를 참조해 보세요.

package com.google.android.bmptest;

import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.graphics.*;
import android.graphics.drawable.*;
import android.view.*;

public class bmptest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        LinearLayout linLayout = new LinearLayout(this);

        // load the origial BitMap (500 x 500 px)
        Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
               R.drawable.fedor); //미리 해당 폴더에 이미지 파일이 있어야 한다. fedor.jpg
      
        int width = bitmapOrg.getWidth();
        int height = bitmapOrg.getHeight();
        int newWidth = 200;
        int newHeight = 200;
       
        // calculate the scale - in this case = 0.4f
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
       
        // create a matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);
        // rotate the Bitmap
        matrix.postRotate(45); 
        
        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                          width, height, matrix, true);
   
        // make a Drawable from Bitmap to allow to set the BitMap
        // to the ImageView, ImageButton or what ever

        BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
       
        ImageView imageView = new ImageView(this);
       
        // set the Drawable on the ImageView
        imageView.setImageDrawable(bmd);
     
        // center the Image
        imageView.setScaleType(ImageView.ScaleType.CENTER);
            
        // add ImageView to the Layout
        linLayout.addView(imageView,
          new LinearLayout.LayoutParams(
                      LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT
                )
        );
       
        // set LinearLayout as ContentView
        setContentView(linLayout);
    }
}

결과는 아래와 같다.