无裁剪手选择图像的一部分 [英] crop free hand selected part of image

查看:69
本文介绍了无裁剪手选择图像的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的ANDROID应用程序中添加裁剪功能.我知道画廊中有默认的裁剪功能,但是在其中只能选择矩形或圆形区域.我想用徒手选择图像的任何部分,然后从原始图像中裁剪图像的选定部分.例如,选择完整的人类图片的头部,然后将其裁剪.见下面我想要的.

I wants to add cropping fature in my ANDROID app. I know there are deafault cropping feature available in gallery but in them only rectangular or circular selection of area is possible. I wants to selected any part of image with free hand and them crop the selected part of image from original image. for example selecting head part of an complete human picture and then crop it. See below what i wants.

之前

之后

请帮助我,如果有免费的lib,也请多加指教.

Please help me and also sugest if any free lib is there.

谢谢

推荐答案

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Point;
import android.os.Build.VERSION;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.SimpleOnScaleGestureListener;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import java.util.ArrayList;
import java.util.List;

public class HandsCropView extends View implements OnTouchListener {
    static Bitmap bitmap;
    public static List<Point> points;
    int C_H_Point;
    int C_W_Point;
    int DIST = 2;
    int D_height;
    int D_width;
    boolean NutralButton = false;
    boolean bfirstpoint = false;
    int canvasHeight;
    int canvasWidth;
    boolean flgPathDraw = true;
    Bitmap img;
    int img_height;
    int img_width;
    LayoutParams layoutParams;
    Context mContext;
    private ScaleGestureDetector mScaleDetector;
    private float mScaleFactor = 1.0f;
    Point mfirstpoint = null;
    Point mlastpoint = null;
    private Paint paint;
    Paint tectcolor = new Paint();

    private class ScaleListener extends SimpleOnScaleGestureListener {
        private ScaleListener() {
        }

        public boolean onScale(ScaleGestureDetector detector) {
            HandsCropView.this.mScaleFactor = HandsCropView.this.mScaleFactor * detector.getScaleFactor();
            HandsCropView.this.mScaleFactor = Math.max(0.1f, Math.min(HandsCropView.this.mScaleFactor, 5.0f));
            HandsCropView.this.invalidate();
            return true;
        }
    }

    public HandsCropView(Context c, Bitmap image) {
        super(c);
        bitmap = image;
        this.img_width = bitmap.getWidth();
        this.img_height = bitmap.getHeight();
        System.out.println("img_width" + this.img_width + "img_height" + this.img_height);
        DisplayMetrics metrics1 = getResources().getDisplayMetrics();
        this.D_width = metrics1.widthPixels;
        this.D_height = metrics1.heightPixels;
        if (this.img_width <= this.D_width) {
            this.C_W_Point = this.D_width - this.img_width;
        }
        if (this.img_height <= this.D_height) {
            this.C_H_Point = this.D_height - this.img_height;
        }
        this.mContext = c;
        setFocusable(true);
        setFocusableInTouchMode(true);
        this.paint = new Paint(1);
        this.paint.setStyle(Style.STROKE);
        this.paint.setPathEffect(new DashPathEffect(new float[]{10.0f, 20.0f}, 5.0f));
        this.paint.setStrokeWidth(5.0f);
        this.paint.setColor(-1);
        if (VERSION.SDK_INT >= 15) {
            setLayerType(1, this.paint);
        }
        this.paint.setShadowLayer(5.5f, 6.0f, 6.0f, Integer.MIN_VALUE);
        this.layoutParams = new LayoutParams(bitmap.getWidth(), bitmap.getHeight());
        setOnTouchListener(this);
        points = new ArrayList<>();
        this.bfirstpoint = false;
        this.mScaleDetector = new ScaleGestureDetector(c, new ScaleListener());
    }

    public HandsCropView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        setFocusable(true);
        setFocusableInTouchMode(true);
        this.paint = new Paint(1);
        this.paint.setStyle(Style.STROKE);
        this.paint.setStrokeWidth(2.0f);
        setOnTouchListener(this);
        points = new ArrayList<>();
        this.bfirstpoint = false;
    }

    public void onDraw(Canvas canvas) {
        canvas.scale(this.mScaleFactor, this.mScaleFactor);
        canvas.drawBitmap(bitmap, 0.0f, 0.0f, null);
        Path path = new Path();
        boolean first = true;
        for (int i = 0; i < points.size(); i += 2) {
            Point point = (Point) points.get(i);
            if (first) {
                first = false;
                path.moveTo((float) point.x, (float) point.y);
            } else if (i < points.size() - 1) {
                Point next = (Point) points.get(i + 1);
                path.quadTo((float) point.x, (float) point.y, (float) next.x, (float) next.y);
            } else {
                this.mlastpoint = (Point) points.get(i);
                path.lineTo((float) point.x, (float) point.y);
            }
        }
        canvas.drawPath(path, this.paint);
    }

    public boolean onTouch(View view, MotionEvent event) {
        Point point = new Point();
        point.x = (int) event.getX();
        point.y = (int) event.getY();
        if (this.flgPathDraw) {
            if (this.bfirstpoint) {
                if (comparepoint(this.mfirstpoint, point)) {
                    points.add(this.mfirstpoint);
                    this.flgPathDraw = false;
                    GetValue();
                } else if (point.x <= this.img_width && point.y <= this.img_height) {
                    points.add(point);
                }
            } else if (point.x <= this.img_width && point.y <= this.img_height) {
                points.add(point);
            }
            if (!this.bfirstpoint) {
                this.mfirstpoint = point;
                this.bfirstpoint = true;
            }
        } else {
            this.mScaleDetector.onTouchEvent(event);
        }
        invalidate();
        Log.e("Hi  ==>", "Size: " + point.x + " " + point.y);
        if (event.getAction() == 1) {
            this.mlastpoint = point;
            if (this.flgPathDraw && points.size() > 12 && !comparepoint(this.mfirstpoint, this.mlastpoint)) {
                this.flgPathDraw = false;
                points.add(this.mfirstpoint);
                GetValue();
            }
        }
        return true;
    }

    private boolean comparepoint(Point first, Point current) {
        int left_range_y = current.y - 3;
        int right_range_x = current.x + 3;
        int right_range_y = current.y + 3;
        if (current.x - 3 >= first.x || first.x >= right_range_x || left_range_y >= first.y || first.y >= right_range_y || points.size() < 10) {
            return false;
        }
        return true;
    }

    public void fillinPartofPath() {
        Point point = new Point();
        point.x = ((Point) points.get(0)).x;
        point.y = ((Point) points.get(0)).y;
        points.add(point);
        invalidate();
    }

    public void resetView() {
        points.clear();
        this.paint.setColor(-1);
        this.paint.setStyle(Style.STROKE);
        this.flgPathDraw = true;
        invalidate();
    }

    public static boolean GetValue() {
        return true;
    }

    public boolean getBooleanValue() {
        return this.NutralButton;
    }
}

这篇关于无裁剪手选择图像的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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