我已经编写了在 android 上运行的图像处理 java。我尝试建立一种方法来平均图像特定区域的颜色。我发现了一些相同的警告,我真的不知道如何解决它。例如,警告告诉我“未使用局部变量“red”的值”,因此在第一个中我通过在顶部声明 int red 来解决,但它无法修复。 “红色”,“绿色”,“蓝色”,“xImage”,“yImage”都是相同的。此外,TextView 在每个变量中都显示零。
如果我把 return red << 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;
}
}
最佳答案
你声明了 int xImage,yImage,红,绿,蓝; 但你没有使用它们。
因此,您收到了警告。
因为你在AverageColor函数中再次声明了局部变量(xImage,yImage,red,green,blue)。您可以删除AverageColor函数中的“int”。
关于Java(安卓): how to average rgb in specific area,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20739609/