CS6825: Computer Vision word cloud

Android Application Options for Comptuer Vision

OPTION 1 using built in camera app and Bitmap to do your own image processing

 

step 1: Use Camera to take picture and its is saved into a file on phone --

 

 

// MOST LIKELY INSIDE AN ACTIVITY CLASS ---class variables that are useful

File fileURL; // represent File of where we will store image we will take with camera

String mCurrentPhotoPath; //represents File location of the fileURL as a String

private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;

ImageView mImageView; //NOTE you will need to create this image by done line of code like this in the Activities
                                        //onCreate method
                                        //     mImageView = (ImageView) findViewByID(R.id.imageViewDisplay);

                                        // where imageViewDisplay is the ID of the XML ImageView specfied in Android GUI layout

//method to take a picture using Built-In camera app and store in specified location

  void takePicture()
{
      //get the picture taken by external existing Camera App right

        //away at start...could do instead as a result of Event handling

        // create Intent to take a picture and return control to the calling application

       //Create Intent LAUNCH built-in camera app
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

         //setup File URL for location for image
         fileURI = getOutputMediaFileUri(); // create a file to save the image

         //save location also as a String
        mCurrentPhotoPath = fileUri.toString();

         intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

        // start the image capture Intent-- this asks Android System to start the build in Camera App and return
        // when done to this activity by calling the below method onActivityResult(**) method
        // the name CAPTURE_IMGE_ACTIVITY_RESUEST_CODE is a made up value used in the onActivityResult
          startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

}

 

//method to generate a FILE to store the image in with naming IMG_timestamp**.jpg
    /** Create a File for saving an image */
    private static File getOutputMediaFile(){

        // To be safe, you should check that the SDCard is mountedi
        // using Environment.getExternalStorageState() before doing this.

        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyCameraApp");

        // This location works best if you want the created images to be shared
        // between applications and persist after your app has been uninstalled.

       // Create the storage directory if it does not exist
        if (! mediaStorageDir.exists()){

            if (! mediaStorageDir.mkdirs()){

                Log.d("MyCameraApp", "failed to create directory");

                return null;

            }

        }



      / / Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

        File mediaFile = new File(mediaStorageDir.getPath() + File.separator +   "IMG_"+ timeStamp + ".jpg");

        return mediaFile;

    }

}

 

 

 

step 2: when camera App is done taking picture it will call onActivityResult -- this is where you call your method to process the image

//NOTE: parameter mCurrentPhotoPath is the path to the file stored on your phone containing the image --see above.

//camera App is done taking picture and returning to this application's Activity by calling the onActivityResult method

onActivityResult( int requestCode, int resultCode, Intent data) {

        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) //if returning from the request above to take picture

             if (resultCode == RESULT_OK) { //if everything went okay

               // PROCESS the image

              Bitmap newBitMapImage = processImage(this.mCurrentPhotoPath);

           //display the processed image if you wish in the Activites ImageView GUI widget referred to as mImageView
            mImageView.setImageBitmap(bitmap); //display image we are working on in the ImageView Widget

             

            } else if (resultCode == RESULT_CANCELED) {

                // User cancelled the image capture -- do whatever is appropriate here

               

            } else {

                // Image capture failed, advise user -- do whatever is appropriate here

               

            }

step 3: process the image by first read the file where image stored into a Bitmap then create your own processing routines

private BitMap processImage(String mCurrentPhotoPath) {
    // Get the dimensions of the View   - NOTE: assumes the Activity has a ImageView object mImageView
    int targetW = mImageView.getWidth();
    int targetH = mImageView.getHeight();
 
    // Get the dimensions of the bitmap –NOTE: assumes mCurrentPhotoPath is location of image you took
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;
 
    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
 
    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    // READ in the picture we took prior to this method call from the mCurrentPhotoPath location
      Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);

  

     //process the image as you wish
     /*     NOTE: bitmap.getWidth() = with get number of columns/width of image

                          bitmap.getHeight() = height/ number of rows of image
                          bitmap.getPixel(c,r) = pixel at row r and column c

                           bitmap.setPixel(c,r) = set the pixel at row r and column c

      */

     for(int i=0; i<bmp.getWidth(); i++){
for(int j=0; j<bmp.getHeight(); j++){
int p = bmp.getPixel(i, j); //do whatever you want with pixel p like p = ******         bitmap.setPixel(i,j,p);

} }


       //Note rather that changing the values of bitmap you can create a second BitMap object and set its new pixel values to show the altered image that is up to you.

        return bitmap;


   
}

 

 

© Lynne Grewe