Android: Free Croping of Image -


  • i want free style of croping on image...free croping image can gallery or can camera...

    is there solution regarding this?

  • load image gallery or camera on view (using canvas draw image)

     public class someview extends view implements ontouchlistener {     private paint paint;     public static list<point> points;     int dist = 2;     boolean flgpathdraw = true;      point mfirstpoint = null;     boolean bfirstpoint = false;      point mlastpoint = null;      bitmap bitmap = bitmapfactory.decoderesource(getresources(),             r.drawable.gallery_12);     context mcontext;      public someview(context c) {         super(c);          mcontext = c;         setfocusable(true);         setfocusableintouchmode(true);          paint = new paint(paint.anti_alias_flag);         paint.setstyle(paint.style.stroke);         paint.setpatheffect(new dashpatheffect(new float[] { 10, 20 }, 0));         paint.setstrokewidth(5);         paint.setcolor(color.white);          this.setontouchlistener(this);         points = new arraylist<point>();          bfirstpoint = false;     }      public someview(context context, attributeset attrs) {         super(context, attrs);         mcontext = context;         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>();         bfirstpoint = false;      }      public void ondraw(canvas canvas) {         canvas.drawbitmap(bitmap, 0, 0, null);          path path = new path();         boolean first = true;          (int = 0; < points.size(); += 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);     }      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) {              if (bfirstpoint) {                  if (comparepoint(mfirstpoint, point)) {                     // points.add(point);                     points.add(mfirstpoint);                         flgpathdraw = false;                                         showcropdialog();                 } else {                     points.add(point);                 }             } else {                 points.add(point);             }              if (!(bfirstpoint)) {                  mfirstpoint = point;                 bfirstpoint = true;             }         }          invalidate();         log.e("hi  ==>", "size: " + point.x + " " + point.y);          if (event.getaction() == motionevent.action_up) {             log.d("action up*******~~~~~~~>>>>", "called");             mlastpoint = point;             if (flgpathdraw) {                 if (points.size() > 12) {                     if (!comparepoint(mfirstpoint, mlastpoint)) {                         flgpathdraw = false;                         points.add(mfirstpoint);                         showcropdialog();                     }                 }             }         }          return true;     }      private boolean comparepoint(point first, point current) {         int left_range_x = (int) (current.x - 3);         int left_range_y = (int) (current.y - 3);          int right_range_x = (int) (current.x + 3);         int right_range_y = (int) (current.y + 3);          if ((left_range_x < first.x && first.x < right_range_x)                 && (left_range_y < first.y && first.y < right_range_y)) {             if (points.size() < 10) {                 return false;             } else {                 return true;             }         } else {             return false;         }      }      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();     }      private void showcropdialog() {         dialoginterface.onclicklistener dialogclicklistener = new dialoginterface.onclicklistener() {             @override             public void onclick(dialoginterface dialog, int which) {                 intent intent;                 switch (which) {                 case dialoginterface.button_positive:                     // yes button clicked                     // bfirstpoint = false;                      intent = new intent(mcontext, cropactivity.class);                     intent.putextra("crop", true);                     mcontext.startactivity(intent);                     break;                  case dialoginterface.button_negative:                     // no button clicked                      intent = new intent(mcontext, cropactivity.class);                     intent.putextra("crop", false);                     mcontext.startactivity(intent);                      bfirstpoint = false;                     // resetview();                      break;                 }             }         };          alertdialog.builder builder = new alertdialog.builder(mcontext);         builder.setmessage("do want save crop or non-crop image?")                 .setpositivebutton("crop", dialogclicklistener)                 .setnegativebutton("non-crop", dialogclicklistener).show()                 .setcancelable(false);     }    } 

    class point {

    public float dy;  public float dx; float x, y;  @override public string tostring() {     return x + ", " + y; } } 
  • mainactivity call someview draw/load image

    public class mainactivity_ extends activity {  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);  }  @override protected void onresume() {     super.onresume();     setcontentview(new someview(mainactivity_.this)); }  } 
  • cropactivity : load crop image on bitmap.

    public class cropactivity extends activity {

        imageview compositeimageview;     boolean crop;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.cropview);          bundle extras = getintent().getextras();         if (extras != null) {             crop = extras.getboolean("crop");         }         int widthofscreen = 0;         int heightofscreen = 0;          displaymetrics dm = new displaymetrics();         try {             getwindowmanager().getdefaultdisplay().getmetrics(dm);         } catch (exception ex) {         }         widthofscreen = dm.widthpixels;         heightofscreen = dm.heightpixels;          compositeimageview = (imageview) findviewbyid(r.id.our_imageview);          bitmap bitmap2 = bitmapfactory.decoderesource(getresources(),                 r.drawable.gallery_12);          bitmap resultingimage = bitmap.createbitmap(widthofscreen,                 heightofscreen, bitmap2.getconfig());          canvas canvas = new canvas(resultingimage);         paint paint = new paint();         paint.setantialias(true);          path path = new path();         (int = 0; < someview.points.size(); i++) {             path.lineto(someview.points.get(i).x, someview.points.get(i).y);         }         canvas.drawpath(path, paint);         if (crop) {             paint.setxfermode(new porterduffxfermode(mode.src_in));          } else {             paint.setxfermode(new porterduffxfermode(mode.src_out));         }         canvas.drawbitmap(bitmap2, 0, 0, paint);         compositeimageview.setimagebitmap(resultingimage);     } } 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -