Android Open Source - CompareImageData_Android C I Main Activity






From Project

Back to project page CompareImageData_Android.

License

The source code is released under:

GNU General Public License

If you think the Android project CompareImageData_Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.blogspot.techzealous.compareimages;
/*from ww  w .  jav a 2  s .co  m*/
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.blogspot.techzealous.utils.CIComparator;
import com.blogspot.techzealous.utils.CIConstants;

public class CIMainActivity extends Activity {

  private Button buttonImage1;
  private Button buttonImage2;
  private Button buttonGo;
  private TextView textViewImage1;
  private TextView textViewImage2;
  private TextView textViewMethod1;
  private TextView textViewMethod2;
  private TextView textViewMethod3;
  private TextView textViewMethod4;
  private TextView textViewTime;
  
  private SharedPreferences prefs;
  private Handler mainHandler;
  private Runnable runnableCompare;
  private Runnable runnableResult;
  
  private int imageRqCode1 = 1;
  private int imageRqCode2 = 2;
  private boolean isLoadedImg1 = false;
  private boolean isLoadedImg2 = false;
  private long timeCompareSimple; //for both compareSimple and compareSimpleAVG
  private long timeVectorDiff;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    
    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    
    buttonImage1 = (Button) findViewById(R.id.buttonImage1);
    buttonImage2 = (Button) findViewById(R.id.buttonImage2);
    buttonGo = (Button) findViewById(R.id.buttonGo);
    textViewImage1 = (TextView) findViewById(R.id.textViewImageName1);
    textViewImage2 = (TextView) findViewById(R.id.textViewImageName2);
    textViewMethod1 = (TextView) findViewById(R.id.textViewMethod1);
    textViewMethod2 = (TextView) findViewById(R.id.textViewMethod2);
    textViewMethod3 = (TextView) findViewById(R.id.textViewMethod3);
    textViewMethod4 = (TextView) findViewById(R.id.textViewMethod4);
    textViewTime = (TextView) findViewById(R.id.textViewTime);
    
    mainHandler = new Handler();
    runnableCompare = new Runnable() {
      public void run() {
        if(isLoadedImg1 && isLoadedImg2) {
          long start = System.currentTimeMillis();
        
          CIComparator imageComp = new CIComparator();
          imageComp.setImages(prefs.getString(CIConstants.PREF_IMAGE1, ""), prefs.getString(CIConstants.PREF_IMAGE2, ""), prefs.getInt(CIConstants.PREF_SAMPLE_RATE1, 0), prefs.getInt(CIConstants.PREF_SAMPLE_RATE2, 0));
          imageComp.compareSimple();
          timeCompareSimple = System.currentTimeMillis() - start;
          
          start = System.currentTimeMillis();
          imageComp.compareVectorDiffAVG();
          timeVectorDiff = System.currentTimeMillis() - start;
          
          mainHandler.post(runnableResult);
        } else {
          mainHandler.post(runnableResult);
        }
      }
    };
    runnableResult = new Runnable() {
      public void run() {
        if(isLoadedImg1 && isLoadedImg2) {
          textViewTime.setText("Result time:" + "\n" + "compareSimple and SimpleAVG:" + timeCompareSimple + " ms" + "\n" + "compareVectorDiff:\t" + timeVectorDiff + "ms");
          textViewMethod1.setText(CIConstants.RESULT_COMPARESIMPLE);
          textViewMethod2.setText(CIConstants.RESULT_COMPARESIMPLEAVG);
          textViewMethod3.setText(CIConstants.RESULT_COMPAREVECTORDIFF_AVG);
          textViewMethod4.setText(CIConstants.RESULT_COMPAREVECTORDIFF);
        } else {
          Toast.makeText(CIMainActivity.this, getResources().getString(R.string.toast_images_notloaded), Toast.LENGTH_LONG).show();
        }
      }
    };
    
    
    buttonImage1.setOnClickListener(new OnClickListener() {  
      @Override
      public void onClick(View v) {
        Intent i = new Intent(Intent.ACTION_PICK, Uri.parse("image/*"));
        i.setType("image/*");
        startActivityForResult(i, imageRqCode1);
      }
    });
    
    buttonImage2.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent i = new Intent(Intent.ACTION_PICK, Uri.parse("image/*"));
        i.setType("image/*");
        startActivityForResult(i, imageRqCode2);
      }
    });
    
    buttonGo.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        Thread threadCompare = new Thread(runnableCompare);
        threadCompare.start();
      }
    });
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.cimain, menu);
    return true;
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if(requestCode == imageRqCode1 || requestCode == imageRqCode2) {
      if(resultCode == Activity.RESULT_OK) {
        Uri imageUri = intent.getData();
        
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        String[] fileSizeColumn = {MediaStore.Images.Media.SIZE};
        
        Cursor cursor = getContentResolver().query(imageUri, filePathColumn, null, null, null);
        String filePath = "";
        if(cursor.moveToFirst()) {
          int columnIndex = cursor.getColumnIndexOrThrow(filePathColumn[0]);
          filePath = cursor.getString(columnIndex);
          
          //get the size of the image in bytes (long)
          cursor = getContentResolver().query(imageUri, fileSizeColumn, null, null, null);
          long fileSize = 0;
          if(cursor.moveToFirst()) {
            int columnIndexSize = cursor.getColumnIndexOrThrow(fileSizeColumn[0]);
            fileSize = cursor.getLong(columnIndexSize);
            cursor.close();
          }
          
          if(requestCode == imageRqCode1) {
            //set sample rate according to image size
            int sampleRate = 0;
            if(fileSize < 512000) {
              sampleRate = 4;
            } else if(fileSize >= 512000 & fileSize < 1024000) {
              sampleRate = 8;
            } else if(fileSize >= 1024000) {
              sampleRate = 16;
            }
            prefs.edit().putInt(CIConstants.PREF_SAMPLE_RATE1, sampleRate).commit();
            prefs.edit().putString(CIConstants.PREF_IMAGE1, filePath).commit();
            isLoadedImg1 = true;
            textViewImage1.setText(filePath);
          } else {
            //set sample rate according to image size
            int sampleRate = 0;
            if(fileSize < 512000) {
              sampleRate = 2;
            } else if(fileSize >= 512000 & fileSize < 1024000) {
              sampleRate = 8;
            } else if(fileSize >= 1024000) {
              sampleRate = 16;
            }
            prefs.edit().putInt(CIConstants.PREF_SAMPLE_RATE2, sampleRate).commit();
            prefs.edit().putString(CIConstants.PREF_IMAGE2, filePath).commit();
            isLoadedImg2 = true;
            textViewImage2.setText(filePath);
          }
        }
        
        
        
        
      }
    }
  }

}




Java Source Code List

com.blogspot.techzealous.compareimages.CIMainActivity.java
com.blogspot.techzealous.utils.CIComparator.java
com.blogspot.techzealous.utils.CIConstants.java