2013-12-23 34 views
2

我已经编写了用于android上的图像处理的java。我尝试构建在图像特定区域平均颜色的方法。我发现了一些相同的警告,我真的不知道如何解决它。例如,警告告诉我“局部变量的值”红色“没有使用”,所以在第一个我通过在顶部声明int红色来解决,但它不能修复。 “红色”,“绿色”,“蓝色”,“xImage”,“yImage”都是一样的。另外,TextView在每个变量中都显示零。Java(android):如何在特定区域平均rgb

如果我把红色返回< < 16 |绿色< < 8 |蓝色;该警告丢失,但TextView仍显示为零。

这里是java代码。请帮助我T^T。

import java.io.File; 

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 

import android.os.Bundle; 
import android.os.Environment; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class ProcessPic extends Activity { 

int xImage,yImage,red,green,blue; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.layout_process); 


    String path = Environment.getExternalStorageDirectory()+ "/TestProcess/picture.jpg"; 
    File imgFile = new File(path); 

    Bitmap myBitmapPic = BitmapFactory.decodeFile(imgFile.getAbsolutePath());     
    ImageView myImage = (ImageView) findViewById(R.id.my_image); 
    myImage.setImageBitmap(myBitmapPic); 
    ProcessPic test = new ProcessPic(); 
    test.AverageColor(myBitmapPic, 0, 200, 0, 200); 


    TextView tv1 = (TextView)findViewById(R.id.textView1); 
    TextView tv2 = (TextView)findViewById(R.id.textView2); 
    TextView tv3 = (TextView)findViewById(R.id.textView3); 
    TextView tv4 = (TextView)findViewById(R.id.textView4); 
    TextView tv5 = (TextView)findViewById(R.id.textView5); 

    tv1.setText(Integer.toString(xImage)); 
    tv2.setText(Integer.toString(yImage)); 
    tv3.setText(Integer.toString(red)); 
    tv4.setText(Integer.toString(green)); 
    tv5.setText(Integer.toString(blue)); 

} 

public void AverageColor (Bitmap myBitmap,int minw, int maxw,int minh, int maxh){ 

    int xImage = myBitmap.getWidth(); 
    int yImage = myBitmap.getHeight(); 

    int red = 0; 
    int green = 0; 
    int blue = 0; 
    int count = 0; 

    for (int i=minw;i<maxw;i++){ 
     for (int j=minh;j<maxh;j++){ 
      int pixel = myBitmap.getPixel(i,j); 

      red += pixel >> 16 & 0xFF; 
      green += pixel >> 8 & 0xFF; 
      blue += pixel & 0xFF; 

      count++;   

     } 
    } 
    red /= count; 
    green /= count; 
    blue /= count; 
    //return red << 16 | green << 8 | blue; 

} 

} 

回答

2

您声明 int xImage,yImage,red,green,blue; 但你没有使用它们。

因此,你得到了警告。

因为您在AverageColor的函数中再次声明了局部变量(xImage,yImage,红色,绿色,蓝色)。你可以去掉AverageColor函数中的“int”。

+0

非常感谢。它真的工作:-) – user3101751