裁剪自由手选择图像的一部分

时间:2014-04-07 04:43:32

标签: android image

我想在我的ANDROID应用中添加裁剪功能。我知道画廊中有聋人裁剪功能,但在这些中只能选择矩形或圆形的区域。我想用自由手选择图像的任何部分,然后从原始图像中裁剪图像的选定部分。例如,选择一个完整的人类图片的头部,然后裁剪它。请参阅下文我想要的内容。

enter image description here

AFTER

enter image description here

请帮助我,如果有任何免费的免费资料库,也请提出。

由于

2 个答案:

答案 0 :(得分:0)

这是我曾经使用过的图书馆: Android widget for cropping and rotating an image.

要将Cropper添加到您的应用程序,请在布局XML中指定com.edmodo.cropper.CropImageView

<com.edmodo.cropper.CropImageView 
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="@+id/CropImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

或者您可以通过编程方式修改属性 请参阅此处的WIKI

答案 1 :(得分:0)

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;
    }
}