2012-10-05 3 views
1

메신저 내가증가/안드로이드에서 이미지의 각 채널을 감소

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     imageView=(ImageView) findViewById(R.id.imgView); 
     bgr = BitmapFactory.decodeResource(getResources(), R.drawable.test02); 


     int A, R, G, B; 
     int pixel; 

     // scan through all pixels 
     for(int x = 0; x < bgr.getWidth(); ++x) 
     { 
      for(int y = 0; y < bgr.getHeight(); ++y) 
      { 
       // get pixel color 
       pixel = bgr.getPixel(x, y); 
       A = Color.alpha(pixel); 
       R = Color.red(pixel); 
       G = Color.green(pixel); 
       B = Color.blue(pixel); 

       // increase/decrease each channel 
       R += value; 
       if(R > 255) { R = 255; } 
       else if(R < 0) { R = 0; } 

       G += value; 
       if(G > 255) { G = 255; } 
       else if(G < 0) { G = 0; } 

       B += value; 
       if(B > 255) { B = 255; } 
       else if(B < 0) { B = 0; } 

       // apply new pixel color to output bitmap 
       bgr.setPixel(x, y, Color.argb(A, R, G, B)); 
      } 
      System.out.println("x"); 
     } 

     imageView.setImageBitmap(bgr); 

    } 

문제가이 느린이 할 수있는 다른 방법입니다 에입니다이 코드를 사용하는 이리저리 몇 가지 이미지 필터를 구현하기 위해 노력하고 느리게하는 것입니다 그것 또는 그것을 빨리하십시오.

답변

3

당신은 ColorMatrix를 사용하는 것에 대해 읽어야합니다. 이 행렬은 수동으로 수행하는 작업을 정확하게 수행 할 수 있습니다. 귀하의 경우, 각 구성 요소에 상수 값을 추가하기 만하면 행렬은 a = g = m = s = 1e = j = o = value이되고 행렬의 나머지는 0이됩니다.

setColorFilter()을 사용하면 ImageView에 ColorMatrixColorFilter을 적용 할 수 있습니다.

+0

예. 감사합니다. +1 ~ ... – Youddh

0

어쩌면 비트 연산을 사용하면 더 빠릅니까?

for(int x = 0; x < bgr.getWidth(); ++x) 
    { 
     for(int y = 0; y < bgr.getHeight(); ++y) 
     { 
      // get pixel color 
      pixel = bgr.getPixel(x, y); 
    int alpha = (pixel & 0xff000000) >> 24; 
    int R = (pixel & 0x00ff0000) >> 16; 
     int G = (pixel & 0x0000ff00) >> 8; 
     int B = (pixel & 0x000000ff); 

      // increase/decrease each channel 
      R += value; 
      if(R > 255) { R = 255; } 
      else if(R < 0) { R = 0; } 

      G += value; 
      if(G > 255) { G = 255; } 
      else if(G < 0) { G = 0; } 

      B += value; 
      if(B > 255) { B = 255; } 
      else if(B < 0) { B = 0; } 

      // apply new pixel color to output bitmap 
      bgr.setPixel(x, y, (alpha << 24) + (red << 16) + (green << 8) + blue); 
     } 

는 내가 그것이 아마 더 효율적으로 동일한 필터링입니다 않는 가정하더라도, 그것을 할 수있는 가장 좋은 방법이 될 것 ColorMatrix를 사용하여, 너무 느린 aswell을 증명합니다.

관련 문제