android – How to compare images?

Question:

I have a problem. I need to get the image from the ImageView and compare it with another image that I have in the Drawable and in case they are the same I Drawable to use later. The problem is that I can't compare them correctly. Can you help me?

This is the code of what I did but when I run it it tells me that the application stopped:

    public class MainActivity extends AppCompatActivity {

    ImageView iv1;
    Button b1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv1 = (ImageView) findViewById(R.id.iv1);
        b1 = (Button) findViewById(R.id.b1);
    }

    public void b1(View view){

        int tagImageView = (int) iv1.getTag();
        if (tagImageView==R.drawable.imagen){
            Bitmap bmap = BitmapFactory.decodeResource(iv1.getResources(), R.drawable.imagen);

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();

            String base64_imagen = Base64.encodeToString(byteArray, Base64.DEFAULT);

            SharedPreferences preferences = getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString("Guardar", base64_imagen);
            editor.apply();
        }
    }
}

Random to get image:

final Random rand = new Random();

            int arregloSeleccionado = rand.nextInt(100);

            if (arregloSeleccionado >= 0 && arregloSeleccionado <= 70) {
                final TypedArray imgs = getResources().obtainTypedArray(R.array.PlantasSimples);
                final Random rand2 = new Random();
                final int rndInt = rand2.nextInt(imgs.length());
                final int rID = imgs.getResourceId(rndInt, 0);
                planta.setImageResource(rID);                    

            } else if (arregloSeleccionado >= 71 && arregloSeleccionado <= 93) {
                final TypedArray imgs = getResources().obtainTypedArray(R.array.PlantasRaras);
                final Random rand2 = new Random();
                final int rndInt = rand2.nextInt(imgs.length());
                final int rID = imgs.getResourceId(rndInt, 0);
                planta.setImageResource(rID);                  

            } else if (arregloSeleccionado >= 94 && arregloSeleccionado <= 100) {
                final TypedArray imgs = getResources().obtainTypedArray(R.array.PlantaEspecial);
                final Random rand2 = new Random();
                final int rndInt = rand2.nextInt(imgs.length());
                final int rID = imgs.getResourceId(rndInt, 0);
                planta.setImageResource(rID);                    
            }              
        }

Answer:

You only need 1 step to verify this, you get the getTag from your ImageView but then compare with a resource, what you should do is create a second ImageView with your resource

ImageView image2 = (ImageView) findViewById(R.id.imageView);  
image2.setTag(R.drawable.image);
if (iv1.getTag().equals(image2.getTag())) {
      //son iguales
}
Scroll to Top