Skip to content
This repository was archived by the owner on Mar 17, 2023. It is now read-only.

Improve add text feature #68

Open
wants to merge 13 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ You can receive the new processed image path and it's edit status like this-
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == PHOTO_EDITOR_REQUEST_CODE) { // same code you used while starting
String newFilePath = data.getStringExtra(EditImageActivity.OUTPUT_PATH);
boolean isImageEdit = data.getBooleanExtra(EditImageActivity.IMAGE_IS_EDIT, false);
String newFilePath = data.getStringExtra(ImageEditorIntentBuilder.OUTPUT_PATH);
boolean isImageEdit = data.getBooleanExtra(EditImageActivity.IS_IMAGE_EDITED, false);
}
}
```
Expand Down
1 change: 1 addition & 0 deletions ananas/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ dependencies {
kapt "com.github.bumptech.glide:compiler:${glide_version}"
implementation "androidx.core:core-ktx:${core_ktx_version}"
implementation "com.android.support.constraint:constraint-layout:${constraint_layout_version}"
implementation 'com.github.thirstycoda:CarouselPicker:v1.5'
}
repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
Expand All @@ -29,6 +31,8 @@

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;

import iamutkarshtiwari.github.io.ananas.BaseActivity;
import iamutkarshtiwari.github.io.ananas.R;
import iamutkarshtiwari.github.io.ananas.editimage.fragment.AddTextFragment;
Expand All @@ -42,16 +46,13 @@
import iamutkarshtiwari.github.io.ananas.editimage.fragment.crop.CropFragment;
import iamutkarshtiwari.github.io.ananas.editimage.fragment.paint.PaintFragment;
import iamutkarshtiwari.github.io.ananas.editimage.interfaces.OnLoadingDialogListener;
import iamutkarshtiwari.github.io.ananas.editimage.interfaces.OnMainBitmapChangeListener;
import iamutkarshtiwari.github.io.ananas.editimage.utils.BitmapUtils;
import iamutkarshtiwari.github.io.ananas.editimage.utils.PermissionUtils;
import iamutkarshtiwari.github.io.ananas.editimage.view.BrightnessView;
import iamutkarshtiwari.github.io.ananas.editimage.view.CustomPaintView;
import iamutkarshtiwari.github.io.ananas.editimage.view.CustomViewPager;
import iamutkarshtiwari.github.io.ananas.editimage.view.RotateImageView;
import iamutkarshtiwari.github.io.ananas.editimage.view.SaturationView;
import iamutkarshtiwari.github.io.ananas.editimage.view.StickerView;
import iamutkarshtiwari.github.io.ananas.editimage.view.TextStickerView;
import iamutkarshtiwari.github.io.ananas.editimage.view.imagezoom.ImageViewTouch;
import iamutkarshtiwari.github.io.ananas.editimage.view.imagezoom.ImageViewTouchBase;
import iamutkarshtiwari.github.io.ananas.editimage.widget.RedoUndoController;
Expand All @@ -73,19 +74,20 @@ public class EditImageActivity extends BaseActivity implements OnLoadingDialogLi
public static final int MODE_BEAUTY = 7;
public static final int MODE_BRIGHTNESS = 8;
public static final int MODE_SATURATION = 9;
public static HashMap<String, Typeface> fonts;
private static final int PERMISSIONS_REQUEST_CODE = 110;

private final String[] requiredPermissions = new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};

public Uri sourceUri;
public String sourceFilePath;
public String outputFilePath;
public String editorTitle;
public StickerView stickerView;
public CropImageView cropPanel;
public ImageViewTouch mainImage;
public TextStickerView textStickerView;
public int mode = MODE_NONE;
protected boolean isBeenSaved = false;
protected boolean isPortraitForced = false;
Expand All @@ -112,11 +114,13 @@ public class EditImageActivity extends BaseActivity implements OnLoadingDialogLi
private TextView titleView;
private MainMenuFragment mainMenuFragment;
private RedoUndoController redoUndoController;
private OnMainBitmapChangeListener onMainBitmapChangeListener;
private CompositeDisposable compositeDisposable = new CompositeDisposable();

public static void start(Activity activity, Intent intent, int requestCode) {
if (TextUtils.isEmpty(intent.getStringExtra(ImageEditorIntentBuilder.SOURCE_PATH))) {
String sourcePath = intent.getStringExtra(ImageEditorIntentBuilder.SOURCE_PATH);
String sourceUriStr = intent.getStringExtra(ImageEditorIntentBuilder.SOURCE_URI);

if (TextUtils.isEmpty(sourcePath) && TextUtils.isEmpty(sourceUriStr)) {
Toast.makeText(activity, R.string.iamutkarshtiwari_github_io_ananas_not_selected, Toast.LENGTH_SHORT).show();
return;
}
Expand Down Expand Up @@ -151,6 +155,11 @@ private void getData() {
isPortraitForced = getIntent().getBooleanExtra(ImageEditorIntentBuilder.FORCE_PORTRAIT, false);
isSupportActionBarEnabled = getIntent().getBooleanExtra(ImageEditorIntentBuilder.SUPPORT_ACTION_BAR_VISIBILITY, false);

String sourceUriStr = getIntent().getStringExtra(ImageEditorIntentBuilder.SOURCE_URI);
if (!TextUtils.isEmpty(sourceUriStr)) {
sourceUri = Uri.parse(sourceUriStr);
}

sourceFilePath = getIntent().getStringExtra(ImageEditorIntentBuilder.SOURCE_PATH);
outputFilePath = getIntent().getStringExtra(ImageEditorIntentBuilder.OUTPUT_PATH);
editorTitle = getIntent().getStringExtra(ImageEditorIntentBuilder.EDITOR_TITLE);
Expand Down Expand Up @@ -189,10 +198,8 @@ private void initView() {
View backBtn = findViewById(R.id.back_btn);
backBtn.setOnClickListener(v -> onBackPressed());

stickerView = findViewById(R.id.sticker_panel);
cropPanel = findViewById(R.id.crop_panel);
rotatePanel = findViewById(R.id.rotate_panel);
textStickerView = findViewById(R.id.text_sticker_panel);
paintView = findViewById(R.id.custom_paint_view);
brightnessView = findViewById(R.id.brightness_panel);
saturationView = findViewById(R.id.contrast_panel);
Expand All @@ -211,8 +218,7 @@ private void initView() {
beautyFragment = BeautyFragment.newInstance();
brightnessFragment = BrightnessFragment.newInstance();
saturationFragment = SaturationFragment.newInstance();
addTextFragment = AddTextFragment.newInstance();
setOnMainBitmapChangeListener(addTextFragment);
addTextFragment = AddTextFragment.newInstance(fonts);

bottomGallery.setAdapter(bottomGalleryAdapter);

Expand All @@ -228,11 +234,11 @@ private void initView() {
ActivityCompat.requestPermissions(this, requiredPermissions, PERMISSIONS_REQUEST_CODE);
}

loadImageFromFile(sourceFilePath);
}

private void setOnMainBitmapChangeListener(OnMainBitmapChangeListener listener) {
onMainBitmapChangeListener = listener;
if (!TextUtils.isEmpty(sourceFilePath)) {
loadImageFromFile(sourceFilePath);
} else {
loadImageFromUri(sourceUri);
}
}

@Override
Expand Down Expand Up @@ -307,7 +313,9 @@ public void onBackPressed() {
} else {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage(R.string.iamutkarshtiwari_github_io_ananas_exit_without_save)
.setCancelable(false).setPositiveButton(R.string.iamutkarshtiwari_github_io_ananas_confirm, (dialog, id) -> finish()).setNegativeButton(R.string.iamutkarshtiwari_github_io_ananas_cancel, (dialog, id) -> dialog.cancel());
.setCancelable(false)
.setPositiveButton(R.string.iamutkarshtiwari_github_io_ananas_confirm, (dialog, id) -> finish())
.setNegativeButton(R.string.iamutkarshtiwari_github_io_ananas_cancel, (dialog, id) -> dialog.cancel());

AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
Expand All @@ -328,15 +336,12 @@ public void changeMainBitmap(Bitmap newBit, boolean needPushUndoStack) {
mainBitmap = newBit;
mainImage.setImageBitmap(mainBitmap);
mainImage.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);

if (mode == MODE_TEXT) {
onMainBitmapChangeListener.onMainBitmapChange();
}
}
}

protected void onSaveTaskDone() {
Intent returnIntent = new Intent();
returnIntent.putExtra(ImageEditorIntentBuilder.SOURCE_URI, sourceUri.toString());
returnIntent.putExtra(ImageEditorIntentBuilder.SOURCE_PATH, sourceFilePath);
returnIntent.putExtra(ImageEditorIntentBuilder.OUTPUT_PATH, outputFilePath);
returnIntent.putExtra(IS_IMAGE_EDITED, numberOfOperations > 0);
Expand Down Expand Up @@ -377,6 +382,19 @@ private Single<Boolean> saveImage(Bitmap finalBitmap) {
});
}

private void loadImageFromUri(Uri uri) {
compositeDisposable.clear();

Disposable loadImageDisposable = loadImage(uri)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe(subscriber -> loadingDialog.show())
.doFinally(() -> loadingDialog.dismiss())
.subscribe(processedBitmap -> changeMainBitmap(processedBitmap, false), e -> showToast(R.string.iamutkarshtiwari_github_io_ananas_load_error));

compositeDisposable.add(loadImageDisposable);
}

private void loadImageFromFile(String filePath) {
compositeDisposable.clear();

Expand All @@ -395,6 +413,11 @@ private Single<Bitmap> loadImage(String filePath) {
imageHeight));
}

private Single<Bitmap> loadImage(Uri uri) {
return Single.fromCallable(() -> BitmapUtils.decodeSampledBitmap(this, uri,
imageWidth, imageHeight));
}

private void showToast(@StringRes int resId) {
Toast.makeText(this, resId, Toast.LENGTH_SHORT).show();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package iamutkarshtiwari.github.io.ananas.editimage

import android.content.Context
import android.content.Intent
import android.graphics.Typeface
import android.net.Uri

class ImageEditorIntentBuilder @JvmOverloads constructor(private val context: Context,
private val sourcePath: String?,
Expand All @@ -11,6 +13,17 @@ class ImageEditorIntentBuilder @JvmOverloads constructor(private val context: Co
EditImageActivity::class.java
)
) {
private var sourceUri: Uri? = null

@JvmOverloads constructor(context: Context,
sourceUri: Uri,
outputPath: String?,
intent: Intent = Intent(
context,
EditImageActivity::class.java
)) : this(context, null, outputPath, intent) {
this.sourceUri = sourceUri
}

fun withAddText(): ImageEditorIntentBuilder {
intent.putExtra(ADD_TEXT_FEATURE, true)
Expand Down Expand Up @@ -62,8 +75,16 @@ class ImageEditorIntentBuilder @JvmOverloads constructor(private val context: Co
return this
}

fun withSourceUri(sourceUri: Uri): ImageEditorIntentBuilder {
this.sourceUri = sourceUri
intent.putExtra(SOURCE_URI, sourceUri.toString())
intent.removeExtra(SOURCE_PATH)
return this
}

fun withSourcePath(sourcePath: String): ImageEditorIntentBuilder {
intent.putExtra(SOURCE_PATH, sourcePath)
intent.removeExtra(SOURCE_URI)
return this
}

Expand All @@ -72,6 +93,11 @@ class ImageEditorIntentBuilder @JvmOverloads constructor(private val context: Co
return this
}

fun withFonts(fonts: HashMap<String, Typeface>): ImageEditorIntentBuilder {
EditImageActivity.fonts = fonts
return this
}

fun forcePortrait(isForcePortrait: Boolean): ImageEditorIntentBuilder {
intent.putExtra(FORCE_PORTRAIT, isForcePortrait)
return this
Expand All @@ -85,10 +111,14 @@ class ImageEditorIntentBuilder @JvmOverloads constructor(private val context: Co
@Throws(Exception::class)
fun build(): Intent {

if (sourcePath.isNullOrBlank()) {
throw Exception("Output image path required. Use withOutputPath(path) to provide the output image path.")
} else {
if (sourcePath.isNullOrBlank() && sourceUri == null) {
throw Exception("Source image required. Use withSourcePath(path) or withSourceUri(uri) to provide the source.")
} else if (!sourcePath.isNullOrBlank() && sourceUri != null) {
throw Exception("Multiple source images specified. Use either withSourcePath(path) or withSourceUri(uri) to provide the source.")
} else if (!sourcePath.isNullOrBlank()) {
intent.putExtra(SOURCE_PATH, sourcePath)
} else {
intent.putExtra(SOURCE_URI, sourceUri.toString())
}

if (outputPath.isNullOrBlank()) {
Expand All @@ -111,6 +141,7 @@ class ImageEditorIntentBuilder @JvmOverloads constructor(private val context: Co
const val BEAUTY_FEATURE = "beauty_feature"
const val STICKER_FEATURE = "sticker_feature"

const val SOURCE_URI = "source_uri"
const val SOURCE_PATH = "source_path"
const val OUTPUT_PATH = "output_path"
const val FORCE_PORTRAIT = "force_portrait"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private final class ImageClick implements OnClickListener {
public void onClick(View v) {
String data = (String) v.getTag(R.id.iamutkarshtiwari_github_io_ananas_TAG_STICKERS_PATH);
int count = (int) v.getTag(R.id.iamutkarshtiwari_github_io_ananas_TAG_STICKERS_COUNT);
stickerFragment.swipToStickerDetails(data, count);
stickerFragment.swipeToStickerDetails(data, count);
}
}
}

This file was deleted.

This file was deleted.

Loading