Search in sources :

Example 11 with EditorElement

use of org.signal.imageeditor.core.model.EditorElement in project Signal-Android by signalapp.

the class ImageEditorFragment method onViewCreated.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    Mode mode = Mode.getByCode(requireArguments().getString(KEY_MODE));
    imageEditorHud = view.findViewById(R.id.scribble_hud);
    imageEditorView = view.findViewById(R.id.image_editor_view);
    int width = getResources().getDisplayMetrics().widthPixels;
    int height = (int) ((16 / 9f) * width);
    imageEditorView.setMinimumHeight(height);
    imageEditorView.requestLayout();
    imageEditorHud.setBottomOfImageEditorView(getResources().getDisplayMetrics().heightPixels - height);
    imageEditorHud.setEventListener(this);
    imageEditorView.setDragListener(dragListener);
    imageEditorView.setTapListener(selectionListener);
    imageEditorView.setDrawingChangedListener(stillTouching -> onDrawingChanged(stillTouching, true));
    imageEditorView.setUndoRedoStackListener(this::onUndoRedoAvailabilityChanged);
    EditorModel editorModel = null;
    if (restoredModel != null) {
        editorModel = restoredModel;
        restoredModel = null;
    }
    if (editorModel == null) {
        switch(mode) {
            case AVATAR_EDIT:
                editorModel = EditorModel.createForAvatarEdit();
                break;
            case AVATAR_CAPTURE:
                editorModel = EditorModel.createForAvatarCapture();
                break;
            default:
                editorModel = EditorModel.create();
                break;
        }
        EditorElement image = new EditorElement(new UriGlideRenderer(imageUri, true, imageMaxWidth, imageMaxHeight, UriGlideRenderer.STRONG_BLUR, mainImageRequestListener));
        image.getFlags().setSelectable(false).persist();
        editorModel.addElement(image);
    } else {
        controller.onMainImageLoaded();
    }
    if (mode == Mode.AVATAR_CAPTURE || mode == Mode.AVATAR_EDIT) {
        imageEditorHud.setUpForAvatarEditing();
    }
    if (mode == Mode.AVATAR_CAPTURE) {
        imageEditorHud.enterMode(ImageEditorHudV2.Mode.CROP);
    }
    imageEditorView.setModel(editorModel);
    if (!SignalStore.tooltips().hasSeenBlurHudIconTooltip()) {
        imageEditorHud.showBlurHudTooltip();
        SignalStore.tooltips().markBlurHudIconTooltipSeen();
    }
    onDrawingChanged(false, false);
    requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), onBackPressedCallback);
}
Also used : EditorModel(org.signal.imageeditor.core.model.EditorModel) EditorElement(org.signal.imageeditor.core.model.EditorElement) Paint(android.graphics.Paint) Point(android.graphics.Point)

Example 12 with EditorElement

use of org.signal.imageeditor.core.model.EditorElement in project Signal-Android by signalapp.

the class ImageEditorFragment method onBlurFacesToggled.

@Override
public void onBlurFacesToggled(boolean enabled) {
    EditorModel model = imageEditorView.getModel();
    EditorElement mainImage = model.getMainImage();
    if (mainImage == null) {
        imageEditorHud.hideBlurToast();
        return;
    }
    if (!enabled) {
        model.clearFaceRenderers();
        imageEditorHud.hideBlurToast();
        return;
    }
    Matrix inverseCropPosition = model.getInverseCropPosition();
    if (cachedFaceDetection != null) {
        if (cachedFaceDetection.first().equals(getUri()) && cachedFaceDetection.second().position.equals(inverseCropPosition)) {
            renderFaceBlurs(cachedFaceDetection.second());
            imageEditorHud.showBlurToast();
            return;
        } else {
            cachedFaceDetection = null;
        }
    }
    AlertDialog progress = SimpleProgressDialog.show(requireContext());
    mainImage.getFlags().setChildrenVisible(false);
    SimpleTask.run(getLifecycle(), () -> {
        if (mainImage.getRenderer() != null) {
            Bitmap bitmap = ((UriGlideRenderer) mainImage.getRenderer()).getBitmap();
            if (bitmap != null) {
                FaceDetector detector = new AndroidFaceDetector();
                Point size = model.getOutputSizeMaxWidth(1000);
                Bitmap render = model.render(ApplicationDependencies.getApplication(), size);
                try {
                    return new FaceDetectionResult(detector.detect(render), new Point(render.getWidth(), render.getHeight()), inverseCropPosition);
                } finally {
                    render.recycle();
                    mainImage.getFlags().reset();
                }
            }
        }
        return new FaceDetectionResult(Collections.emptyList(), new Point(0, 0), new Matrix());
    }, result -> {
        mainImage.getFlags().reset();
        renderFaceBlurs(result);
        progress.dismiss();
        imageEditorHud.showBlurToast();
    });
}
Also used : AlertDialog(androidx.appcompat.app.AlertDialog) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) EditorModel(org.signal.imageeditor.core.model.EditorModel) EditorElement(org.signal.imageeditor.core.model.EditorElement) Point(android.graphics.Point)

Example 13 with EditorElement

use of org.signal.imageeditor.core.model.EditorElement in project Signal-Android by signalapp.

the class ImageEditorFragment method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && requestCode == SELECT_STICKER_REQUEST_CODE && data != null) {
        final Uri uri = data.getData();
        if (uri != null) {
            UriGlideRenderer renderer = new UriGlideRenderer(uri, true, imageMaxWidth, imageMaxHeight);
            EditorElement element = new EditorElement(renderer, EditorModel.Z_STICKERS);
            imageEditorView.getModel().addElementCentered(element, 0.4f);
            setCurrentSelection(element);
            hasMadeAnEditThisSession = true;
            imageEditorHud.setMode(ImageEditorHudV2.Mode.MOVE_STICKER);
        }
    } else {
        imageEditorHud.setMode(ImageEditorHudV2.Mode.DRAW);
    }
}
Also used : EditorElement(org.signal.imageeditor.core.model.EditorElement) Uri(android.net.Uri)

Example 14 with EditorElement

use of org.signal.imageeditor.core.model.EditorElement in project Signal-Android by signalapp.

the class ImageEditorFragment method addText.

protected void addText() {
    String initialText = "";
    int color = imageEditorHud.getActiveColor();
    MultiLineTextRenderer renderer = new MultiLineTextRenderer(initialText, color, MultiLineTextRenderer.Mode.REGULAR);
    EditorElement element = new EditorElement(renderer, EditorModel.Z_TEXT);
    imageEditorView.getModel().addElementCentered(element, 1);
    imageEditorView.invalidate();
    setCurrentSelection(element);
    startTextEntityEditing(element, true);
}
Also used : MultiLineTextRenderer(org.signal.imageeditor.core.renderers.MultiLineTextRenderer) EditorElement(org.signal.imageeditor.core.model.EditorElement) Paint(android.graphics.Paint) Point(android.graphics.Point)

Example 15 with EditorElement

use of org.signal.imageeditor.core.model.EditorElement in project Signal-Android by signalapp.

the class UriGlideRenderer method renderBlurOverlay.

private void renderBlurOverlay(RendererContext rendererContext) {
    boolean renderMask = false;
    for (EditorElement child : rendererContext.getChildren()) {
        if (child.getZOrder() == EditorModel.Z_MASK) {
            renderMask = true;
            if (blurPaint == null) {
                blurPaint = new Paint();
                blurPaint.setAntiAlias(true);
                blurPaint.setFilterBitmap(true);
                blurPaint.setDither(true);
            }
            blurPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
            rendererContext.setMaskPaint(blurPaint);
            child.draw(rendererContext);
        }
    }
    if (renderMask) {
        rendererContext.save();
        rendererContext.canvasMatrix.concat(imageProjectionMatrix);
        blurPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP));
        blurPaint.setMaskFilter(null);
        if (blurredBitmap == null) {
            blurredBitmap = blur(bitmap, rendererContext.context, blurRadius);
            blurScaleMatrix.setRectToRect(new RectF(0, 0, blurredBitmap.getWidth(), blurredBitmap.getHeight()), new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight()), Matrix.ScaleToFit.FILL);
        }
        rendererContext.canvas.concat(blurScaleMatrix);
        rendererContext.canvas.drawBitmap(blurredBitmap, 0, 0, blurPaint);
        blurPaint.setXfermode(null);
        rendererContext.restore();
    }
}
Also used : RectF(android.graphics.RectF) PorterDuffXfermode(android.graphics.PorterDuffXfermode) EditorElement(org.signal.imageeditor.core.model.EditorElement) Paint(android.graphics.Paint)

Aggregations

EditorElement (org.signal.imageeditor.core.model.EditorElement)24 EditorModel (org.signal.imageeditor.core.model.EditorModel)10 Matrix (android.graphics.Matrix)8 Point (android.graphics.Point)8 MultiLineTextRenderer (org.signal.imageeditor.core.renderers.MultiLineTextRenderer)8 Paint (android.graphics.Paint)6 BezierDrawingRenderer (org.signal.imageeditor.core.renderers.BezierDrawingRenderer)4 FaceBlurRenderer (org.signal.imageeditor.core.renderers.FaceBlurRenderer)4 Bitmap (android.graphics.Bitmap)2 PorterDuffXfermode (android.graphics.PorterDuffXfermode)2 RectF (android.graphics.RectF)2 Uri (android.net.Uri)2 DisplayMetrics (android.util.DisplayMetrics)2 MenuItem (android.view.MenuItem)2 AlertDialog (androidx.appcompat.app.AlertDialog)2 Toolbar (androidx.appcompat.widget.Toolbar)2 UriRenderer (org.signal.imageeditor.app.renderers.UriRenderer)2 UrlRenderer (org.signal.imageeditor.app.renderers.UrlRenderer)2 ColorableRenderer (org.signal.imageeditor.core.ColorableRenderer)2 ImageEditorView (org.signal.imageeditor.core.ImageEditorView)2