Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

implemanting freehand crop in android

Tags:

android

canvas

i m trying to implement freehand crop in android using canvas. i use drawPath and store it in List and draw it in canvas path drawing ok,

like this

enter image description here

but now i want to make all pixel in that path in side area with this code but i dont no how to do it..

public  Bitmap getBitmapWithTransparentBG(Bitmap srcBitmap) 
    {
        Bitmap result = srcBitmap.copy(Bitmap.Config.ARGB_8888, true);
        int nWidth = result.getWidth();
        int nHeight = result.getHeight();
        for (int y = 0; y < nHeight; ++y)
        {
          for (int x = 0; x < nWidth; ++x) 
          {
             for (int i = 0; i < points.size() ; i++) 
             {

             }
              result.setPixel(x, y, Color.TRANSPARENT);
          }
        }
        return result;
    }

points is list of path coordinate hear is code for draw path

package com.org;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class SomeView extends View implements OnTouchListener {

    private Paint paint;
    List<Point> points;
    int DIST = 2;
    boolean flgPathDraw = true;

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.waterlilies);

    public SomeView(Context c  ) {
        super(c);
        setFocusable(true);
        setFocusableInTouchMode(true);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
        paint.setColor(Color.WHITE);

        this.setOnTouchListener(this);
        points = new ArrayList<Point>();
    }
    public SomeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setFocusable(true);
        setFocusableInTouchMode(true);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
        paint.setColor(Color.WHITE);

        this.setOnTouchListener(this);
        points = new ArrayList<Point>();

    }

    public void onDraw(Canvas canvas) 
    {
        canvas.drawBitmap(bitmap, 0, 0, null);

        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 {
                path.lineTo(point.x, point.y);
            }
        }
        canvas.drawPath(path, paint);
    }

    public boolean onTouch(View view, MotionEvent event) {
        // if(event.getAction() != MotionEvent.ACTION_DOWN)
        // return super.onTouchEvent(event);
        Point point = new Point();
        point.x = (int) event.getX();
        point.y = (int) event.getY();

        if (flgPathDraw) {
            points.add(point);
        }

        invalidate();
        Log.e("Hi  ==>", "Size: " + points.size());

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

        points.add(point);
        invalidate();
    }
    public void resetView()
    {
        points.clear();
        paint.setColor(Color.WHITE);
        paint.setStyle(Style.STROKE);
        flgPathDraw=true;
        invalidate();
    }
}

class Point {
    public float dy;
    public float dx;
    float x, y;

    @Override
    public String toString() {
        return x + ", " + y;
    }
}
like image 599
Youddh Avatar asked Oct 03 '12 09:10

Youddh


2 Answers

Hi i think below link for your exact solution, what u try?

Android: Free Croping of Image

Don't forget to put your vote and feedback here.

like image 100
Sakthivel Appavu Avatar answered Nov 15 '22 00:11

Sakthivel Appavu


Lets look at a bit more complex example:

example

The red point is the point you want to test. You have to find the edges that cross the y coordinate of the red point. In this example 4 edges cross the y coordinate (the blue points).

Now test how much intersections you get on the left side and on the right side of the point you want to check. If there is an odd number of intersections on both sides the point is inside the shape.

update: you can find a more detailed description of this algorithm here

like image 39
micha Avatar answered Nov 14 '22 22:11

micha