【发布时间】:2014-05-12 18:14:39
【问题描述】:
我正在尝试计算从图库中选择的图像中有多少种颜色,但我无法解决我的代码中的问题,我是 android 新手,请帮助我.
公共类 MainActivity 扩展 Activity {
private static int RESULT_LOAD_IMAGE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
String asd = picturePath;
ImageView imageView = (ImageView) findViewById(R.id.imgView);
Bitmap originalBm = BitmapFactory.decodeFile(picturePath);
Bitmap bmSolüst = Bitmap.createBitmap(originalBm, 0, 0, originalBm.getWidth()/2, (originalBm.getHeight() / 2));
ImageView imageView2 = (ImageView) findViewById(R.id.imgView2);
imageView2.setImageBitmap(bmSolüst);
Set<Integer> colors = new HashSet<Integer>();
int w = bmSagalt.getWidth();
int h = bmSagalt.getHeight();
for(int y = 0; y < h; y++) {
for(int x = 0; x < w; x++) {
int pixel = bmSolüst.getPixel(x, y);
colors.add(pixel);
}
TextView txt = (TextView) findViewById(R.id.textView2);
txt.setText(colors.size());
}
}
}
【问题讨论】:
-
什么是“代码中的问题”?
-
当我使用“TextView txt = (TextView) findViewById(R.id.textView2); txt.setText(colors.size());”我的项目出错了。我应该数颜色,我需要学习它们的数字,但我不会。
-
您可能遇到了 nullPointerException。发布您的 logcat 堆栈跟踪
-
我将我的 logcat 错误添加到我的问题中
标签: android image colors pixel pixels