Senin, 23 Juli 2018

Android code compare 2 image



package com.tes.compareimage;

import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;


public class TES extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        Drawable myDrawable1 = getResources().getDrawable(R.drawable.obj1a);
       // Bitmap myLogo1 = ((BitmapDrawable) myDrawable1).getBitmap();
       
        Drawable myDrawable2 = getResources().getDrawable(R.drawable.obj1a);
       // Bitmap myLogo2= ((BitmapDrawable) myDrawable2).getBitmap();
       
        Drawable myDrawable3 = getResources().getDrawable(R.drawable.obj1b);
        //Bitmap myLogo3= ((BitmapDrawable) myDrawable3).getBitmap();
       
        Drawable myDrawable4 = getResources().getDrawable(R.drawable.obj2a);
       // Bitmap myLogo4= ((BitmapDrawable) myDrawable4).getBitmap();
   
        boolean hasil=areDrawablesIdentical(myDrawable1,myDrawable2);
        Log.v("HASIL11",hasil+"#");
       
        boolean hasil2=areDrawablesIdentical(myDrawable1,myDrawable3);
        Log.v("HASIL11b",hasil2+"#");
       
        boolean hasil3=areDrawablesIdentical(myDrawable1,myDrawable4);
        Log.v("HASIL12",hasil3+"#");
       
       
    }


   
    @SuppressLint("NewApi") public boolean areDrawablesIdentical(Drawable drawableA, Drawable drawableB) {
        Drawable.ConstantState stateA = drawableA.getConstantState();
        Drawable.ConstantState stateB = drawableB.getConstantState();

        return (stateA != null && stateB != null && stateA.equals(stateB))|| getBitmap(drawableA).sameAs(getBitmap(drawableB));
    }

    public static Bitmap getBitmap(Drawable drawable) {
        Bitmap result;
        if (drawable instanceof BitmapDrawable) {
            result = ((BitmapDrawable) drawable).getBitmap();
        } else {
            int width = drawable.getIntrinsicWidth();
            int height = drawable.getIntrinsicHeight();
            // Some drawables have no intrinsic width - e.g. solid colours.
            if (width <= 0) {
                width = 1;
            }
            if (height <= 0) {
                height = 1;
            }

            result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(result);
            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            drawable.draw(canvas);
        }
        return result;
    }
}


Hasilnya:
PIC A dengan A=true;
PIC A dengan A'=false;
PIC A dengan B=false;





Tidak ada komentar:

Posting Komentar