Skip to content

Commit

Permalink
Added UCrop to gui
Browse files Browse the repository at this point in the history
  • Loading branch information
k3b committed Apr 10, 2019
1 parent 310f843 commit c6bdcca
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 24 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.k3b.android.lossless_jpg_crop">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:name=".MainApp"
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package de.k3b.android.lossless_jpg_crop;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;

/**
* Created by Oleksii Shliama (https://github.com/shliama).
*/
public class BaseActivity extends Activity {

private AlertDialog mAlertDialog;

/**
* Hide alert dialog if any.
*/
@Override
protected void onStop() {
super.onStop();
if (mAlertDialog != null && mAlertDialog.isShowing()) {
mAlertDialog.dismiss();
}
}


/**
* Requests given permission.
* If the permission has been denied previously, a Dialog will prompt the user to grant the
* permission, otherwise it is requested directly.
*/
protected void requestPermission(final String permission, String rationale, final int requestCode) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
showAlertDialog(getString(R.string.permission_title_rationale), rationale,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(BaseActivity.this,
new String[]{permission}, requestCode);
}
}, getString(android.R.string.ok), null, getString(android.R.string.cancel));
} else {
ActivityCompat.requestPermissions(this, new String[]{permission}, requestCode);
}
}

/**
* This method shows dialog with given title & message.
* Also there is an option to pass onClickListener for positive & negative button.
*
* @param title - dialog title
* @param message - dialog message
* @param onPositiveButtonClickListener - listener for positive button
* @param positiveText - positive button text
* @param onNegativeButtonClickListener - listener for negative button
* @param negativeText - negative button text
*/
protected void showAlertDialog(@Nullable String title, @Nullable String message,
@Nullable DialogInterface.OnClickListener onPositiveButtonClickListener,
@NonNull String positiveText,
@Nullable DialogInterface.OnClickListener onNegativeButtonClickListener,
@NonNull String negativeText) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title);
builder.setMessage(message);
builder.setPositiveButton(positiveText, onPositiveButtonClickListener);
builder.setNegativeButton(negativeText, onNegativeButtonClickListener);
mAlertDialog = builder.show();
}

}
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
package de.k3b.android.lossless_jpg_crop;

import android.Manifest;
import android.app.ActionBar;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v4.app.ActivityCompat;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.Toast;

import com.yalantis.ucrop.UCrop;
import com.yalantis.ucrop.UCropFragment;
import com.yalantis.ucrop.UCropFragmentCallback;
import com.yalantis.ucrop.view.GestureCropImageView;
import com.yalantis.ucrop.view.OverlayView;
import com.yalantis.ucrop.view.UCropView;

import java.io.InputStream;
import java.io.OutputStream;

public class MainActivity extends AppCompatActivity {
public class MainActivity extends BaseActivity {
private static final String TAG = "ResultActivity";
private static final int REQUEST_GET_PICTURE = 1;
protected static final int REQUEST_GET_PICTURE_PERMISSION = 101;


private ImageProcessor mSpectrum;

Expand All @@ -32,13 +36,25 @@ protected void onCreate(Bundle savedInstanceState) {
mSpectrum = new ImageProcessor();

Uri uri = getIntent().getData();
if (uri != null) {

if (uri == null) {
// must be called with image uri
pickFromGallery();
} else {
try {
UCropView uCropView = findViewById(R.id.ucrop);
uCropView.getCropImageView().setImageUri(uri, null);
uCropView.getOverlayView().setShowCropFrame(false);
uCropView.getOverlayView().setShowCropGrid(false);
uCropView.getOverlayView().setDimmedColor(Color.TRANSPARENT);

final GestureCropImageView cropImageView = uCropView.getCropImageView();
cropImageView.setImageUri(uri, null);
cropImageView.setRotateEnabled(false);


final OverlayView cropOverlayView = uCropView.getOverlayView();
cropOverlayView.setShowCropFrame(true);
cropOverlayView.setShowCropGrid(true);
cropOverlayView.setDimmedColor(Color.TRANSPARENT);
cropOverlayView.setFreestyleCropMode(OverlayView.FREESTYLE_CROP_MODE_ENABLE);


/* parameters for UCropActivity/UCropFragment not used here
UCrop.Options options = new UCrop.Options();
Expand All @@ -63,17 +79,69 @@ protected void onCreate(Bundle savedInstanceState) {
BitmapFactory.decodeFile(new File(getIntent().getData().getPath()).getAbsolutePath(), options);
*/

setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
final ActionBar actionBar = getSupportActionBar();
final ActionBar actionBar = this.getActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle(getString(R.string.app_name)); // , options.outWidth, options.outHeight));
}

}

private void pickFromGallery() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
requestPermission(Manifest.permission.READ_EXTERNAL_STORAGE,
getString(R.string.permission_read_storage_rationale),
REQUEST_GET_PICTURE_PERMISSION);
} else {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT)
.setType("image/jpeg")
.addCategory(Intent.CATEGORY_OPENABLE);

startActivityForResult(Intent.createChooser(intent, getString(R.string.label_select_picture)), REQUEST_GET_PICTURE);
}
}
private void onGetPictureResult(int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
final Uri selectedUri = data.getData();
if (selectedUri != null) {
Intent intent = new Intent(Intent.ACTION_VIEW, selectedUri, this, MainActivity.class);
this.startActivity(intent);
finish();
return;
}
}
Toast.makeText(this, R.string.toast_cannot_retrieve_selected_image, Toast.LENGTH_SHORT).show();
finish();
return;
}

/**
* Callback received when a permissions request has been completed.
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_GET_PICTURE_PERMISSION:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
pickFromGallery();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_GET_PICTURE) {
onGetPictureResult(resultCode, data);
return;
}
super.onActivityResult(requestCode, resultCode, data);
}



Expand Down
9 changes: 0 additions & 9 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@
tools:context=".MainActivity"
>

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?colorAccent"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="@string/app_name"
app:titleTextColor="@android:color/white"/>

<com.yalantis.ucrop.view.UCropView
android:id="@+id/ucrop"
android:layout_width="match_parent"
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
<resources>
<string name="app_name">Loss-Less-Crop</string>

<string name="label_select_picture">Select Picture</string>

<string name="permission_title_rationale">Permission needed</string>
<string name="permission_read_storage_rationale">Storage read permission is needed to pick files.</string>
<string name="permission_write_storage_rationale">Storage write permission is needed to save the image.</string>

<string name="toast_cannot_retrieve_selected_image">Cannot retrieve selected image</string>
<string name="toast_cannot_retrieve_cropped_image">Cannot retrieve cropped image</string>
<string name="toast_unexpected_error">Unexpected error</string>

</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
Expand Down

0 comments on commit c6bdcca

Please sign in to comment.