Skip to content

Commit

Permalink
refactored code for getting camera and gallery data loaded into the a…
Browse files Browse the repository at this point in the history
…pp. related to #57 and other tickets.
  • Loading branch information
n8fr8 committed Aug 23, 2011
1 parent 6f06981 commit ef624e0
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 72 deletions.
78 changes: 51 additions & 27 deletions src/org/witness/sscphase1/CameraObscuraMainMenu.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package org.witness.sscphase1;

import java.io.File;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.Menu;
Expand All @@ -25,6 +29,18 @@ public class CameraObscuraMainMenu extends Activity implements OnClickListener {

final static int ABOUT = 0;

final static String CAMERA_TMP_FILE = "tmp.jpg";

@Override
protected void onDestroy() {
super.onDestroy();

File tmpFile = new File(this.getExternalFilesDir(null),CAMERA_TMP_FILE);
if (tmpFile.exists())
tmpFile.delete();
}


Button choosePictureButton, takePictureButton;

Uri imageFileUri;
Expand Down Expand Up @@ -65,12 +81,12 @@ public void onClick(View v) {

} else if (v == takePictureButton) {

// Create the Uri, this should put it in the gallery, is this desired?
imageFileUri = getContentResolver().insert(
Media.EXTERNAL_CONTENT_URI, new ContentValues());

imageFileUri = Uri.fromFile( new File(getExternalFilesDir(null),CAMERA_TMP_FILE));

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
//i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
i.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri); // URI of the file where pic will be stored

startActivityForResult(i, CAMERA_RESULT);

takePictureButton.setVisibility(View.VISIBLE);
Expand All @@ -81,34 +97,42 @@ public void onClick(View v) {
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);

if (resultCode == RESULT_OK) {
if (requestCode == GALLERY_RESULT || requestCode == CAMERA_RESULT) {

if (requestCode == GALLERY_RESULT || imageFileUri == null) {

// If imageFileUri is null and we are coming from the camera app,
// the below code will may give us a small version of the image
// I am not sure we want that...
// http://code.google.com/p/android/issues/detail?id=1480
imageFileUri = intent.getData();

}

if (requestCode == GALLERY_RESULT)
{
imageFileUri = intent.getData();

// This comes back null if we are rotated as the activity is restarted
// Let's lock in portrait for now
if (imageFileUri != null)
{
Intent passingIntent = new Intent(this,ImageEditor.class);
passingIntent.setData(imageFileUri);
startActivityForResult(passingIntent, IMAGE_EDITOR);
}

}
else if (requestCode == CAMERA_RESULT)
{
File fileTmp = new File(this.getExternalFilesDir(null),CAMERA_TMP_FILE);

if (fileTmp.exists())
{
imageFileUri = Uri.fromFile(fileTmp);
Intent passingIntent = new Intent(this,ImageEditor.class);
passingIntent.setData(imageFileUri);
startActivity(passingIntent);
}
else if (intent.hasExtra("data"))
{
Bitmap b = (Bitmap) intent.getExtras().get("data");

Intent passingIntent = new Intent(this,ImageEditor.class);
passingIntent.putExtra("bitmap", b);
startActivity(passingIntent);

if (imageFileUri != null)
{
Log.v(LOGTAG,"Sending: " + imageFileUri.toString());

Intent passingIntent = new Intent(this,ImageEditor.class);
passingIntent.setData(imageFileUri);
startActivityForResult(passingIntent, IMAGE_EDITOR);
}
}
}


}

/*
Expand Down
130 changes: 85 additions & 45 deletions src/org/witness/sscphase1/ImageEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,32 @@ public void onCreate(Bundle savedInstanceState) {
zoomIn.setOnClickListener(this);
zoomOut.setOnClickListener(this);


// Instantiate the vibrator
vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

// Passed in from CameraObscuraMainMenu
originalImageUri = getIntent().getData();

// If originalImageUri is null, we are likely coming from another app via "share"
if (originalImageUri == null && getIntent().hasExtra(Intent.EXTRA_STREAM)) {
originalImageUri = (Uri) getIntent().getExtras().get(Intent.EXTRA_STREAM);
if (originalImageUri == null)
{
if (getIntent().hasExtra(Intent.EXTRA_STREAM))
{
originalImageUri = (Uri) getIntent().getExtras().get(Intent.EXTRA_STREAM);
}
else if (getIntent().hasExtra("bitmap"))
{
Bitmap b = (Bitmap)getIntent().getExtras().get("bitmap");
setBitmap(b);
originalImageWidth = b.getWidth();
originalImageHeight = b.getHeight();
return;

}
}


// Instantiate the vibrator
vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);


// Load the image if it isn't null
if (originalImageUri != null) {
Expand All @@ -199,12 +214,10 @@ public void onCreate(Bundle savedInstanceState) {
// Load up the image's dimensions not the image itself
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;

// Needs to be this config for Google Face Detection
bmpFactoryOptions.inPreferredConfig = Bitmap.Config.RGB_565;

// Parse the image
imageBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(originalImageUri), null, bmpFactoryOptions);
Bitmap loadedBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(originalImageUri), null, bmpFactoryOptions);

// Hold onto the unscaled dimensions
originalImageWidth = bmpFactoryOptions.outWidth;
Expand Down Expand Up @@ -240,54 +253,66 @@ public void onCreate(Bundle savedInstanceState) {

// Decode it for real
bmpFactoryOptions.inJustDecodeBounds = false;
imageBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(originalImageUri), null, bmpFactoryOptions);
Log.v(LOGTAG,"Was: " + imageBitmap.getConfig());
loadedBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(originalImageUri), null, bmpFactoryOptions);
Log.v(LOGTAG,"Was: " + loadedBitmap.getConfig());

if (imageBitmap == null) {
if (loadedBitmap == null) {
Log.v(LOGTAG,"bmp is null");
}

float matrixWidthRatio = (float) currentDisplay.getWidth() / (float) imageBitmap.getWidth();
float matrixHeightRatio = (float) currentDisplay.getHeight() / (float) imageBitmap.getHeight();

// Setup the imageView and matrix for scaling
float matrixScale = matrixHeightRatio;

if (matrixWidthRatio < matrixHeightRatio) {
matrixScale = matrixWidthRatio;
}

imageView.setImageBitmap(imageBitmap);

PointF midpoint = new PointF((float)imageBitmap.getWidth()/2f, (float)imageBitmap.getHeight()/2f);
matrix.postScale(matrixScale, matrixScale);

// This doesn't completely center the image but it get's closer
int fudge = 42;
matrix.postTranslate((float)((float)currentDisplay.getWidth()-(float)imageBitmap.getWidth()*(float)matrixScale)/2f,(float)((float)currentDisplay.getHeight()-(float)imageBitmap.getHeight()*matrixScale)/2f-fudge);

imageView.setImageMatrix(matrix);
}
else
setBitmap (loadedBitmap);

} catch (IOException e) {
e.printStackTrace();
Log.e(LOGTAG, "error loading bitmap from Uri: " + e.getMessage(), e);
}

// Set the OnTouch and OnLongClick listeners to this (ImageEditor)
imageView.setOnTouchListener(this);
imageView.setOnLongClickListener(this);

// Layout for Image Regions
regionButtonsLayout = (RelativeLayout) this.findViewById(R.id.RegionButtonsLayout);

// Do auto detect popup

Toast autodetectedToast = Toast.makeText(this, "Detecting faces...", Toast.LENGTH_SHORT);
autodetectedToast.show();
mHandler.postDelayed(mUpdateTimeTask, 1000);

}
}

private void setBitmap (Bitmap nBitmap)
{
imageBitmap = nBitmap;

// Get the current display to calculate ratios
Display currentDisplay = getWindowManager().getDefaultDisplay();

float matrixWidthRatio = (float) currentDisplay.getWidth() / (float) imageBitmap.getWidth();
float matrixHeightRatio = (float) currentDisplay.getHeight() / (float) imageBitmap.getHeight();

// Setup the imageView and matrix for scaling
float matrixScale = matrixHeightRatio;

if (matrixWidthRatio < matrixHeightRatio) {
matrixScale = matrixWidthRatio;
}

imageView.setImageBitmap(imageBitmap);

PointF midpoint = new PointF((float)imageBitmap.getWidth()/2f, (float)imageBitmap.getHeight()/2f);
matrix.postScale(matrixScale, matrixScale);

// This doesn't completely center the image but it get's closer
int fudge = 42;
matrix.postTranslate((float)((float)currentDisplay.getWidth()-(float)imageBitmap.getWidth()*(float)matrixScale)/2f,(float)((float)currentDisplay.getHeight()-(float)imageBitmap.getHeight()*matrixScale)/2f-fudge);

imageView.setImageMatrix(matrix);

// Set the OnTouch and OnLongClick listeners to this (ImageEditor)
imageView.setOnTouchListener(this);
imageView.setOnLongClickListener(this);

// Layout for Image Regions
regionButtonsLayout = (RelativeLayout) this.findViewById(R.id.RegionButtonsLayout);

// Do auto detect popup

Toast autodetectedToast = Toast.makeText(this, "Detecting faces...", Toast.LENGTH_SHORT);
autodetectedToast.show();
mHandler.postDelayed(mUpdateTimeTask, 1000);
}
/*
* Call this to delete the original image, will ask the user
*/
Expand Down Expand Up @@ -504,7 +529,7 @@ public boolean onTouch(View v, MotionEvent event)

matrix.postTranslate(event.getX() - startPoint.x, event.getY() - startPoint.y);
imageView.setImageMatrix(matrix);
// Reset the start point
// // Reset the start point
startPoint.set(event.getX(), event.getY());

putOnScreen();
Expand Down Expand Up @@ -575,6 +600,21 @@ public boolean onTouch(View v, MotionEvent event)
return handled; // indicate event was handled
}

public void moveAndZoom (float x, float y, float scale)
{
matrix.postTranslate(x - startPoint.x, y - startPoint.y);
imageView.setImageMatrix(matrix);
// // Reset the start point
startPoint.set(x, y);

matrix.postScale(scale, scale, startFingerSpacingMidPoint.x, startFingerSpacingMidPoint.y);

imageView.setImageMatrix(matrix);

putOnScreen();
redrawRegions();

}
/*
* For live previews
*/
Expand Down

0 comments on commit ef624e0

Please sign in to comment.