【问题标题】:How can i count that how many kinds of colors(pixels) in the image android我怎么能算出图像android中有多少种颜色(像素)
【发布时间】: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


【解决方案1】:

我认为问题可能是您尝试使用带有非字符串参数的 setText。

试试这个代码:

txt.setText(String.valueOf(colors.size()));

我希望这会有所帮助。

【讨论】:

  • 谢谢。我解决了我的问题。我意识到我没有在我的 activity_main.xml 文件中添加“android:contentDescription="@string/hello_world"”代码。
猜你喜欢
  • 2015-03-29
  • 2011-02-22
  • 1970-01-01
  • 2020-08-03
  • 2012-01-20
  • 1970-01-01
  • 1970-01-01
  • 2013-12-13
  • 1970-01-01
相关资源
最近更新 更多