Skip to content

Commit

Permalink
fixed image processing when one dimension was < 612 and one was > 612.
Browse files Browse the repository at this point in the history
  • Loading branch information
markchang committed Mar 29, 2011
1 parent dc84abd commit d80ebb2
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 11 deletions.
9 changes: 5 additions & 4 deletions AndroidManifest.xml
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.acmelab.andgram"
android:versionCode="10"
android:versionName="1.81"
android:versionCode="11"
android:versionName="1.85"

>

Expand All @@ -14,8 +14,9 @@
<application android:label="Andgram"
android:icon="@drawable/icon"
android:theme="@android:style/Theme.Light.NoTitleBar"
android:debuggable="false">
<activity android:name=".TakePictureActivity">
android:debuggable="true">
<activity android:name=".TakePictureActivity"
android:configChanges="keyboardHidden|orientation">
</activity>
<activity android:name=".LoginActivity">
<intent-filter>
Expand Down
72 changes: 65 additions & 7 deletions src/org/acmelab/andgram/TakePictureActivity.java
Expand Up @@ -294,6 +294,7 @@ public Map<String, String> doUpload() {
}
}

// fixme: this is terrible, but gets it done
private void processImage(String filePath) {
Bitmap resizedBitmap;
Bitmap croppedBitmap;
Expand All @@ -319,31 +320,30 @@ private void processImage(String filePath) {
int srcWidth = srcBitmap.getWidth();
int srcHeight = srcBitmap.getHeight();
int desiredWidth = Utils.IMAGE_WIDTH;
int desiredHeight = Utils.IMAGE_HEIGHT;

// scale image short length to desiredWidth
// crop long dimension
if( srcWidth > desiredWidth && srcHeight > desiredWidth ) {
if( srcWidth < srcHeight ) {
int newWidth = desiredWidth;
float scaleRatio = (float)srcWidth/(float)desiredWidth;
float newHeight = (float)srcHeight / scaleRatio;
resizedBitmap = Bitmap.createScaledBitmap(srcBitmap, newWidth, (int)newHeight, true);
resizedBitmap = Bitmap.createScaledBitmap(srcBitmap, desiredWidth, (int)newHeight, true);
srcBitmap.recycle();

int offsetY = resizedBitmap.getHeight()/2 - desiredWidth / 2;
croppedBitmap = Bitmap.createBitmap(resizedBitmap, 0, offsetY, desiredWidth, desiredWidth);
croppedBitmap = Bitmap.createBitmap(resizedBitmap, 0, offsetY, desiredWidth, desiredHeight);
resizedBitmap.recycle();
roundedBitmap = getRoundedCornerBitmap(croppedBitmap);
croppedBitmap.recycle();
} else {
int newHeight = desiredWidth;
float scaleRatio = (float)srcHeight/(float)desiredWidth;
float newWidth = (float)srcWidth/scaleRatio;
resizedBitmap = Bitmap.createScaledBitmap(srcBitmap, (int)newWidth, newHeight, true);
resizedBitmap = Bitmap.createScaledBitmap(srcBitmap, (int)newWidth, desiredHeight, true);
srcBitmap.recycle();

int offsetX = resizedBitmap.getWidth()/2 - desiredWidth / 2;
croppedBitmap = Bitmap.createBitmap(resizedBitmap, offsetX, 0, desiredWidth, desiredWidth);
croppedBitmap = Bitmap.createBitmap(resizedBitmap, offsetX, 0, desiredWidth, desiredHeight);
resizedBitmap.recycle();
roundedBitmap = getRoundedCornerBitmap(croppedBitmap);
croppedBitmap.recycle();
Expand All @@ -362,12 +362,70 @@ private void processImage(String filePath) {
} catch( Exception e ) {

}
} else {
} else if( srcWidth <= desiredWidth && srcHeight <= desiredWidth ) {
Log.i(Utils.TAG, "Small image, leaving alone");
processedImageUri = srcImageUri;
} else {
// one dimension is smaller than desired, one is larger
// resize the larger dimension down then round it into 612x612
if( srcWidth > desiredWidth ) {
float scaleRatio = (float)srcWidth/(float)desiredWidth;
float newHeight = (float)srcHeight/scaleRatio;
resizedBitmap = Bitmap.createScaledBitmap(srcBitmap, desiredWidth, (int)newHeight, true);
srcBitmap.recycle();
roundedBitmap = getScaledRoundedCornerBitmap(resizedBitmap);
resizedBitmap.recycle();
} else {
float scaleRatio = (float)srcHeight/(float)desiredWidth;
float newWidth = (float)srcWidth/scaleRatio;
resizedBitmap = Bitmap.createScaledBitmap(srcBitmap, (int)newWidth, desiredHeight, true);
srcBitmap.recycle();
roundedBitmap = getScaledRoundedCornerBitmap(resizedBitmap);
resizedBitmap.recycle();
}

// Save
try {
File outputFile = new File(Environment.getExternalStorageDirectory(), Utils.OUTPUT_DIR + "/" + Utils.OUTPUT_FILE_PROCESSED);
processedImageUri = Uri.fromFile(outputFile);

FileOutputStream out = new FileOutputStream(processedImageFilename.toString());
roundedBitmap.compress(Bitmap.CompressFormat.JPEG,
Utils.IMAGE_JPEG_COMPRESSION_QUALITY, out);
roundedBitmap.recycle();
Log.i(TAG,"Processed image, now returning");
} catch( Exception e ) {

}
}
}

private Bitmap getScaledRoundedCornerBitmap(Bitmap bitmap) {
// paint bitmap into black bitmap
Bitmap roundedBitmap = Bitmap.createBitmap(Utils.IMAGE_WIDTH, Utils.IMAGE_HEIGHT, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(roundedBitmap);

final int color = 0xff424242;
final Paint paint = new Paint();
final Rect destRect = new Rect((Utils.IMAGE_WIDTH-bitmap.getWidth())/2,
Utils.IMAGE_BORDER,
(Utils.IMAGE_WIDTH)-(Utils.IMAGE_WIDTH-bitmap.getWidth())/2,
Utils.IMAGE_HEIGHT-Utils.IMAGE_BORDER);
final RectF rectF = new RectF(destRect);
final Rect srcRect = new Rect(0,0,bitmap.getWidth(), Utils.IMAGE_HEIGHT);
final float roundPx = Utils.IMAGE_CORNER_RADIUS;

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, srcRect, destRect, paint);

return roundedBitmap;
}

private Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Expand Down

0 comments on commit d80ebb2

Please sign in to comment.