我需要存储所有颜色的imageview在一个linkedlist,但它不工作



嗨,正如标题提到的,我想一行一行地存储图像的所有颜色在我的imageview链表中,因为我不知道大小。但它一直说x一定大于0或者y一定大于0,我谷歌了一下,修复了它,但错误仍然存在。

一开始我尝试触摸,但后来注释为尝试点击

with on touch我可以得到我触摸到的每个地方的颜色但是存储它仍然会给我错误

public class Encrypt extends Activity {
private static final int SELECTED_PICTURE=1;
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_encode);
    Button open = (Button) findViewById(R.id.button1);
    iv=(ImageView)findViewById(R.id.imageView1);
    final Button openGallery = (Button) findViewById(R.id.button1);
    openGallery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, SELECTED_PICTURE);
        }
    });

}
View.OnClickListener onClick1 = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        TextView txt = (TextView) findViewById(R.id.textView);

        iv.buildDrawingCache();
        Bitmap bitmap = iv.getDrawingCache();

        int pixel_old = 0;
        int pixel_new = 0;
        int count = 0;

        LinkedList<String> colors_list = new LinkedList<>();

        //txt.setText( " h: " + color);
        for (int i=iv.getMinimumHeight(); i< iv.getMaxHeight();i++){
            for(int j=iv.getMinimumWidth(); j<iv.getMaxWidth();j++){
                pixel_new= bitmap.getPixel(i,j);
                if(pixel_new != pixel_old){
                    colors_list.add(String.format("%06X",0xFFFFFF & pixel_new));
                    count =0;
                }
                else {
                }
                pixel_old=pixel_new;
            }
        }
        txt.setText(colors_list.get(1)+"   "+colors_list.get(2));
    }
};
/*
   OnTouchListener imgSourceOnTouchListener
        = new OnTouchListener(){
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        TextView txt = (TextView) findViewById(R.id.textView);

        iv.buildDrawingCache();
        Bitmap bitmap = iv.getDrawingCache();

        int x = (int)event.getX();
        int y = (int)event.getY();
        int pixel = bitmap.getPixel(x,y);
        String color = String.format("%06X",0xFFFFFF & pixel);



        txt.setText("p: "+pixel+" h: "+color);
        return true;
    }};
    */

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case SELECTED_PICTURE:
            if(resultCode==RESULT_OK){
                Uri uri=data.getData();
                String[]projection={MediaStore.Images.Media.DATA};
                Cursor cursor=getContentResolver().query(uri, projection, null, null, null);
                cursor.moveToFirst();
                int columnIndex=cursor.getColumnIndex(projection[0]);
                String filePath=cursor.getString(columnIndex);
                cursor.close();
                Bitmap yourSelectedImage=BitmapFactory.decodeFile(filePath);
                Drawable d=new BitmapDrawable(yourSelectedImage);
                iv.setBackground(d);
               // iv.setOnTouchListener(imgSourceOnTouchListener);
                iv.setOnClickListener(onClick1);
            }
            break;
        default:
            break;
    }

}

getDrawingCache()将返回整个View的位图,包括边界和填充,如果您想获得ImageView正在使用的图像的颜色,请尝试:

Bitmap  b = ((BitmapDrawable) iv.getDrawable()).getBitmap();
List<String> colors = new ArrayList<String>();
for(int x = 0; x < b.getWidth(); x++){
   for(int y = 0;y < b.getHeight(); y++){
      colors.add(String.format("%06X",0xFFFFFF & b.getPixel(x,y)));
   }
}

同样,getPixel(...)方法接受2个int参数,第一个是x坐标,第二个是y坐标,您在代码中颠倒了这个坐标,因为i是y坐标。

相关内容

  • 没有找到相关文章

最新更新