Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to set result min size #480

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
43 changes: 41 additions & 2 deletions sample/src/main/java/com/yalantis/ucrop/sample/SampleActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public class SampleActivity extends BaseActivity implements UCropFragmentCallbac
private static final String SAMPLE_CROPPED_IMAGE_NAME = "SampleCropImage";

private RadioGroup mRadioGroupAspectRatio, mRadioGroupCompressionSettings;
private EditText mEditTextMaxWidth, mEditTextMaxHeight;
private EditText mEditTextMinWidth, mEditTextMinHeight, mEditTextMaxWidth, mEditTextMaxHeight;
private EditText mEditTextRatioX, mEditTextRatioY;
private CheckBox mCheckBoxMaxSize;
private CheckBox mCheckBoxMinSize, mCheckBoxMaxSize;
private SeekBar mSeekBarQuality;
private TextView mTextViewQuality;
private CheckBox mCheckBoxHideBottomControls;
Expand Down Expand Up @@ -145,9 +145,12 @@ public void onClick(View v) {
settingsView = findViewById(R.id.settings);
mRadioGroupAspectRatio = findViewById(R.id.radio_group_aspect_ratio);
mRadioGroupCompressionSettings = findViewById(R.id.radio_group_compression_settings);
mCheckBoxMinSize = findViewById(R.id.checkbox_min_size);
mCheckBoxMaxSize = findViewById(R.id.checkbox_max_size);
mEditTextRatioX = findViewById(R.id.edit_text_ratio_x);
mEditTextRatioY = findViewById(R.id.edit_text_ratio_y);
mEditTextMinWidth = findViewById(R.id.edit_text_min_width);
mEditTextMinHeight = findViewById(R.id.edit_text_min_height);
mEditTextMaxWidth = findViewById(R.id.edit_text_max_width);
mEditTextMaxHeight = findViewById(R.id.edit_text_max_height);
mSeekBarQuality = findViewById(R.id.seekbar_quality);
Expand Down Expand Up @@ -184,6 +187,8 @@ public void onStopTrackingTouch(SeekBar seekBar) {
}
});

mEditTextMinHeight.addTextChangedListener(mMinSizeTextWatcher);
mEditTextMinWidth.addTextChangedListener(mMinSizeTextWatcher);
mEditTextMaxHeight.addTextChangedListener(mMaxSizeTextWatcher);
mEditTextMaxWidth.addTextChangedListener(mMaxSizeTextWatcher);
}
Expand All @@ -206,6 +211,26 @@ public void afterTextChanged(Editable s) {
}
};

private TextWatcher mMinSizeTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {
if (s != null && !s.toString().trim().isEmpty()) {
if (Integer.valueOf(s.toString()) < UCrop.MIN_SIZE) {
Toast.makeText(SampleActivity.this, String.format(getString(R.string.format_min_cropped_image_size), UCrop.MIN_SIZE), Toast.LENGTH_SHORT).show();
}
}
}
};

private TextWatcher mMaxSizeTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Expand Down Expand Up @@ -301,10 +326,24 @@ private UCrop basisConfig(@NonNull UCrop uCrop) {
break;
}

if (mCheckBoxMinSize.isChecked()) {
try {
int minWidth = Integer.valueOf(mEditTextMinWidth.getText().toString().trim());
int minHeight = Integer.valueOf(mEditTextMinHeight.getText().toString().trim());

if (minWidth > UCrop.MIN_SIZE && minHeight > UCrop.MIN_SIZE) {
uCrop = uCrop.withMinResultSize(minWidth, minHeight);
}
} catch (NumberFormatException e) {
Log.e(TAG, "Number please", e);
}
}

if (mCheckBoxMaxSize.isChecked()) {
try {
int maxWidth = Integer.valueOf(mEditTextMaxWidth.getText().toString().trim());
int maxHeight = Integer.valueOf(mEditTextMaxHeight.getText().toString().trim());

if (maxWidth > UCrop.MIN_SIZE && maxHeight > UCrop.MIN_SIZE) {
uCrop = uCrop.withMaxResultSize(maxWidth, maxHeight);
}
Expand Down
51 changes: 51 additions & 0 deletions sample/src/main/res/layout/include_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,57 @@

</RadioGroup>

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="5dp"
android:background="@color/colorAccent" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/label_min_cropped_image_size"
android:textAppearance="?android:textAppearanceSmall" />

<CheckBox
android:id="@+id/checkbox_min_size"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/label_resize_image_to_min_size"
android:textAppearance="?android:textAppearanceMedium" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<EditText
android:id="@+id/edit_text_min_width"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:hint="@string/label_width"
android:inputType="number" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x"
tools:ignore="HardcodedText" />

<EditText
android:id="@+id/edit_text_min_height"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:hint="@string/label_height"
android:inputType="number" />

</LinearLayout>

<View
android:layout_width="match_parent"
android:layout_height="1dp"
Expand Down
3 changes: 3 additions & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
<string name="label_dynamic">Dynamic</string>
<string name="label_image_source">Image source</string>
<string name="label_square">Square</string>
<string name="label_min_cropped_image_size">Min cropped image size</string>
<string name="label_max_cropped_image_size">Max cropped image size</string>
<string name="label_compression_settings">Compression settings</string>
<string name="label_ui">UI</string>
<string name="label_width">Width</string>
<string name="label_height">Height</string>
<string name="label_resize_image_to_min_size">Resize image to min size</string>
<string name="label_resize_image_to_max_size">Resize image to max size</string>
<string name="label_hide_bottom_ui_controls">Hide bottom UI controls</string>
<string name="label_freestyle_crop">Freestyle crop</string>
Expand All @@ -32,6 +34,7 @@
<string name="format_crop_result_d_d">Crop Result (%1$dx%2$d)</string>
<string name="format_quality_d">Quality: %d</string>
<string name="format_d_d_px">%1$dx%2$d px</string>
<string name="format_min_cropped_image_size">Min cropped image size cannot be less then %d</string>
<string name="format_max_cropped_image_size">Max cropped image size cannot be less then %d</string>

<string name="permission_title_rationale">Permission needed</string>
Expand Down
45 changes: 45 additions & 0 deletions ucrop/src/main/java/com/yalantis/ucrop/UCrop.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class UCrop {
public static final String EXTRA_ASPECT_RATIO_X = EXTRA_PREFIX + ".AspectRatioX";
public static final String EXTRA_ASPECT_RATIO_Y = EXTRA_PREFIX + ".AspectRatioY";

public static final String EXTRA_MIN_SIZE_X = EXTRA_PREFIX + ".MinSizeX";
public static final String EXTRA_MIN_SIZE_Y = EXTRA_PREFIX + ".MinSizeY";
public static final String EXTRA_MAX_SIZE_X = EXTRA_PREFIX + ".MaxSizeX";
public static final String EXTRA_MAX_SIZE_Y = EXTRA_PREFIX + ".MaxSizeY";

Expand Down Expand Up @@ -94,8 +96,30 @@ public UCrop useSourceImageAspectRatio() {
return this;
}

/**
* Set minimum size for result cropped image. Minimum size cannot be less then {@value MIN_SIZE}
* The priority of this method is less than the {@link #withMaxResultSize(int, int) withMaxResultSize} method.
*
* @param width min cropped image width
* @param height min cropped image height
*/
public UCrop withMinResultSize(@IntRange(from = MIN_SIZE) int width, @IntRange(from = MIN_SIZE) int height) {
if (width < MIN_SIZE) {
width = MIN_SIZE;
}

if (height < MIN_SIZE) {
height = MIN_SIZE;
}

mCropOptionsBundle.putInt(EXTRA_MIN_SIZE_X, width);
mCropOptionsBundle.putInt(EXTRA_MIN_SIZE_Y, height);
return this;
}

/**
* Set maximum size for result cropped image. Maximum size cannot be less then {@value MIN_SIZE}
* The priority of this method is higher than the {@link #withMinResultSize(int, int) withMinResultSize} method.
*
* @param width max cropped image width
* @param height max cropped image height
Expand Down Expand Up @@ -545,8 +569,29 @@ public void useSourceImageAspectRatio() {
mOptionBundle.putFloat(EXTRA_ASPECT_RATIO_Y, 0);
}

/**
* Set minimum size for result cropped image.
* The priority of this method is less than the {@link #withMaxResultSize(int, int) withMaxResultSize} method.
*
* @param width min cropped image width
* @param height min cropped image height
*/
public void withMinResultSize(@IntRange(from = MIN_SIZE) int width, @IntRange(from = MIN_SIZE) int height) {
if (width < MIN_SIZE) {
width = MIN_SIZE;
}

if (height < MIN_SIZE) {
height = MIN_SIZE;
}

mOptionBundle.putInt(EXTRA_MIN_SIZE_X, width);
mOptionBundle.putInt(EXTRA_MIN_SIZE_Y, height);
}

/**
* Set maximum size for result cropped image.
* The priority of this method is higher than the {@link #withMinResultSize(int, int) withMinResultSize} method.
*
* @param width max cropped image width
* @param height max cropped image height
Expand Down
9 changes: 9 additions & 0 deletions ucrop/src/main/java/com/yalantis/ucrop/UCropActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,19 @@ private void processOptions(@NonNull Intent intent) {
mGestureCropImageView.setTargetAspectRatio(CropImageView.SOURCE_IMAGE_ASPECT_RATIO);
}

// Result bitmap min size options
int minSizeX = intent.getIntExtra(UCrop.EXTRA_MIN_SIZE_X, 0);
int minSizeY = intent.getIntExtra(UCrop.EXTRA_MIN_SIZE_Y, 0);

// Result bitmap max size options
int maxSizeX = intent.getIntExtra(UCrop.EXTRA_MAX_SIZE_X, 0);
int maxSizeY = intent.getIntExtra(UCrop.EXTRA_MAX_SIZE_Y, 0);

if (minSizeX > 0 && minSizeY > 0) {
mGestureCropImageView.setMinResultImageSizeX(minSizeX);
mGestureCropImageView.setMinResultImageSizeY(minSizeY);
}

if (maxSizeX > 0 && maxSizeY > 0) {
mGestureCropImageView.setMaxResultImageSizeX(maxSizeX);
mGestureCropImageView.setMaxResultImageSizeY(maxSizeY);
Expand Down
9 changes: 9 additions & 0 deletions ucrop/src/main/java/com/yalantis/ucrop/UCropFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,19 @@ private void processOptions(@NonNull Bundle bundle) {
mGestureCropImageView.setTargetAspectRatio(CropImageView.SOURCE_IMAGE_ASPECT_RATIO);
}

// Result bitmap min size options
int minSizeX = bundle.getInt(UCrop.EXTRA_MIN_SIZE_X, 0);
int minSizeY = bundle.getInt(UCrop.EXTRA_MIN_SIZE_Y, 0);

// Result bitmap max size options
int maxSizeX = bundle.getInt(UCrop.EXTRA_MAX_SIZE_X, 0);
int maxSizeY = bundle.getInt(UCrop.EXTRA_MAX_SIZE_Y, 0);

if (minSizeX > 0 && minSizeY > 0) {
mGestureCropImageView.setMinResultImageSizeX(minSizeX);
mGestureCropImageView.setMinResultImageSizeY(minSizeY);
}

if (maxSizeX > 0 && maxSizeY > 0) {
mGestureCropImageView.setMaxResultImageSizeX(maxSizeX);
mGestureCropImageView.setMaxResultImageSizeY(maxSizeY);
Expand Down
15 changes: 13 additions & 2 deletions ucrop/src/main/java/com/yalantis/ucrop/model/CropParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@
*/
public class CropParameters {

private int mMaxResultImageSizeX, mMaxResultImageSizeY;
private int mMinResultImageSizeX, mMinResultImageSizeY, mMaxResultImageSizeX, mMaxResultImageSizeY;

private Bitmap.CompressFormat mCompressFormat;
private int mCompressQuality;
private String mImageInputPath, mImageOutputPath;
private ExifInfo mExifInfo;


public CropParameters(int maxResultImageSizeX, int maxResultImageSizeY,
public CropParameters(int minResultImageSizeX, int minResultImageSizeY,
int maxResultImageSizeX, int maxResultImageSizeY,
Bitmap.CompressFormat compressFormat, int compressQuality,
String imageInputPath, String imageOutputPath, ExifInfo exifInfo) {
mMinResultImageSizeX = minResultImageSizeX;
mMinResultImageSizeY = minResultImageSizeY;
mMaxResultImageSizeX = maxResultImageSizeX;
mMaxResultImageSizeY = maxResultImageSizeY;
mCompressFormat = compressFormat;
Expand All @@ -27,6 +30,14 @@ public CropParameters(int maxResultImageSizeX, int maxResultImageSizeY,
mExifInfo = exifInfo;
}

public int getMinResultImageSizeX() {
return mMinResultImageSizeX;
}

public int getMinResultImageSizeY() {
return mMinResultImageSizeY;
}

public int getMaxResultImageSizeX() {
return mMaxResultImageSizeX;
}
Expand Down
Loading