Android Open Source - sana Image Processing Task






From Project

Back to project page sana.

License

The source code is released under:

Copyright (c) 2010, Moca All rights reserved. The source code for Moca is licensed under the BSD license as follows: Redistribution and use in source and binary forms, with or without modification, ...

If you think the Android project sana 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 org.moca.task;
//  www .  j a v a 2  s  .  com
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.moca.db.EventDAO;
import org.moca.db.ImageProvider;
import org.moca.db.MocaDB.ImageSQLFormat;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

public class ImageProcessingTask extends AsyncTask<ImageProcessingTaskRequest, Void, Void> {
  public static final String TAG = ImageProcessingTask.class.toString();
  
  
  private void logDebugInformationAboutCameraIntents(Intent data) {
    // Print some diagnostic information about the Intent sent back
    // from the Camera app so that if a manufacturer does goofy
    // things in the future we have a fighting chance at remotely
    // debugging it.
    if (data != null) {
      Log.i(TAG, "Received intent from Camera application. May need to enable workaround. The Intent's Action is: " + data.getAction());

      // The HTC Tattoo's Camera app just sends back a content://
      // Uri pointing to the image in the Intent. It also included
      // a Parcelable android.graphics.Bitmap object in the extras
      // bundle, except it's a tiny image (320x240).
      Uri returnedUri = data.getData();
      if (returnedUri != null) {
        Log.i(TAG, "Received Uri from Camera application: " + returnedUri);
      }

      Bundle b = data.getExtras();
      if (b != null) {
        Log.d(TAG, "Camera intent had bundle.");
        for (String key : b.keySet()) {
          Log.d(TAG, "Camera Intent Bundle has key: " + key);
        }
        
        // Try to get the bitmap and poke at it.
        Bitmap bitmap = b.getParcelable("data");
        if (bitmap != null) {
          int iWidth = bitmap.getWidth();
          int iHeight = bitmap.getHeight();
          Log.i(TAG, "Intent had a bitmap Parcel in the 'data' extra. width: " + iWidth + " height: " + iHeight);
        }
      }
    }

  }
  
  /**
   * Some Android manufacturers have decided to replace the
   * Google Camera application with their own, buggy camera
   * application. This method attempts to smooth over the
   * differences and undo the damage done by HTC to Android.
   * 
   * This currently has a workaround for the HTC Tattoo only.
   * 
   * @return An InputStream to the image we just captured with the Camera app.
   * @throws FileNotFoundException
   */
  private InputStream getImageInputStreamWithWorkaround(ImageProcessingTaskRequest request) throws FileNotFoundException {
    InputStream is = null; 
    // Workaround for phones using a custom Camera application like the HTC Tattoo.
    if (request.tempImageFile.exists()) {
      Log.i(TAG, "Temp file exists, no workaround needed.");
      is = new FileInputStream(request.tempImageFile);
    } else if (request.intent != null) {
      Log.i(TAG, "HTC Sense Workaround active.");
      // The Tattoo's Camera app will not store the file in the tempImageFile path. Instead it will return a Uri pointing to the file. 
      Uri fileUri = request.intent.getData();
      Log.i(TAG, "HTC Sense Workaround active. File uri is: " + fileUri.toString());
      if (fileUri != null) {
        is = request.c.getContentResolver().openInputStream(fileUri);
      }
    }
    return is;
  }     

  @Override
  protected Void doInBackground(ImageProcessingTaskRequest... params) {
    ImageProcessingTaskRequest request = params[0];
    
    if (request == null) {
      Log.e(TAG, "Didn't receive valid ImageProcessingTaskRequest");
      return null;
    }
    
    File tempImageFile = request.tempImageFile;
    String savedProcedureId = request.savedProcedureId;
    String elementId = request.elementId;
    Context c = request.c;
    
    logDebugInformationAboutCameraIntents(request.intent);
    
    // Get the image parameters stored in the Intent
    ContentValues values = new ContentValues();
    values.put(ImageSQLFormat.SAVED_PROCEDURE_ID, savedProcedureId);
    values.put(ImageSQLFormat.ELEMENT_ID, elementId);
    
    Uri imageUri = c.getContentResolver().insert(ImageSQLFormat.CONTENT_URI, values);
    Uri thumbUri = ImageProvider.getThumbUri(imageUri);

    Log.i(TAG, "Old URI: " + imageUri);
    Log.i(TAG, "Thumb URI: " + thumbUri);
        
    try {
      InputStream is = getImageInputStreamWithWorkaround(request); 
      BitmapFactory.Options options = new BitmapFactory.Options();
      options.inJustDecodeBounds = true;
      options.inSampleSize = 1;
      // Only decode the size, not the image itself. If we
      // decode the full image then we sometimes get Out
      // of Memory errors.
      BitmapFactory.decodeStream(is, null, options);
      //BitmapFactory.decodeFile(tempImageFile.getAbsolutePath(), options); //Works for Android 1.5
      is.close();
      
      int iWidth = options.outWidth;
      int iHeight = options.outHeight;
      
      Log.i(TAG, "Image capture activity returned bitmap of width " + iWidth + " and height " + iHeight);
                                  
      OutputStream os = c.getContentResolver().openOutputStream(imageUri);
      is = getImageInputStreamWithWorkaround(request);
      
      final int bufSize = 4096;
      byte[] buffer = new byte[bufSize];
      int bytesRead = 0;
      while (bytesRead != -1) {
        os.write(buffer, 0, bytesRead);
        bytesRead = is.read(buffer, 0, bufSize);
      } 
      is.close();
      os.flush();
      os.close();
      
      int thumbCompression = 50;
      int thumbMaxSize = 100;
      int largestDimension = (iWidth > iHeight) ? iWidth : iHeight;
      
      Log.i(TAG, "Saving thumbnail for " + imageUri + " with " + thumbCompression + "% quality.");
      // We want a picture with it's largest side 100 pixels.
      int scaleFactor = largestDimension / thumbMaxSize;
      options = new BitmapFactory.Options();
      options.inSampleSize = scaleFactor;
      
      is = getImageInputStreamWithWorkaround(request);
      Bitmap thumbBitmap = BitmapFactory.decodeStream(is, null, options);
      
      os = c.getContentResolver().openOutputStream(thumbUri);
      thumbBitmap.compress(Bitmap.CompressFormat.JPEG, thumbCompression, os);
      os.flush();
      os.close();
      is.close();
      thumbBitmap.recycle();

      // Flag the file as saved - does not record image size
      values = new ContentValues();
      values.put(ImageSQLFormat.FILE_VALID, true);
      c.getContentResolver().update(imageUri, values, null, null);
      
      if (tempImageFile.exists()) {
        tempImageFile.delete();
      }
      
      Log.i(TAG, "Successfully saved " + imageUri);
    } catch (FileNotFoundException e) {
      Log.e(TAG, "While storing the image, got an exception: " + e.toString());
      EventDAO.logException(c, e);
    } catch (IOException e) {
      Log.e(TAG, "While storing the image, got an exception: " + e.toString());
      EventDAO.logException(c, e);
    } 
    
    return null;
  }

}




Java Source Code List

.Moca.java
org.moca.Constants.java
org.moca.ImagePreviewDialog.java
org.moca.ScalingImageAdapter.java
org.moca.SelectableImageView.java
org.moca.activity.NotificationList.java
org.moca.activity.NotificationViewer.java
org.moca.activity.PatientInfoDialog.java
org.moca.activity.ProcedureRunner.java
org.moca.activity.ProceduresList.java
org.moca.activity.SavedProcedureList.java
org.moca.activity.Settings.java
org.moca.db.EncounterDAO.java
org.moca.db.EventDAO.java
org.moca.db.EventProvider.java
org.moca.db.Event.java
org.moca.db.ImageProvider.java
org.moca.db.MocaDB.java
org.moca.db.NotificationMessage.java
org.moca.db.NotificationProvider.java
org.moca.db.PatientInfo.java
org.moca.db.PatientProvider.java
org.moca.db.PatientValidator.java
org.moca.db.ProcedureDAO.java
org.moca.db.ProcedureProvider.java
org.moca.db.SavedProcedureProvider.java
org.moca.db.SoundProvider.java
org.moca.media.AudioPlayer.java
org.moca.net.MDSCode.java
org.moca.net.MDSInterface.java
org.moca.net.MDSNotification.java
org.moca.net.MDSResult.java
org.moca.net.SMSReceive.java
org.moca.procedure.BinaryUploadElement.java
org.moca.procedure.DateElement.java
org.moca.procedure.GpsElement.java
org.moca.procedure.MultiSelectElement.java
org.moca.procedure.PatientIdElement.java
org.moca.procedure.PictureElement.java
org.moca.procedure.ProcedureElement.java
org.moca.procedure.ProcedurePage.java
org.moca.procedure.ProcedureParseException.java
org.moca.procedure.Procedure.java
org.moca.procedure.RadioElement.java
org.moca.procedure.SelectElement.java
org.moca.procedure.SoundElement.java
org.moca.procedure.TextElement.java
org.moca.procedure.TextEntryElement.java
org.moca.procedure.ValidationError.java
org.moca.procedure.branching.Criteria.java
org.moca.procedure.branching.Criterion.java
org.moca.procedure.branching.LogicAnd.java
org.moca.procedure.branching.LogicBase.java
org.moca.procedure.branching.LogicNot.java
org.moca.procedure.branching.LogicOr.java
org.moca.service.BackgroundUploader.java
org.moca.service.QueueManager.java
org.moca.service.ServiceConnector.java
org.moca.service.ServiceListener.java
org.moca.task.CheckCredentialsTask.java
org.moca.task.ImageProcessingTaskRequest.java
org.moca.task.ImageProcessingTask.java
org.moca.task.MDSSyncTask.java
org.moca.task.PatientLookupListener.java
org.moca.task.PatientLookupTask.java
org.moca.task.ResetDatabaseTask.java
org.moca.task.ValidationListener.java
org.moca.util.MocaUtil.java
org.moca.util.UserDatabase.java