Skip to content

Commit

Permalink
Merge pull request RZulfikri#1 from RZulfikri/add/crop-android
Browse files Browse the repository at this point in the history
add crop feature for android using same library in react-native-image…
  • Loading branch information
RZulfikri committed Jul 27, 2019
2 parents dcf77d7 + 5d237a7 commit 39bfdfa
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 3 deletions.
1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ dependencies {
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.github.yalantis:ucrop:2.2.2-native'
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
Expand Down Expand Up @@ -51,7 +53,12 @@
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import java.util.List;
import java.util.UUID;

import com.yalantis.ucrop.UCrop;
import com.yalantis.ucrop.UCropActivity;

import ui.photoeditor.R;
public class PhotoEditorActivity extends AppCompatActivity implements View.OnClickListener, OnPhotoEditorSDKListener {
Expand All @@ -73,18 +80,30 @@ public class PhotoEditorActivity extends AppCompatActivity implements View.OnCli
private int colorCodeTextView = -1;
private PhotoEditorSDK photoEditorSDK;

// CROP OPTION
private boolean cropperCircleOverlay = false;
private boolean freeStyleCropEnabled = false;
private boolean showCropGuidelines = true;
private boolean hideBottomControls = false;

String selectedImagePath = "";
ImageView photoEditImageView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo_editor);

String selectedImagePath = getIntent().getExtras().getString("selectedImagePath");
selectedImagePath = getIntent().getExtras().getString("selectedImagePath");

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath, options);

Typeface newFont = getFontFromRes(R.raw.eventtusicons);
Typeface fontAwesome = getFontFromRes(R.raw.font_awesome_solid);

emojiFont = getFontFromRes(R.raw.emojioneandroid);

BrushDrawingView brushDrawingView = (BrushDrawingView) findViewById(R.id.drawing_view);
Expand All @@ -96,6 +115,7 @@ protected void onCreate(Bundle savedInstanceState) {
RelativeLayout deleteRelativeLayout = (RelativeLayout) findViewById(R.id.delete_rl);
TextView deleteTextView = (TextView) findViewById(R.id.delete_tv);
TextView addImageEmojiTextView = (TextView) findViewById(R.id.add_image_emoji_tv);
TextView addCropTextView = (TextView) findViewById(R.id.add_crop_tv);
TextView saveTextView = (TextView) findViewById(R.id.save_tv);
TextView saveTextTextView = (TextView) findViewById(R.id.save_text_tv);
undoTextView = (TextView) findViewById(R.id.undo_tv);
Expand All @@ -105,7 +125,7 @@ protected void onCreate(Bundle savedInstanceState) {
TextView clearAllTextView = (TextView) findViewById(R.id.clear_all_tv);
TextView clearAllTextTextView = (TextView) findViewById(R.id.clear_all_text_tv);
TextView goToNextTextView = (TextView) findViewById(R.id.go_to_next_screen_tv);
ImageView photoEditImageView = (ImageView) findViewById(R.id.photo_edit_iv);
photoEditImageView = (ImageView) findViewById(R.id.photo_edit_iv);
mLayout = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout);
topShadow = findViewById(R.id.top_shadow);
topShadowRelativeLayout = (RelativeLayout) findViewById(R.id.top_parent_rl);
Expand All @@ -121,6 +141,7 @@ protected void onCreate(Bundle savedInstanceState) {
addTextView.setTypeface(newFont);
addPencil.setTypeface(newFont);
addImageEmojiTextView.setTypeface(newFont);
addCropTextView.setTypeface(fontAwesome);
saveTextView.setTypeface(newFont);
undoTextView.setTypeface(newFont);
clearAllTextView.setTypeface(newFont);
Expand Down Expand Up @@ -179,6 +200,7 @@ public void onPageScrollStateChanged(int state) {

closeTextView.setOnClickListener(this);
addImageEmojiTextView.setOnClickListener(this);
addCropTextView.setOnClickListener(this);
addTextView.setOnClickListener(this);
addPencil.setOnClickListener(this);
saveTextView.setOnClickListener(this);
Expand Down Expand Up @@ -248,6 +270,9 @@ public void onFinish() {
if (hiddenControls.get(i).toString().equalsIgnoreCase("sticker")) {
addImageEmojiTextView.setVisibility(View.INVISIBLE);
}
if (hiddenControls.get(i).toString().equalsIgnoreCase("crop")) {
addCropTextView.setVisibility(View.INVISIBLE);
}
}
}

Expand Down Expand Up @@ -511,6 +536,9 @@ public void onClick(View v) {
onBackPressed();
} else if (v.getId() == R.id.add_image_emoji_tv) {
mLayout.setPanelState(SlidingUpPanelLayout.PanelState.EXPANDED);
} else if(v.getId() == R.id.add_crop_tv) {
System.out.println("CROP IMAGE DUD");
startCropping();
} else if (v.getId() == R.id.add_text_tv) {
openAddTextPopupWindow("", -1);
} else if (v.getId() == R.id.add_pencil_tv) {
Expand Down Expand Up @@ -663,4 +691,58 @@ private Typeface getFontFromRes(int resource)

return tf;
}

private void startCropping() {
System.out.println(selectedImagePath);
Uri uri = Uri.fromFile(new File(selectedImagePath));
UCrop.Options options = new UCrop.Options();
options.setCompressionFormat(Bitmap.CompressFormat.JPEG);
options.setCompressionQuality(100);
options.setCircleDimmedLayer(cropperCircleOverlay);
options.setFreeStyleCropEnabled(freeStyleCropEnabled);
options.setShowCropGrid(showCropGuidelines);
options.setHideBottomControls(hideBottomControls);
options.setAllowedGestures(
UCropActivity.ALL, // When 'scale'-tab active
UCropActivity.ALL, // When 'rotate'-tab active
UCropActivity.ALL // When 'aspect ratio'-tab active
);


UCrop uCrop = UCrop
.of(uri, Uri.fromFile(new File(this.getTmpDir(this), UUID.randomUUID().toString() + ".jpg")))
.withOptions(options);

uCrop.start(this);
}


private String getTmpDir(Activity activity) {
String tmpDir = activity.getCacheDir() + "/react-native-photo-editor";
new File(tmpDir).mkdir();

return tmpDir;
}

@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
if (resultCode == RESULT_OK && requestCode == UCrop.REQUEST_CROP) {
if (data != null) {
final Uri resultUri = UCrop.getOutput(data);
if (resultUri != null) {
try {
selectedImagePath = resultUri.toString();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver() , resultUri);
photoEditImageView.setImageBitmap(bitmap);
} catch (Exception ex) {
System.out.println("NO IMAGE DATA FOUND");
}
} else {
System.out.println("NO IMAGE DATA FOUND");
}
} else {
System.out.println("NO RESULT");
}
}
}
}
13 changes: 12 additions & 1 deletion android/src/main/res/layout/activity_photo_editor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,26 @@
android:textSize="20sp" />

<TextView
android:id="@+id/add_image_emoji_tv"
android:id="@+id/add_crop_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="@+id/add_pencil_tv"
android:padding="15dp"
android:text="@string/ic_crop"
android:textColor="#FFFFFF"
android:textSize="20sp" />

<TextView
android:id="@+id/add_image_emoji_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="@+id/add_crop_tv"
android:padding="15dp"
android:text="@string/ic_images"
android:textColor="#FFFFFF"
android:textSize="20sp" />


</RelativeLayout>

<View
Expand Down
Binary file added android/src/main/res/raw/font_awesome_solid.otf
Binary file not shown.
1 change: 1 addition & 0 deletions android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<string name="ic_clear_all">"\ue947"</string>
<string name="ic_continue">"\ue948"</string>
<string name="ic_delete">"\ue92c"</string>
<string name="ic_crop">&#xf565;</string>
<string name="choose_from_gallery">Choose From Gallery</string>
<string name="choose_from_camera">Choose From Camera</string>
<string name="undo">Undo</string>
Expand Down

0 comments on commit 39bfdfa

Please sign in to comment.