Skip to content
This repository has been archived by the owner on Jan 7, 2023. It is now read-only.

Fix output size (Crop is then just a region selector) #109

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions lib/src/main/java/com/soundcloud/android/crop/Crop.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ public class Crop {
static interface Extra {
String ASPECT_X = "aspect_x";
String ASPECT_Y = "aspect_y";
String MAX_X = "max_x";
String MAX_Y = "max_y";
String ERROR = "error";
String MAX_X = "max_x";
String MAX_Y = "max_y";
String ERROR = "error";
String FIX_X = "fix_x";
String FIX_Y = "fix_y";
String FIX_MIN = "fix_min";
}

private Intent cropIntent;
Expand Down Expand Up @@ -68,15 +71,52 @@ public Crop asSquare() {
/**
* Set maximum crop size
*
* Incompatible with {@link #fixedRegion(int, int)}
*
* @param width Max width
* @param height Max height
*/
public Crop withMaxSize(int width, int height) {
if (cropIntent.hasExtra(Extra.FIX_X)) {
throw new IllegalStateException("fixed dimensions specified, cannot set max size then");
}
cropIntent.putExtra(Extra.MAX_X, width);
cropIntent.putExtra(Extra.MAX_Y, height);
return this;
}

/**
* Enable the cropper to be used just as a image region selector, ensuring
* client application a desired width and height... user won't be able to resize
* Highlighted zone, just move it.
*
* The region specified is the target output fixed dimension, not screen dimensions.
*
* Note: this behaviour is incompatible with {@link #withMaxSize(int, int)}
*
* @param fixedWidth the with of the result
* @param fixedHeight the height of the result
*/
public Crop fixedRegion(int fixedWidth, int fixedHeight) {
if (cropIntent.hasExtra(Extra.MAX_X)) {
throw new IllegalStateException("maximum dimensions already defined, cannot use fixed dimensions now");
}
cropIntent.putExtra(Extra.FIX_X, fixedWidth);
cropIntent.putExtra(Extra.FIX_Y, fixedHeight);
return this;
}

/**
* Set up the crop to just crop (not resize) bound to the minimum image dimension, and as a square region.
*/
public Crop fixToMin() {
if (cropIntent.hasExtra(Extra.MAX_X)) {
throw new IllegalStateException("maximum dimensions already defined, cannot use fixed dimensions now");
}
cropIntent.putExtra(Extra.FIX_MIN, true);
return this.asSquare();
}

/**
* Send the crop Intent from an Activity
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public class CropImageActivity extends MonitoredActivity {
private int maxX;
private int maxY;
private int exifRotation;
private int fixedX;
private int fixedY;

// Extra options
private boolean fixToMin;


private Uri sourceUri;
private Uri saveUri;
Expand Down Expand Up @@ -113,6 +119,9 @@ private void setupFromIntent() {
aspectY = extras.getInt(Crop.Extra.ASPECT_Y);
maxX = extras.getInt(Crop.Extra.MAX_X);
maxY = extras.getInt(Crop.Extra.MAX_Y);
fixedX = extras.getInt(Crop.Extra.FIX_X);
fixedY = extras.getInt(Crop.Extra.FIX_Y);
fixToMin = extras.getBoolean(Crop.Extra.FIX_MIN, false);
saveUri = extras.getParcelable(MediaStore.EXTRA_OUTPUT);
}

Expand Down Expand Up @@ -215,10 +224,23 @@ private void makeDefault() {

Rect imageRect = new Rect(0, 0, width, height);

// Make the default size about 4/5 of the width or height
int cropWidth = Math.min(width, height) * 4 / 5;
@SuppressWarnings("SuspiciousNameCombination")
int cropHeight = cropWidth;
int cropWidth;
int cropHeight;

if (fixToMin) {
fixedX = fixedY = sampleSize * Math.min(width, height);
}

if (fixedY != 0 && fixedX != 0) {
cropWidth = Math.min(width, fixedX / sampleSize);
cropHeight = Math.min(height, fixedY / sampleSize);
} else {

// Make the default size about 4/5 of the width or height
cropWidth = Math.min(width, height) * 4 / 5;

cropHeight = cropWidth;
}

if (aspectX != 0 && aspectY != 0) {
if (aspectX > aspectY) {
Expand All @@ -232,7 +254,7 @@ private void makeDefault() {
int y = (height - cropHeight) / 2;

RectF cropRect = new RectF(x, y, x + cropWidth, y + cropHeight);
hv.setup(imageView.getUnrotatedMatrix(), imageRect, cropRect, aspectX != 0 && aspectY != 0);
hv.setup(imageView.getUnrotatedMatrix(), imageRect, cropRect, aspectX != 0 && aspectY != 0, fixedX == 0 && fixedY == 0);
imageView.add(hv);
}

Expand Down
12 changes: 10 additions & 2 deletions lib/src/main/java/com/soundcloud/android/crop/HighlightView.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ enum HandleMode { Changing, Always, Never }
private float handleRadius;
private float outlineWidth;
private boolean isFocused;
private boolean enableResize;

public HighlightView(View context) {
viewContext = context;
Expand All @@ -97,12 +98,13 @@ private void initStyles(Context context) {
}
}

public void setup(Matrix m, Rect imageRect, RectF cropRect, boolean maintainAspectRatio) {
public void setup(Matrix m, Rect imageRect, RectF cropRect, boolean maintainAspectRatio, boolean enableResize) {
matrix = new Matrix(m);

this.cropRect = cropRect;
this.imageRect = new RectF(imageRect);
this.maintainAspectRatio = maintainAspectRatio;
this.enableResize = enableResize;

initialAspectRatio = this.cropRect.width() / this.cropRect.height();
drawRect = computeLayout();
Expand Down Expand Up @@ -232,6 +234,7 @@ public int getHit(float x, float y) {
Rect r = computeLayout();
final float hysteresis = 20F;
int retval = GROW_NONE;
boolean allowFromEdges = false;

// verticalCheck makes sure the position is between the top and
// the bottom edge (with some tolerance). Similar for horizCheck.
Expand All @@ -254,8 +257,13 @@ public int getHit(float x, float y) {
retval |= GROW_BOTTOM_EDGE;
}

if (!enableResize && retval != GROW_NONE) {
// also allow movement from edges then...
allowFromEdges = true;
}

// Not near any edge but inside the rectangle: move
if (retval == GROW_NONE && r.contains((int) x, (int) y)) {
if ((retval == GROW_NONE && r.contains((int) x, (int) y)) || allowFromEdges) {
retval = MOVE;
}
return retval;
Expand Down