diff --git a/android/src/main/java/com/ahmedadeltito/photoeditor/ColorPickerAdapter.java b/android/src/main/java/com/ahmedadeltito/photoeditor/ColorPickerAdapter.java index c0bc4430..8927eca0 100755 --- a/android/src/main/java/com/ahmedadeltito/photoeditor/ColorPickerAdapter.java +++ b/android/src/main/java/com/ahmedadeltito/photoeditor/ColorPickerAdapter.java @@ -9,6 +9,8 @@ import android.graphics.drawable.shapes.OvalShape; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; + +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -22,76 +24,93 @@ public class ColorPickerAdapter extends RecyclerView.Adapter { - private Context context; - private LayoutInflater inflater; - private List colorPickerColors; - private OnColorPickerClickListener onColorPickerClickListener; - - public ColorPickerAdapter(@NonNull Context context, @NonNull List colorPickerColors) { - this.context = context; - this.inflater = LayoutInflater.from(context); - this.colorPickerColors = colorPickerColors; + private Context context; + private LayoutInflater inflater; + private List colorPickerColors; + private OnColorPickerClickListener onColorPickerClickListener; + private int pos; + + public ColorPickerAdapter(@NonNull Context context, @NonNull List colorPickerColors, int initColorCode) { + this.context = context; + this.inflater = LayoutInflater.from(context); + this.colorPickerColors = colorPickerColors; + int position = colorPickerColors.indexOf(initColorCode); + this.pos = position == -1 ? 5 : position; // 5 equals index of red item + } + + @Override + public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + View view = inflater.inflate(R.layout.color_picker_item_list, parent, false); + return new ViewHolder(view); + } + + @Override + public void onBindViewHolder(ViewHolder holder, int position) { + buildColorPickerView(holder.colorPickerView, colorPickerColors.get(position), position); + } + + @Override + public int getItemCount() { + return colorPickerColors.size(); + } + + private void buildColorPickerView(View view, int colorCode, int position) { + view.setVisibility(View.VISIBLE); + int w = 70; + if (pos == position){ + w = 120; } - @Override - public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { - View view = inflater.inflate(R.layout.color_picker_item_list, parent, false); - return new ViewHolder(view); - } - - @Override - public void onBindViewHolder(ViewHolder holder, int position) { - buildColorPickerView(holder.colorPickerView, colorPickerColors.get(position)); - } - - @Override - public int getItemCount() { - return colorPickerColors.size(); - } - - private void buildColorPickerView(View view, int colorCode) { - view.setVisibility(View.VISIBLE); - - ShapeDrawable biggerCircle = new ShapeDrawable(new OvalShape()); - biggerCircle.setIntrinsicHeight(20); - biggerCircle.setIntrinsicWidth(20); - biggerCircle.setBounds(new Rect(0, 0, 20, 20)); - biggerCircle.getPaint().setColor(colorCode); + ShapeDrawable biggerCircle = new ShapeDrawable(new OvalShape()); + biggerCircle.setIntrinsicHeight(20); + biggerCircle.setIntrinsicWidth(20); + biggerCircle.setBounds(new Rect(0, 0, 20, 20)); + biggerCircle.getPaint().setColor(colorCode); + + ShapeDrawable smallerCircle = new ShapeDrawable(new OvalShape()); + smallerCircle.setIntrinsicHeight(5); + smallerCircle.setIntrinsicWidth(5); + smallerCircle.setBounds(new Rect(0, 0, 5, 5)); + smallerCircle.getPaint().setColor(Color.WHITE); + smallerCircle.setPadding(10, 10, 10, 10); + Drawable[] drawables = {smallerCircle, biggerCircle}; + + LayerDrawable layerDrawable = new LayerDrawable(drawables); + + view.setBackground(layerDrawable); + ViewGroup.LayoutParams params = view.getLayoutParams(); + params.width = w; + params.height = w; + view.setLayoutParams(params); + } + + public void setOnColorPickerClickListener(OnColorPickerClickListener onColorPickerClickListener) { + this.onColorPickerClickListener = onColorPickerClickListener; + } + + class ViewHolder extends RecyclerView.ViewHolder { + View colorPickerView; + + public ViewHolder(View itemView) { + super(itemView); + colorPickerView = itemView.findViewById(R.id.color_picker_view); + itemView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (onColorPickerClickListener != null) { + int code = colorPickerColors.get(getAdapterPosition()); + pos = getAdapterPosition(); + onColorPickerClickListener.onColorPickerClickListener(code, pos); + notifyDataSetChanged(); + } - ShapeDrawable smallerCircle = new ShapeDrawable(new OvalShape()); - smallerCircle.setIntrinsicHeight(5); - smallerCircle.setIntrinsicWidth(5); - smallerCircle.setBounds(new Rect(0, 0, 5, 5)); - smallerCircle.getPaint().setColor(Color.WHITE); - smallerCircle.setPadding(10, 10, 10, 10); - Drawable[] drawables = {smallerCircle, biggerCircle}; - - LayerDrawable layerDrawable = new LayerDrawable(drawables); - - view.setBackgroundDrawable(layerDrawable); - } - - public void setOnColorPickerClickListener(OnColorPickerClickListener onColorPickerClickListener) { - this.onColorPickerClickListener = onColorPickerClickListener; - } - - class ViewHolder extends RecyclerView.ViewHolder { - View colorPickerView; - - public ViewHolder(View itemView) { - super(itemView); - colorPickerView = itemView.findViewById(R.id.color_picker_view); - itemView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - if (onColorPickerClickListener != null) - onColorPickerClickListener.onColorPickerClickListener(colorPickerColors.get(getAdapterPosition())); - } - }); } - } + }); - public interface OnColorPickerClickListener { - void onColorPickerClickListener(int colorCode); } + } + + public interface OnColorPickerClickListener { + void onColorPickerClickListener(int colorCode, int position); + } } diff --git a/android/src/main/java/com/ahmedadeltito/photoeditor/PhotoEditorActivity.java b/android/src/main/java/com/ahmedadeltito/photoeditor/PhotoEditorActivity.java index 9bf2eb71..8be81d5b 100755 --- a/android/src/main/java/com/ahmedadeltito/photoeditor/PhotoEditorActivity.java +++ b/android/src/main/java/com/ahmedadeltito/photoeditor/PhotoEditorActivity.java @@ -21,6 +21,7 @@ import android.provider.DocumentsContract; import android.provider.MediaStore; import androidx.annotation.NonNull; +import androidx.annotation.RequiresApi; import androidx.exifinterface.media.ExifInterface; import androidx.core.app.ActivityCompat; import androidx.fragment.app.Fragment; @@ -71,844 +72,849 @@ public class PhotoEditorActivity extends AppCompatActivity implements View.OnClickListener, OnPhotoEditorSDKListener { - public static Typeface emojiFont = null; - - protected static final int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE_GALLERY = 0x1; - final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; - - private final String TAG = "PhotoEditorActivity"; - private RelativeLayout parentImageRelativeLayout; - private RecyclerView drawingViewColorPickerRecyclerView; - private TextView undoTextView, undoTextTextView, doneDrawingTextView, eraseDrawingTextView; - private SlidingUpPanelLayout mLayout; - private View topShadow; - private RelativeLayout topShadowRelativeLayout; - private View bottomShadow; - private RelativeLayout bottomShadowRelativeLayout; - private ArrayList colorPickerColors; - private int colorCodeTextView = -1; - private PhotoEditorSDK photoEditorSDK; - private String selectedImagePath; - private int imageOrientation; - - // CROP OPTION - private boolean cropperCircleOverlay = false; - private boolean freeStyleCropEnabled = false; - private boolean showCropGuidelines = true; - private boolean hideBottomControls = false; - - private ImageView photoEditImageView; + public static Typeface emojiFont = null; + + protected static final int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE_GALLERY = 0x1; + final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; + + private final String TAG = "PhotoEditorActivity"; + private RelativeLayout parentImageRelativeLayout; + private RecyclerView drawingViewColorPickerRecyclerView; + private TextView undoTextView, undoTextTextView, doneDrawingTextView, eraseDrawingTextView; + private SlidingUpPanelLayout mLayout; + private View topShadow; + private RelativeLayout topShadowRelativeLayout; + private View bottomShadow; + private RelativeLayout bottomShadowRelativeLayout; + private ArrayList colorPickerColors; + private int selectedColorCode = -1242561; // red + private PhotoEditorSDK photoEditorSDK; + private String selectedImagePath; + private int imageOrientation; + + // CROP OPTION + private boolean cropperCircleOverlay = false; + private boolean freeStyleCropEnabled = false; + private boolean showCropGuidelines = true; + private boolean hideBottomControls = false; + + private ImageView photoEditImageView; + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_photo_editor); + + getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); + + selectedImagePath = getIntent().getExtras().getString("selectedImagePath"); + if (selectedImagePath.contains("content://")) { + selectedImagePath = getPath(Uri.parse(selectedImagePath)); + } + Log.d("PhotoEditorSDK", "Selected image path: " + selectedImagePath); + + BitmapFactory.Options options = new BitmapFactory.Options(); + options.inSampleSize = 1; + Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath, options); + + Bitmap rotatedBitmap; + try { + ExifInterface exif = new ExifInterface(selectedImagePath); + imageOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED); + rotatedBitmap = rotateBitmap(bitmap, imageOrientation, false); + } catch (IOException e) { + rotatedBitmap = bitmap; + imageOrientation = ExifInterface.ORIENTATION_NORMAL; + + e.printStackTrace(); + } - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_photo_editor); + 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); + drawingViewColorPickerRecyclerView = (RecyclerView) findViewById(R.id.drawing_view_color_picker_recycler_view); + parentImageRelativeLayout = (RelativeLayout) findViewById(R.id.parent_image_rl); + TextView closeTextView = (TextView) findViewById(R.id.close_tv); + TextView addTextView = (TextView) findViewById(R.id.add_text_tv); + TextView addPencil = (TextView) findViewById(R.id.add_pencil_tv); + 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); + undoTextTextView = (TextView) findViewById(R.id.undo_text_tv); + doneDrawingTextView = (TextView) findViewById(R.id.done_drawing_tv); + eraseDrawingTextView = (TextView) findViewById(R.id.erase_drawing_tv); + 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); + 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); + bottomShadow = findViewById(R.id.bottom_shadow); + bottomShadowRelativeLayout = (RelativeLayout) findViewById(R.id.bottom_parent_rl); + + ViewPager pager = (ViewPager) findViewById(R.id.image_emoji_view_pager); + PageIndicator indicator = (PageIndicator) findViewById(R.id.image_emoji_indicator); + + photoEditImageView.setImageBitmap(rotatedBitmap); + + closeTextView.setTypeface(newFont); + addTextView.setTypeface(newFont); + addPencil.setTypeface(newFont); + addImageEmojiTextView.setTypeface(newFont); + addCropTextView.setTypeface(fontAwesome); + saveTextView.setTypeface(newFont); + undoTextView.setTypeface(newFont); + clearAllTextView.setTypeface(newFont); + goToNextTextView.setTypeface(newFont); + deleteTextView.setTypeface(newFont); + + final List fragmentsList = new ArrayList<>(); + + ImageFragment imageFragment = new ImageFragment(); + ArrayList stickers = (ArrayList) getIntent().getExtras().getSerializable("stickers"); + if (stickers != null && stickers.size() > 0) { + Bundle bundle = new Bundle(); + bundle.putSerializable("stickers", stickers); + + imageFragment.setArguments(bundle); + } - getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); + fragmentsList.add(imageFragment); + + EmojiFragment emojiFragment = new EmojiFragment(); + fragmentsList.add(emojiFragment); + + PreviewSlidePagerAdapter adapter = new PreviewSlidePagerAdapter(getSupportFragmentManager(), fragmentsList); + pager.setAdapter(adapter); + pager.setOffscreenPageLimit(5); + indicator.setViewPager(pager); + + photoEditorSDK = new PhotoEditorSDK.PhotoEditorSDKBuilder(PhotoEditorActivity.this) + .parentView(parentImageRelativeLayout) // add parent image view + .childView(photoEditImageView) // add the desired image view + .deleteView(deleteRelativeLayout) // add the deleted view that will appear during the movement of the views + .brushDrawingView(brushDrawingView) // add the brush drawing view that is responsible for drawing on the image view + .buildPhotoEditorSDK(); // build photo editor sdk + photoEditorSDK.setOnPhotoEditorSDKListener(this); + + pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { + + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { + + } + + @Override + public void onPageSelected(int position) { + if (position == 0) + mLayout.setScrollableView(((ImageFragment) fragmentsList.get(position)).imageRecyclerView); + else if (position == 1) + mLayout.setScrollableView(((EmojiFragment) fragmentsList.get(position)).emojiRecyclerView); + } + + @Override + public void onPageScrollStateChanged(int state) { + + } + }); + + closeTextView.setOnClickListener(this); + addImageEmojiTextView.setOnClickListener(this); + addCropTextView.setOnClickListener(this); + addTextView.setOnClickListener(this); + addPencil.setOnClickListener(this); + saveTextView.setOnClickListener(this); + saveTextTextView.setOnClickListener(this); + undoTextView.setOnClickListener(this); + undoTextTextView.setOnClickListener(this); + doneDrawingTextView.setOnClickListener(this); + eraseDrawingTextView.setOnClickListener(this); + clearAllTextView.setOnClickListener(this); + clearAllTextTextView.setOnClickListener(this); + goToNextTextView.setOnClickListener(this); + + ArrayList intentColors = (ArrayList) getIntent().getExtras().getSerializable("colorPickerColors"); + + colorPickerColors = new ArrayList<>(); +// if (intentColors != null) { +// colorPickerColors = intentColors; +// Log.d(TAG, "onCreate: ", intentColors.toString()); +// } else { + colorPickerColors.add(getResources().getColor(R.color.black)); + colorPickerColors.add(getResources().getColor(R.color.blue_color_picker)); + colorPickerColors.add(getResources().getColor(R.color.brown_color_picker)); + colorPickerColors.add(getResources().getColor(R.color.green_color_picker)); + colorPickerColors.add(getResources().getColor(R.color.orange_color_picker)); + colorPickerColors.add(getResources().getColor(R.color.red_color_picker)); + colorPickerColors.add(getResources().getColor(R.color.red_orange_color_picker)); + colorPickerColors.add(getResources().getColor(R.color.sky_blue_color_picker)); + colorPickerColors.add(getResources().getColor(R.color.violet_color_picker)); + colorPickerColors.add(getResources().getColor(R.color.white)); + colorPickerColors.add(getResources().getColor(R.color.yellow_color_picker)); + colorPickerColors.add(getResources().getColor(R.color.yellow_green_color_picker)); +// } + + new CountDownTimer(500, 100) { + + public void onTick(long millisUntilFinished) { + } + + public void onFinish() { + mLayout.setScrollableView(((ImageFragment) fragmentsList.get(0)).imageRecyclerView); + } + + }.start(); + + ArrayList hiddenControls = (ArrayList) getIntent().getExtras().getSerializable("hiddenControls"); + for (int i = 0;i < hiddenControls.size();i++) { + if (hiddenControls.get(i).toString().equalsIgnoreCase("text")) { + addTextView.setVisibility(View.INVISIBLE); + } + if (hiddenControls.get(i).toString().equalsIgnoreCase("clear")) { + clearAllTextView.setVisibility(View.INVISIBLE); + clearAllTextTextView.setVisibility(View.INVISIBLE); + } + if (hiddenControls.get(i).toString().equalsIgnoreCase("draw")) { + addPencil.setVisibility(View.INVISIBLE); + } + if (hiddenControls.get(i).toString().equalsIgnoreCase("save")) { + saveTextTextView.setVisibility(View.INVISIBLE); + saveTextView.setVisibility(View.INVISIBLE); + } + if (hiddenControls.get(i).toString().equalsIgnoreCase("sticker")) { + addImageEmojiTextView.setVisibility(View.INVISIBLE); + } + if (hiddenControls.get(i).toString().equalsIgnoreCase("crop")) { + addCropTextView.setVisibility(View.INVISIBLE); + } + } + } - selectedImagePath = getIntent().getExtras().getString("selectedImagePath"); - if (selectedImagePath.contains("content://")) { - selectedImagePath = getPath(Uri.parse(selectedImagePath)); + private boolean stringIsNotEmpty(String string) { + if (string != null && !string.equals("null")) { + if (!string.trim().equals("")) { + return true; + } + } + return false; + } + + public void addEmoji(String emojiName) { + photoEditorSDK.addEmoji(emojiName, emojiFont); + if (mLayout != null) + mLayout.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED); + } + + public void addImage(Bitmap image) { + photoEditorSDK.addImage(image); + if (mLayout != null) + mLayout.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED); + } + + private void addText(String text, int colorCodeTextView) { + photoEditorSDK.addText(text, colorCodeTextView); + } + + private void clearAllViews() { + photoEditorSDK.clearAllViews(); + } + + private void undoViews() { + photoEditorSDK.viewUndo(); + } + + private void eraseDrawing() { + photoEditorSDK.brushEraser(); + } + + @RequiresApi(api = Build.VERSION_CODES.Q) + private void openAddTextPopupWindow(String text) { + LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); + View addTextPopupWindowRootView = inflater.inflate(R.layout.add_text_popup_window, null); + final EditText addTextEditText = (EditText) addTextPopupWindowRootView.findViewById(R.id.add_text_edit_text); + TextView addTextDoneTextView = (TextView) addTextPopupWindowRootView.findViewById(R.id.add_text_done_tv); + RecyclerView addTextColorPickerRecyclerView = (RecyclerView) addTextPopupWindowRootView.findViewById(R.id.add_text_color_picker_recycler_view); + LinearLayoutManager layoutManager = new LinearLayoutManager(PhotoEditorActivity.this, LinearLayoutManager.HORIZONTAL, false); + addTextColorPickerRecyclerView.setLayoutManager(layoutManager); + addTextColorPickerRecyclerView.setHasFixedSize(true); + ColorPickerAdapter colorPickerAdapter = new ColorPickerAdapter(PhotoEditorActivity.this, colorPickerColors, selectedColorCode); + colorPickerAdapter.setOnColorPickerClickListener(new ColorPickerAdapter.OnColorPickerClickListener() { + @Override + public void onColorPickerClickListener(int iColorCode, int position) { + addTextEditText.setTextColor(iColorCode); + selectedColorCode = iColorCode; + } + }); + addTextColorPickerRecyclerView.setAdapter(colorPickerAdapter); + addTextEditText.setTextColor(selectedColorCode); + if (stringIsNotEmpty(text)) { + addTextEditText.setText(text); + } + final PopupWindow pop = new PopupWindow(PhotoEditorActivity.this); + pop.setContentView(addTextPopupWindowRootView); + pop.setWidth(LinearLayout.LayoutParams.MATCH_PARENT); + pop.setHeight(LinearLayout.LayoutParams.MATCH_PARENT); + pop.setFocusable(true); + pop.setBackgroundDrawable(null); + pop.showAtLocation(addTextPopupWindowRootView, Gravity.TOP, 0, 0); + InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); + addTextDoneTextView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + addText(addTextEditText.getText().toString(), selectedColorCode); + InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + imm.hideSoftInputFromWindow(view.getWindowToken(), 0); + pop.dismiss(); + } + }); + addTextEditText.requestFocus(); + } + + private void updateView(int visibility) { + topShadow.setVisibility(visibility); + topShadowRelativeLayout.setVisibility(visibility); + bottomShadow.setVisibility(visibility); + bottomShadowRelativeLayout.setVisibility(visibility); + } + + private void updateBrushDrawingView(boolean brushDrawingMode) { + photoEditorSDK.setBrushDrawingMode(brushDrawingMode); + if (brushDrawingMode) { + updateView(View.GONE); + drawingViewColorPickerRecyclerView.setVisibility(View.VISIBLE); + doneDrawingTextView.setVisibility(View.VISIBLE); + eraseDrawingTextView.setVisibility(View.VISIBLE); + LinearLayoutManager layoutManager = new LinearLayoutManager(PhotoEditorActivity.this, LinearLayoutManager.HORIZONTAL, false); + drawingViewColorPickerRecyclerView.setLayoutManager(layoutManager); + drawingViewColorPickerRecyclerView.setHasFixedSize(true); + photoEditorSDK.setBrushColor(selectedColorCode); + ColorPickerAdapter colorPickerAdapter = new ColorPickerAdapter(PhotoEditorActivity.this, colorPickerColors, selectedColorCode); + colorPickerAdapter.setOnColorPickerClickListener(new ColorPickerAdapter.OnColorPickerClickListener() { + @Override + public void onColorPickerClickListener(int colorCode, int position) { + photoEditorSDK.setBrushColor(colorCode); + selectedColorCode = colorCode; } - Log.d("PhotoEditorSDK", "Selected image path: " + selectedImagePath); + }); + drawingViewColorPickerRecyclerView.setAdapter(colorPickerAdapter); + } else { + updateView(View.VISIBLE); + drawingViewColorPickerRecyclerView.setVisibility(View.GONE); + doneDrawingTextView.setVisibility(View.GONE); + eraseDrawingTextView.setVisibility(View.GONE); + } + } - BitmapFactory.Options options = new BitmapFactory.Options(); - options.inSampleSize = 1; - Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath, options); + private void returnBackWithSavedImage() { + int permissionCheck = PermissionChecker.checkCallingOrSelfPermission(this, + android.Manifest.permission.WRITE_EXTERNAL_STORAGE); - Bitmap rotatedBitmap; - try { - ExifInterface exif = new ExifInterface(selectedImagePath); - imageOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED); - rotatedBitmap = rotateBitmap(bitmap, imageOrientation, false); - } catch (IOException e) { - rotatedBitmap = bitmap; - imageOrientation = ExifInterface.ORIENTATION_NORMAL; + if (permissionCheck == PackageManager.PERMISSION_GRANTED) { + updateView(View.GONE); + RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( + RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); + layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); + parentImageRelativeLayout.setLayoutParams(layoutParams); + new CountDownTimer(1000, 500) { + public void onTick(long millisUntilFinished) { - e.printStackTrace(); } + public void onFinish() { + String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); + String imageName = "IMG_" + timeStamp + ".jpg"; + Intent returnIntent = new Intent(); - 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); - drawingViewColorPickerRecyclerView = (RecyclerView) findViewById(R.id.drawing_view_color_picker_recycler_view); - parentImageRelativeLayout = (RelativeLayout) findViewById(R.id.parent_image_rl); - TextView closeTextView = (TextView) findViewById(R.id.close_tv); - TextView addTextView = (TextView) findViewById(R.id.add_text_tv); - TextView addPencil = (TextView) findViewById(R.id.add_pencil_tv); - 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); - undoTextTextView = (TextView) findViewById(R.id.undo_text_tv); - doneDrawingTextView = (TextView) findViewById(R.id.done_drawing_tv); - eraseDrawingTextView = (TextView) findViewById(R.id.erase_drawing_tv); - 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); - 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); - bottomShadow = findViewById(R.id.bottom_shadow); - bottomShadowRelativeLayout = (RelativeLayout) findViewById(R.id.bottom_parent_rl); - - ViewPager pager = (ViewPager) findViewById(R.id.image_emoji_view_pager); - PageIndicator indicator = (PageIndicator) findViewById(R.id.image_emoji_indicator); - - photoEditImageView.setImageBitmap(rotatedBitmap); - - closeTextView.setTypeface(newFont); - addTextView.setTypeface(newFont); - addPencil.setTypeface(newFont); - addImageEmojiTextView.setTypeface(newFont); - addCropTextView.setTypeface(fontAwesome); - saveTextView.setTypeface(newFont); - undoTextView.setTypeface(newFont); - clearAllTextView.setTypeface(newFont); - goToNextTextView.setTypeface(newFont); - deleteTextView.setTypeface(newFont); - - final List fragmentsList = new ArrayList<>(); - - ImageFragment imageFragment = new ImageFragment(); - ArrayList stickers = (ArrayList) getIntent().getExtras().getSerializable("stickers"); - if (stickers != null && stickers.size() > 0) { - Bundle bundle = new Bundle(); - bundle.putSerializable("stickers", stickers); - - imageFragment.setArguments(bundle); - } - - fragmentsList.add(imageFragment); - - EmojiFragment emojiFragment = new EmojiFragment(); - fragmentsList.add(emojiFragment); - - PreviewSlidePagerAdapter adapter = new PreviewSlidePagerAdapter(getSupportFragmentManager(), fragmentsList); - pager.setAdapter(adapter); - pager.setOffscreenPageLimit(5); - indicator.setViewPager(pager); - - photoEditorSDK = new PhotoEditorSDK.PhotoEditorSDKBuilder(PhotoEditorActivity.this) - .parentView(parentImageRelativeLayout) // add parent image view - .childView(photoEditImageView) // add the desired image view - .deleteView(deleteRelativeLayout) // add the deleted view that will appear during the movement of the views - .brushDrawingView(brushDrawingView) // add the brush drawing view that is responsible for drawing on the image view - .buildPhotoEditorSDK(); // build photo editor sdk - photoEditorSDK.setOnPhotoEditorSDKListener(this); - - pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { - - @Override - public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { - + if (isSDCARDMounted()) { + String folderName = "PhotoEditorSDK"; + File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), folderName); + if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()) { + Log.d("PhotoEditorSDK", "Failed to create directory"); } - @Override - public void onPageSelected(int position) { - if (position == 0) - mLayout.setScrollableView(((ImageFragment) fragmentsList.get(position)).imageRecyclerView); - else if (position == 1) - mLayout.setScrollableView(((EmojiFragment) fragmentsList.get(position)).emojiRecyclerView); + String selectedOutputPath = mediaStorageDir.getPath() + File.separator + imageName; + returnIntent.putExtra("imagePath", selectedOutputPath); + Log.d("PhotoEditorSDK", "selected camera path " + selectedOutputPath); + File file = new File(selectedOutputPath); + + try { + FileOutputStream out = new FileOutputStream(file); + if (parentImageRelativeLayout != null) { + parentImageRelativeLayout.setDrawingCacheEnabled(true); + + Bitmap bitmap = parentImageRelativeLayout.getDrawingCache(); + Bitmap rotatedBitmap = rotateBitmap(bitmap, imageOrientation, true); + rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out); + } + + out.flush(); + out.close(); + + try { + ExifInterface exifDest = new ExifInterface(file.getAbsolutePath()); + exifDest.setAttribute(ExifInterface.TAG_ORIENTATION, Integer.toString(imageOrientation)); + exifDest.saveAttributes(); + } catch (IOException e) { + e.printStackTrace(); + } + } catch (Exception var7) { + var7.printStackTrace(); } + } - @Override - public void onPageScrollStateChanged(int state) { - - } - }); - - closeTextView.setOnClickListener(this); - addImageEmojiTextView.setOnClickListener(this); - addCropTextView.setOnClickListener(this); - addTextView.setOnClickListener(this); - addPencil.setOnClickListener(this); - saveTextView.setOnClickListener(this); - saveTextTextView.setOnClickListener(this); - undoTextView.setOnClickListener(this); - undoTextTextView.setOnClickListener(this); - doneDrawingTextView.setOnClickListener(this); - eraseDrawingTextView.setOnClickListener(this); - clearAllTextView.setOnClickListener(this); - clearAllTextTextView.setOnClickListener(this); - goToNextTextView.setOnClickListener(this); - - ArrayList intentColors = (ArrayList) getIntent().getExtras().getSerializable("colorPickerColors"); - - colorPickerColors = new ArrayList<>(); - if (intentColors != null) { - colorPickerColors = intentColors; - } else { - colorPickerColors.add(getResources().getColor(R.color.black)); - colorPickerColors.add(getResources().getColor(R.color.blue_color_picker)); - colorPickerColors.add(getResources().getColor(R.color.brown_color_picker)); - colorPickerColors.add(getResources().getColor(R.color.green_color_picker)); - colorPickerColors.add(getResources().getColor(R.color.orange_color_picker)); - colorPickerColors.add(getResources().getColor(R.color.red_color_picker)); - colorPickerColors.add(getResources().getColor(R.color.red_orange_color_picker)); - colorPickerColors.add(getResources().getColor(R.color.sky_blue_color_picker)); - colorPickerColors.add(getResources().getColor(R.color.violet_color_picker)); - colorPickerColors.add(getResources().getColor(R.color.white)); - colorPickerColors.add(getResources().getColor(R.color.yellow_color_picker)); - colorPickerColors.add(getResources().getColor(R.color.yellow_green_color_picker)); + setResult(Activity.RESULT_OK, returnIntent); + finish(); } + }.start(); + Toast.makeText(this, getString(R.string.save_image_succeed), Toast.LENGTH_SHORT).show(); + } else { + showPermissionRequest(); + } + } - new CountDownTimer(500, 100) { + private void returnBackWithUpdateImage() { + updateView(View.GONE); + RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( + RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); + layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); + parentImageRelativeLayout.setLayoutParams(layoutParams); + new CountDownTimer(1000, 500) { + public void onTick(long millisUntilFinished) { - public void onTick(long millisUntilFinished) { - } + } - public void onFinish() { - mLayout.setScrollableView(((ImageFragment) fragmentsList.get(0)).imageRecyclerView); - } + public void onFinish() { + String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); + String imageName = "/IMG_" + timeStamp + ".jpg"; - }.start(); + String selectedImagePath = getIntent().getExtras().getString("selectedImagePath"); + File file = new File(selectedImagePath); +// String newPath = getCacheDir() + imageName; +// File file = new File(newPath); - ArrayList hiddenControls = (ArrayList) getIntent().getExtras().getSerializable("hiddenControls"); - for (int i = 0;i < hiddenControls.size();i++) { - if (hiddenControls.get(i).toString().equalsIgnoreCase("text")) { - addTextView.setVisibility(View.INVISIBLE); - } - if (hiddenControls.get(i).toString().equalsIgnoreCase("clear")) { - clearAllTextView.setVisibility(View.INVISIBLE); - clearAllTextTextView.setVisibility(View.INVISIBLE); - } - if (hiddenControls.get(i).toString().equalsIgnoreCase("draw")) { - addPencil.setVisibility(View.INVISIBLE); - } - if (hiddenControls.get(i).toString().equalsIgnoreCase("save")) { - saveTextTextView.setVisibility(View.INVISIBLE); - saveTextView.setVisibility(View.INVISIBLE); - } - if (hiddenControls.get(i).toString().equalsIgnoreCase("sticker")) { - addImageEmojiTextView.setVisibility(View.INVISIBLE); - } - if (hiddenControls.get(i).toString().equalsIgnoreCase("crop")) { - addCropTextView.setVisibility(View.INVISIBLE); - } + try { + FileOutputStream out = new FileOutputStream(file); + if (parentImageRelativeLayout != null) { + parentImageRelativeLayout.setDrawingCacheEnabled(true); + Bitmap bitmap = parentImageRelativeLayout.getDrawingCache(); + Bitmap rotatedBitmap = rotateBitmap(bitmap, imageOrientation, true); + rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out); + } + + out.flush(); + out.close(); + try { + ExifInterface exifDest = new ExifInterface(file.getAbsolutePath()); + exifDest.setAttribute(ExifInterface.TAG_ORIENTATION, Integer.toString(imageOrientation)); + exifDest.saveAttributes(); + } catch (IOException e) { + e.printStackTrace(); + } + } catch (Exception var7) { + var7.printStackTrace(); } - } - private boolean stringIsNotEmpty(String string) { - if (string != null && !string.equals("null")) { - if (!string.trim().equals("")) { - return true; - } - } - return false; + Intent returnIntent = new Intent(); + returnIntent.putExtra("imagePath", selectedImagePath); + setResult(Activity.RESULT_OK, returnIntent); + + finish(); + } + }.start(); + } + + private boolean isSDCARDMounted() { + String status = Environment.getExternalStorageState(); + return status.equals("mounted"); + } + + public void showPermissionRequest() { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setMessage(getString(R.string.access_media_permissions_msg)); + builder.setPositiveButton(getString(R.string.continue_txt), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + ActivityCompat.requestPermissions(PhotoEditorActivity.this, + new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, + MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE_GALLERY); + } + }); + builder.setNegativeButton(getString(R.string.not_now), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + Toast.makeText(PhotoEditorActivity.this, getString(R.string.media_access_denied_msg), Toast.LENGTH_SHORT).show(); + } + }); + builder.show(); + } + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + if (requestCode == MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE_GALLERY) { + // If request is cancelled, the result arrays are empty. + int permissionCheck = PermissionChecker.checkCallingOrSelfPermission(this, + android.Manifest.permission.WRITE_EXTERNAL_STORAGE); + if (permissionCheck == PackageManager.PERMISSION_GRANTED) { + returnBackWithSavedImage(); + } else { + // permission denied, boo! Disable the + // functionality that depends on this permission. + Toast.makeText(this, getString(R.string.media_access_denied_msg), Toast.LENGTH_SHORT).show(); + } + return; } - - public void addEmoji(String emojiName) { - photoEditorSDK.addEmoji(emojiName, emojiFont); - if (mLayout != null) - mLayout.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED); + } + + @RequiresApi(api = Build.VERSION_CODES.Q) + @Override + public void onClick(View v) { + if (v.getId() == R.id.close_tv) { + 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(""); + } else if (v.getId() == R.id.add_pencil_tv) { + updateBrushDrawingView(true); + } else if (v.getId() == R.id.done_drawing_tv) { + updateBrushDrawingView(false); + } else if (v.getId() == R.id.save_tv || v.getId() == R.id.save_text_tv) { + returnBackWithSavedImage(); + } else if (v.getId() == R.id.clear_all_tv || v.getId() == R.id.clear_all_text_tv) { + clearAllViews(); + } else if (v.getId() == R.id.undo_text_tv || v.getId() == R.id.undo_tv) { + undoViews(); + } else if (v.getId() == R.id.erase_drawing_tv) { + eraseDrawing(); + } else if (v.getId() == R.id.go_to_next_screen_tv) { + returnBackWithUpdateImage(); } - - public void addImage(Bitmap image) { - photoEditorSDK.addImage(image); - if (mLayout != null) - mLayout.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED); + } + + @RequiresApi(api = Build.VERSION_CODES.Q) + @Override + public void onEditTextChangeListener(String text, int colorCode) { + openAddTextPopupWindow(text); + } + + @Override + public void onAddViewListener(ViewType viewType, int numberOfAddedViews) { + if (numberOfAddedViews > 0) { + undoTextView.setVisibility(View.VISIBLE); + undoTextTextView.setVisibility(View.VISIBLE); } - - private void addText(String text, int colorCodeTextView) { - photoEditorSDK.addText(text, colorCodeTextView); + switch (viewType) { + case BRUSH_DRAWING: + Log.i("BRUSH_DRAWING", "onAddViewListener"); + break; + case EMOJI: + Log.i("EMOJI", "onAddViewListener"); + break; + case IMAGE: + Log.i("IMAGE", "onAddViewListener"); + break; + case TEXT: + Log.i("TEXT", "onAddViewListener"); + break; } - - private void clearAllViews() { - photoEditorSDK.clearAllViews(); + } + + @Override + public void onRemoveViewListener(int numberOfAddedViews) { + Log.i(TAG, "onRemoveViewListener"); + if (numberOfAddedViews == 0) { + undoTextView.setVisibility(View.GONE); + undoTextTextView.setVisibility(View.GONE); } - - private void undoViews() { - photoEditorSDK.viewUndo(); + } + + @Override + public void onStartViewChangeListener(ViewType viewType) { + switch (viewType) { + case BRUSH_DRAWING: + Log.i("BRUSH_DRAWING", "onStartViewChangeListener"); + break; + case EMOJI: + Log.i("EMOJI", "onStartViewChangeListener"); + break; + case IMAGE: + Log.i("IMAGE", "onStartViewChangeListener"); + break; + case TEXT: + Log.i("TEXT", "onStartViewChangeListener"); + break; } - - private void eraseDrawing() { - photoEditorSDK.brushEraser(); + } + + @Override + public void onStopViewChangeListener(ViewType viewType) { + switch (viewType) { + case BRUSH_DRAWING: + Log.i("BRUSH_DRAWING", "onStopViewChangeListener"); + break; + case EMOJI: + Log.i("EMOJI", "onStopViewChangeListener"); + break; + case IMAGE: + Log.i("IMAGE", "onStopViewChangeListener"); + break; + case TEXT: + Log.i("TEXT", "onStopViewChangeListener"); + break; } + } - private void openAddTextPopupWindow(String text, int colorCode) { - colorCodeTextView = colorCode; - LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); - View addTextPopupWindowRootView = inflater.inflate(R.layout.add_text_popup_window, null); - final EditText addTextEditText = (EditText) addTextPopupWindowRootView.findViewById(R.id.add_text_edit_text); - TextView addTextDoneTextView = (TextView) addTextPopupWindowRootView.findViewById(R.id.add_text_done_tv); - RecyclerView addTextColorPickerRecyclerView = (RecyclerView) addTextPopupWindowRootView.findViewById(R.id.add_text_color_picker_recycler_view); - LinearLayoutManager layoutManager = new LinearLayoutManager(PhotoEditorActivity.this, LinearLayoutManager.HORIZONTAL, false); - addTextColorPickerRecyclerView.setLayoutManager(layoutManager); - addTextColorPickerRecyclerView.setHasFixedSize(true); - ColorPickerAdapter colorPickerAdapter = new ColorPickerAdapter(PhotoEditorActivity.this, colorPickerColors); - colorPickerAdapter.setOnColorPickerClickListener(new ColorPickerAdapter.OnColorPickerClickListener() { - @Override - public void onColorPickerClickListener(int colorCode) { - addTextEditText.setTextColor(colorCode); - colorCodeTextView = colorCode; - } - }); - addTextColorPickerRecyclerView.setAdapter(colorPickerAdapter); - if (stringIsNotEmpty(text)) { - addTextEditText.setText(text); - addTextEditText.setTextColor(colorCode == -1 ? getResources().getColor(R.color.white) : colorCode); - } - final PopupWindow pop = new PopupWindow(PhotoEditorActivity.this); - pop.setContentView(addTextPopupWindowRootView); - pop.setWidth(LinearLayout.LayoutParams.MATCH_PARENT); - pop.setHeight(LinearLayout.LayoutParams.MATCH_PARENT); - pop.setFocusable(true); - pop.setBackgroundDrawable(null); - pop.showAtLocation(addTextPopupWindowRootView, Gravity.TOP, 0, 0); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - addTextDoneTextView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - addText(addTextEditText.getText().toString(), colorCodeTextView); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - imm.hideSoftInputFromWindow(view.getWindowToken(), 0); - pop.dismiss(); - } - }); - } + private class PreviewSlidePagerAdapter extends FragmentStatePagerAdapter { + private List mFragments; - private void updateView(int visibility) { - topShadow.setVisibility(visibility); - topShadowRelativeLayout.setVisibility(visibility); - bottomShadow.setVisibility(visibility); - bottomShadowRelativeLayout.setVisibility(visibility); + PreviewSlidePagerAdapter(FragmentManager fm, List fragments) { + super(fm); + mFragments = fragments; } - private void updateBrushDrawingView(boolean brushDrawingMode) { - photoEditorSDK.setBrushDrawingMode(brushDrawingMode); - if (brushDrawingMode) { - updateView(View.GONE); - drawingViewColorPickerRecyclerView.setVisibility(View.VISIBLE); - doneDrawingTextView.setVisibility(View.VISIBLE); - eraseDrawingTextView.setVisibility(View.VISIBLE); - LinearLayoutManager layoutManager = new LinearLayoutManager(PhotoEditorActivity.this, LinearLayoutManager.HORIZONTAL, false); - drawingViewColorPickerRecyclerView.setLayoutManager(layoutManager); - drawingViewColorPickerRecyclerView.setHasFixedSize(true); - ColorPickerAdapter colorPickerAdapter = new ColorPickerAdapter(PhotoEditorActivity.this, colorPickerColors); - colorPickerAdapter.setOnColorPickerClickListener(new ColorPickerAdapter.OnColorPickerClickListener() { - @Override - public void onColorPickerClickListener(int colorCode) { - photoEditorSDK.setBrushColor(colorCode); - } - }); - drawingViewColorPickerRecyclerView.setAdapter(colorPickerAdapter); - } else { - updateView(View.VISIBLE); - drawingViewColorPickerRecyclerView.setVisibility(View.GONE); - doneDrawingTextView.setVisibility(View.GONE); - eraseDrawingTextView.setVisibility(View.GONE); - } + @Override + public Fragment getItem(int position) { + if (mFragments == null) { + return (null); + } + return mFragments.get(position); } - private void returnBackWithSavedImage() { - int permissionCheck = PermissionChecker.checkCallingOrSelfPermission(this, - android.Manifest.permission.WRITE_EXTERNAL_STORAGE); - - if (permissionCheck == PackageManager.PERMISSION_GRANTED) { - updateView(View.GONE); - RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( - RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); - layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); - parentImageRelativeLayout.setLayoutParams(layoutParams); - new CountDownTimer(1000, 500) { - public void onTick(long millisUntilFinished) { - - } - - public void onFinish() { - String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); - String imageName = "IMG_" + timeStamp + ".jpg"; - Intent returnIntent = new Intent(); - - if (isSDCARDMounted()) { - String folderName = "PhotoEditorSDK"; - File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), folderName); - if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()) { - Log.d("PhotoEditorSDK", "Failed to create directory"); - } - - String selectedOutputPath = mediaStorageDir.getPath() + File.separator + imageName; - returnIntent.putExtra("imagePath", selectedOutputPath); - Log.d("PhotoEditorSDK", "selected camera path " + selectedOutputPath); - File file = new File(selectedOutputPath); - - try { - FileOutputStream out = new FileOutputStream(file); - if (parentImageRelativeLayout != null) { - parentImageRelativeLayout.setDrawingCacheEnabled(true); - - Bitmap bitmap = parentImageRelativeLayout.getDrawingCache(); - Bitmap rotatedBitmap = rotateBitmap(bitmap, imageOrientation, true); - rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out); - } - - out.flush(); - out.close(); - - try { - ExifInterface exifDest = new ExifInterface(file.getAbsolutePath()); - exifDest.setAttribute(ExifInterface.TAG_ORIENTATION, Integer.toString(imageOrientation)); - exifDest.saveAttributes(); - } catch (IOException e) { - e.printStackTrace(); - } - } catch (Exception var7) { - var7.printStackTrace(); - } - } - - setResult(Activity.RESULT_OK, returnIntent); - finish(); - } - }.start(); - Toast.makeText(this, getString(R.string.save_image_succeed), Toast.LENGTH_SHORT).show(); - } else { - showPermissionRequest(); - } + @Override + public int getCount() { + return 2; + } + } + + private Typeface getFontFromRes(int resource) + { + Typeface tf = null; + InputStream is = null; + try { + is = getResources().openRawResource(resource); + } + catch(Resources.NotFoundException e) { + Log.e(TAG, "Could not find font in resources!"); } + String outPath = getCacheDir() + "/tmp" + System.currentTimeMillis() + ".raw"; - private void returnBackWithUpdateImage() { - updateView(View.GONE); - RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( - RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); - layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); - parentImageRelativeLayout.setLayoutParams(layoutParams); - new CountDownTimer(1000, 500) { - public void onTick(long millisUntilFinished) { - - } + try + { + byte[] buffer = new byte[is.available()]; + BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outPath)); - public void onFinish() { - String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); - String imageName = "/IMG_" + timeStamp + ".jpg"; + int l = 0; + while((l = is.read(buffer)) > 0) + bos.write(buffer, 0, l); - String selectedImagePath = getIntent().getExtras().getString("selectedImagePath"); - File file = new File(selectedImagePath); -// String newPath = getCacheDir() + imageName; -// File file = new File(newPath); + bos.close(); - try { - FileOutputStream out = new FileOutputStream(file); - if (parentImageRelativeLayout != null) { - parentImageRelativeLayout.setDrawingCacheEnabled(true); - Bitmap bitmap = parentImageRelativeLayout.getDrawingCache(); - Bitmap rotatedBitmap = rotateBitmap(bitmap, imageOrientation, true); - rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out); - } - - out.flush(); - out.close(); - try { - ExifInterface exifDest = new ExifInterface(file.getAbsolutePath()); - exifDest.setAttribute(ExifInterface.TAG_ORIENTATION, Integer.toString(imageOrientation)); - exifDest.saveAttributes(); - } catch (IOException e) { - e.printStackTrace(); - } - } catch (Exception var7) { - var7.printStackTrace(); - } - - Intent returnIntent = new Intent(); - returnIntent.putExtra("imagePath", selectedImagePath); - setResult(Activity.RESULT_OK, returnIntent); - - finish(); - } - }.start(); - } + tf = Typeface.createFromFile(outPath); - private boolean isSDCARDMounted() { - String status = Environment.getExternalStorageState(); - return status.equals("mounted"); + // clean up + new File(outPath).delete(); } - - public void showPermissionRequest() { - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setMessage(getString(R.string.access_media_permissions_msg)); - builder.setPositiveButton(getString(R.string.continue_txt), new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - ActivityCompat.requestPermissions(PhotoEditorActivity.this, - new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, - MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE_GALLERY); - } - }); - builder.setNegativeButton(getString(R.string.not_now), new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - Toast.makeText(PhotoEditorActivity.this, getString(R.string.media_access_denied_msg), Toast.LENGTH_SHORT).show(); - } - }); - builder.show(); - } - - @Override - public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { - if (requestCode == MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE_GALLERY) { - // If request is cancelled, the result arrays are empty. - int permissionCheck = PermissionChecker.checkCallingOrSelfPermission(this, - android.Manifest.permission.WRITE_EXTERNAL_STORAGE); - if (permissionCheck == PackageManager.PERMISSION_GRANTED) { - returnBackWithSavedImage(); - } else { - // permission denied, boo! Disable the - // functionality that depends on this permission. - Toast.makeText(this, getString(R.string.media_access_denied_msg), Toast.LENGTH_SHORT).show(); - } - return; - } + catch (IOException e) + { + Log.e(TAG, "Error reading in font!"); + return null; } - @Override - public void onClick(View v) { - if (v.getId() == R.id.close_tv) { - 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) { - updateBrushDrawingView(true); - } else if (v.getId() == R.id.done_drawing_tv) { - updateBrushDrawingView(false); - } else if (v.getId() == R.id.save_tv || v.getId() == R.id.save_text_tv) { - returnBackWithSavedImage(); - } else if (v.getId() == R.id.clear_all_tv || v.getId() == R.id.clear_all_text_tv) { - clearAllViews(); - } else if (v.getId() == R.id.undo_text_tv || v.getId() == R.id.undo_tv) { - undoViews(); - } else if (v.getId() == R.id.erase_drawing_tv) { - eraseDrawing(); - } else if (v.getId() == R.id.go_to_next_screen_tv) { - returnBackWithUpdateImage(); + Log.d(TAG, "Successfully loaded font."); + + 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"); + } } - - @Override - public void onEditTextChangeListener(String text, int colorCode) { - openAddTextPopupWindow(text, colorCode); - } - - @Override - public void onAddViewListener(ViewType viewType, int numberOfAddedViews) { - if (numberOfAddedViews > 0) { - undoTextView.setVisibility(View.VISIBLE); - undoTextTextView.setVisibility(View.VISIBLE); + } + @TargetApi(Build.VERSION_CODES.KITKAT) + protected String getPath(final Uri uri) { + // DocumentProvider + if (isKitKat && DocumentsContract.isDocumentUri(this, uri)) { + // ExternalStorageProvider + if (GalleryUtils.isExternalStorageDocument(uri)) { + final String docId = DocumentsContract.getDocumentId(uri); + final String[] split = docId.split(":"); + final String type = split[0]; + if ("primary".equalsIgnoreCase(type)) { + return Environment.getExternalStorageDirectory() + "/" + + split[1]; } - switch (viewType) { - case BRUSH_DRAWING: - Log.i("BRUSH_DRAWING", "onAddViewListener"); - break; - case EMOJI: - Log.i("EMOJI", "onAddViewListener"); - break; - case IMAGE: - Log.i("IMAGE", "onAddViewListener"); - break; - case TEXT: - Log.i("TEXT", "onAddViewListener"); - break; + } + // DownloadsProvider + else if (GalleryUtils.isDownloadsDocument(uri)) { + final String id = DocumentsContract.getDocumentId(uri); + final Uri contentUri = ContentUris.withAppendedId( + Uri.parse("content://downloads/public_downloads"), + Long.valueOf(id)); + return GalleryUtils.getDataColumn(this, contentUri, null, null); + } + // MediaProvider + else if (GalleryUtils.isMediaDocument(uri)) { + final String docId = DocumentsContract.getDocumentId(uri); + final String[] split = docId.split(":"); + final String type = split[0]; + + Uri contentUri = null; + if ("image".equals(type)) { + contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; + } else if ("video".equals(type)) { + contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; + } else if ("audio".equals(type)) { + contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; } - } - @Override - public void onRemoveViewListener(int numberOfAddedViews) { - Log.i(TAG, "onRemoveViewListener"); - if (numberOfAddedViews == 0) { - undoTextView.setVisibility(View.GONE); - undoTextTextView.setVisibility(View.GONE); - } - } + final String selection = "_id=?"; + final String[] selectionArgs = new String[]{split[1]}; - @Override - public void onStartViewChangeListener(ViewType viewType) { - switch (viewType) { - case BRUSH_DRAWING: - Log.i("BRUSH_DRAWING", "onStartViewChangeListener"); - break; - case EMOJI: - Log.i("EMOJI", "onStartViewChangeListener"); - break; - case IMAGE: - Log.i("IMAGE", "onStartViewChangeListener"); - break; - case TEXT: - Log.i("TEXT", "onStartViewChangeListener"); - break; - } + return GalleryUtils.getDataColumn(this, contentUri, selection, + selectionArgs); + } } - - @Override - public void onStopViewChangeListener(ViewType viewType) { - switch (viewType) { - case BRUSH_DRAWING: - Log.i("BRUSH_DRAWING", "onStopViewChangeListener"); - break; - case EMOJI: - Log.i("EMOJI", "onStopViewChangeListener"); - break; - case IMAGE: - Log.i("IMAGE", "onStopViewChangeListener"); - break; - case TEXT: - Log.i("TEXT", "onStopViewChangeListener"); - break; - } + // MediaStore (and general) + else if ("content".equalsIgnoreCase(uri.getScheme())) { + return GalleryUtils.getDataColumn(this, uri, null, null); } - - private class PreviewSlidePagerAdapter extends FragmentStatePagerAdapter { - private List mFragments; - - PreviewSlidePagerAdapter(FragmentManager fm, List fragments) { - super(fm); - mFragments = fragments; - } - - @Override - public Fragment getItem(int position) { - if (mFragments == null) { - return (null); - } - return mFragments.get(position); - } - - @Override - public int getCount() { - return 2; - } + // File + else if ("file".equalsIgnoreCase(uri.getScheme())) { + return uri.getPath(); } - private Typeface getFontFromRes(int resource) - { - Typeface tf = null; - InputStream is = null; - try { - is = getResources().openRawResource(resource); - } - catch(Resources.NotFoundException e) { - Log.e(TAG, "Could not find font in resources!"); - } + return null; + } - String outPath = getCacheDir() + "/tmp" + System.currentTimeMillis() + ".raw"; + private static Bitmap rotateBitmap(Bitmap bitmap, int orientation, boolean reverse) { + Matrix matrix = new Matrix(); - try - { - byte[] buffer = new byte[is.available()]; - BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outPath)); + switch (orientation) { + case ExifInterface.ORIENTATION_NORMAL: + return bitmap; + case ExifInterface.ORIENTATION_FLIP_HORIZONTAL: + matrix.setScale(-1, 1); - int l = 0; - while((l = is.read(buffer)) > 0) - bos.write(buffer, 0, l); + break; + case ExifInterface.ORIENTATION_ROTATE_180: + matrix.setRotate(180); - bos.close(); + break; + case ExifInterface.ORIENTATION_FLIP_VERTICAL: + matrix.setRotate(180); + matrix.postScale(-1, 1); - tf = Typeface.createFromFile(outPath); - - // clean up - new File(outPath).delete(); - } - catch (IOException e) - { - Log.e(TAG, "Error reading in font!"); - return null; + break; + case ExifInterface.ORIENTATION_TRANSPOSE: + if (!reverse) { + matrix.setRotate(90); + } else { + matrix.setRotate(-90); } - Log.d(TAG, "Successfully loaded font."); - - 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"); - } - } - } - @TargetApi(Build.VERSION_CODES.KITKAT) - protected String getPath(final Uri uri) { - // DocumentProvider - if (isKitKat && DocumentsContract.isDocumentUri(this, uri)) { - // ExternalStorageProvider - if (GalleryUtils.isExternalStorageDocument(uri)) { - final String docId = DocumentsContract.getDocumentId(uri); - final String[] split = docId.split(":"); - final String type = split[0]; - if ("primary".equalsIgnoreCase(type)) { - return Environment.getExternalStorageDirectory() + "/" - + split[1]; - } - } - // DownloadsProvider - else if (GalleryUtils.isDownloadsDocument(uri)) { - final String id = DocumentsContract.getDocumentId(uri); - final Uri contentUri = ContentUris.withAppendedId( - Uri.parse("content://downloads/public_downloads"), - Long.valueOf(id)); - return GalleryUtils.getDataColumn(this, contentUri, null, null); - } - // MediaProvider - else if (GalleryUtils.isMediaDocument(uri)) { - final String docId = DocumentsContract.getDocumentId(uri); - final String[] split = docId.split(":"); - final String type = split[0]; - - Uri contentUri = null; - if ("image".equals(type)) { - contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; - } else if ("video".equals(type)) { - contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; - } else if ("audio".equals(type)) { - contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; - } - - final String selection = "_id=?"; - final String[] selectionArgs = new String[]{split[1]}; - - return GalleryUtils.getDataColumn(this, contentUri, selection, - selectionArgs); - } + matrix.postScale(-1, 1); + break; + case ExifInterface.ORIENTATION_ROTATE_90: + if (!reverse) { + matrix.setRotate(90); + } else { + matrix.setRotate(-90); } - // MediaStore (and general) - else if ("content".equalsIgnoreCase(uri.getScheme())) { - return GalleryUtils.getDataColumn(this, uri, null, null); + + break; + case ExifInterface.ORIENTATION_TRANSVERSE: + if (!reverse) { + matrix.setRotate(-90); + } else { + matrix.setRotate(90); } - // File - else if ("file".equalsIgnoreCase(uri.getScheme())) { - return uri.getPath(); + + matrix.postScale(-1, 1); + break; + case ExifInterface.ORIENTATION_ROTATE_270: + if (!reverse) { + matrix.setRotate(-90); + } else { + matrix.setRotate(90); } - return null; + break; + default: + return bitmap; } - private static Bitmap rotateBitmap(Bitmap bitmap, int orientation, boolean reverse) { - Matrix matrix = new Matrix(); - - switch (orientation) { - case ExifInterface.ORIENTATION_NORMAL: - return bitmap; - case ExifInterface.ORIENTATION_FLIP_HORIZONTAL: - matrix.setScale(-1, 1); - - break; - case ExifInterface.ORIENTATION_ROTATE_180: - matrix.setRotate(180); - - break; - case ExifInterface.ORIENTATION_FLIP_VERTICAL: - matrix.setRotate(180); - matrix.postScale(-1, 1); - - break; - case ExifInterface.ORIENTATION_TRANSPOSE: - if (!reverse) { - matrix.setRotate(90); - } else { - matrix.setRotate(-90); - } - - matrix.postScale(-1, 1); - break; - case ExifInterface.ORIENTATION_ROTATE_90: - if (!reverse) { - matrix.setRotate(90); - } else { - matrix.setRotate(-90); - } - - break; - case ExifInterface.ORIENTATION_TRANSVERSE: - if (!reverse) { - matrix.setRotate(-90); - } else { - matrix.setRotate(90); - } - - matrix.postScale(-1, 1); - break; - case ExifInterface.ORIENTATION_ROTATE_270: - if (!reverse) { - matrix.setRotate(-90); - } else { - matrix.setRotate(90); - } - - break; - default: - return bitmap; - } - - try { - Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); - bitmap.recycle(); + try { + Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); + bitmap.recycle(); - return bmRotated; - } catch (OutOfMemoryError e) { - e.printStackTrace(); - return null; - } + return bmRotated; + } catch (OutOfMemoryError e) { + e.printStackTrace(); + return null; } + } } diff --git a/android/src/main/res/layout/activity_photo_editor.xml b/android/src/main/res/layout/activity_photo_editor.xml index 3114aa2c..8c17002c 100644 --- a/android/src/main/res/layout/activity_photo_editor.xml +++ b/android/src/main/res/layout/activity_photo_editor.xml @@ -84,11 +84,11 @@ android:textSize="20sp" android:visibility="gone" /> - + - + diff --git a/android/src/main/res/layout/color_picker_item_list.xml b/android/src/main/res/layout/color_picker_item_list.xml index 6aefb6af..a28d39cc 100644 --- a/android/src/main/res/layout/color_picker_item_list.xml +++ b/android/src/main/res/layout/color_picker_item_list.xml @@ -1,15 +1,14 @@ - \ No newline at end of file + diff --git a/android/src/main/res/values/strings.xml b/android/src/main/res/values/strings.xml index ac4a0458..93a55798 100644 --- a/android/src/main/res/values/strings.xml +++ b/android/src/main/res/values/strings.xml @@ -21,6 +21,7 @@ "\ue949" "\ue946" "\ue947" + "\ue910" "\ue948" "\ue92c" diff --git a/ios/RNPhotoEditor.h b/ios/RNPhotoEditor.h index 43004e72..762dc8b5 100644 --- a/ios/RNPhotoEditor.h +++ b/ios/RNPhotoEditor.h @@ -1,4 +1,10 @@ + +#if __has_include("RCTBridgeModule.h") +#import "RCTBridgeModule.h" +#else #import +#endif + #import @import iOSPhotoEditor; diff --git a/ios/RNPhotoEditor.podspec b/ios/RNPhotoEditor.podspec index 3d6f712a..af6de253 100644 --- a/ios/RNPhotoEditor.podspec +++ b/ios/RNPhotoEditor.podspec @@ -10,7 +10,7 @@ Pod::Spec.new do |s| s.homepage = package['homepage'] s.license = package['license'] s.author = package['author'] - s.source = { :git => 'https://github.com/prscX/react-native-photo-editor.git', :tag => s.version } + s.source = { :git => 'https://github.com/Kegmil/react-native-photo-editor.git', :tag => s.version } s.platform = :ios, '9.0' s.ios.deployment_target = '8.0' diff --git a/package.json b/package.json index a8fc5e4a..6b7ac5b0 100644 --- a/package.json +++ b/package.json @@ -3,10 +3,10 @@ "version": "1.0.13", "description": "React Native: Native Photo Editor", "main": "index.tsx", - "homepage": "https://github.com/prscX/react-native-photo-editor.git", + "homepage": "https://github.com/Kegmil/react-native-photo-editor.git", "repository": { "type": "git", - "url": "https://github.com/prscX/react-native-photo-editor.git" + "url": "https://github.com/Kegmil/react-native-photo-editor.git" }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1"