徒手图像裁剪在位图区域内绘制 [英] Freehand Image Crop draw inside bitmap region

查看:90
本文介绍了徒手图像裁剪在位图区域内绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试实现图像的徒手裁剪,因此我可以在图像上进行绘制.但是它超出了位图区域.我只想限制用户只能在位图区域内绘制,请检查以下屏幕截图.

Trying to achieve freehand cropping of an image, so I'm able to draw on the image. But it goes outside bitmap region. I just wanna restrict that user can only draw inside bitmap region, check below screen shot.

我正在尝试实现 Photoshop套索工具之类的功能.

I am trying to implement functionality like Photoshop lasso tool.

其图纸在视图区域之外,从而生成错误的输出.

Its drawing outside view region, which generates incorrect output.

输出

代码@

onDraw

public void onDraw(Canvas canvas) {

        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        canvas.drawBitmap(bitmap, rect, rect, null);
        // RectF r = new RectF();
        // Matrix matrix = new Matrix();
        // matrix.mapRect(r);
        // Log.i(TAG, "Rect " + r.left + " " + r.top + " " + r.right + " " +
        // r.bottom + " ");
        // canvas.clipRect(r.left, r.top, r.right, r.bottom);

        Path path = new Path();
        boolean first = true;

        for (int i = 0; i < points.size(); i += 2) {
            Point point = points.get(i);
            if (first) {
                first = false;
                path.moveTo(point.x, point.y);
            } else if (i < points.size() - 1) {
                Point next = points.get(i + 1);
                path.quadTo(point.x, point.y, next.x, next.y);
            } else {
                mlastpoint = points.get(i);
                path.lineTo(point.x, point.y);
            }
        }
        canvas.drawPath(path, paint);
    }

onCrop

Bitmap resultingImage = Bitmap.createBitmap(widthOfscreen,heightOfScreen, bitmap1.getConfig());

        Canvas canvas = new Canvas(resultingImage);

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        Path path = new Path();
        for (int i = 0; i < SomeView.points.size(); i++) {
            path.lineTo(SomeView.points.get(i).x, SomeView.points.get(i).y);
        }
        // path.lineTo(150, 0);
        // path.lineTo(230, 120);
        // path.lineTo(70, 120);
        // path.lineTo(150, 0);

        canvas.drawPath(path, paint);
        if(crop){
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

        }else{
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT));
        }

建议我实现自己的目标.

suggest me to achieve my goal.

推荐答案

为其他答案使用完整的方法,您可以覆盖onmeasure方法并在该方法中设置图像位图的宽度和高度,并调整画布的大小,以便现在您只能绘制在画布中.这样,图像就居中了.绘制路径仅在位图上显示.

late answer but use full for other u can override onmeasure method and set your image bitmap width and height in that method and resize your canvas so now you can draw only in your canvas.so your image come center.it for drawpath only on your bitmap.

 @Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    height = bitmap.getHeight();
    width = bitmap.getWidth();

    setMeasuredDimension(width,height);
}

,然后更改您的oncrop方法,以便您可以轻松地对其进行处理.

and then change in your oncrop method so you can slove it esaly.

Bitmap resultingImage = Bitmap.createBitmap(yourbitmap.getwidth(),yourbitmap.getheight(), yourbitmap.getConfig());

这篇关于徒手图像裁剪在位图区域内绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆