android - How can i rotate,zoom and scale multiple Imageview on camera surfaceview -


i using http://judepereira.com/blog/multi-touch-in-android-translate-scale-and-rotate/ link code useful imageview,i want multiple imageview @ time

>package logicstree.selfitextcom;  import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.util.arraylist;  import android.app.activity; import android.app.dialog; import android.graphics.bitmap; import android.graphics.bitmap.config; import android.graphics.canvas; import android.graphics.color; import android.graphics.matrix; import android.graphics.paint; import android.graphics.pointf; import android.hardware.camera; import android.hardware.camera.picturecallback; import android.hardware.camera.shuttercallback; import android.os.bundle; import android.text.layout.alignment; import android.text.staticlayout; import android.text.textpaint; import android.util.floatmath; import android.util.log; import android.view.motionevent; import android.view.surfaceholder; import android.view.surfaceview; import android.view.view; import android.view.view.onclicklistener; import android.view.view.ontouchlistener; import android.view.viewgroup; import android.widget.button; import android.widget.edittext; import android.widget.imageview; import android.widget.imageview.scaletype; import android.widget.relativelayout; import android.widget.relativelayout.layoutparams; import android.widget.textview; import android.widget.toast;  import com.capricorn.raymenu;  public class mainactivitysurfaceview extends activity implements         surfaceholder.callback,ontouchlistener {     textview testview;      camera camera;     surfaceview surfaceview;     surfaceholder surfaceholder;      picturecallback rawcallback;     shuttercallback shuttercallback;     picturecallback jpegcallback;      /* drop drag textview */     imageview textimage[] = new imageview[3];     viewgroup _root;     int _xdelta;     int _ydelta;     // textview pinch zoom      final static float step = 200;     float mratio = 1.0f;     int mbasedist;     float mbaseratio;     float fontsize = 13;       private static final string tag = "touch";      private static final float min_zoom = 1f, max_zoom = 1f;     matrix matrix = new matrix();     matrix savedmatrix = new matrix();     static final int none = 0;     static final int drag = 1;     static final int zoom = 2;     int mode = none;      pointf start = new pointf();     pointf mid = new pointf();     float olddist = 1f;        private static final int[] item_drawables = { r.drawable.ic_launcher,             r.drawable.ic_launcher, r.drawable.ic_launcher,             r.drawable.ic_launcher, r.drawable.ic_launcher };        /*new */     public float[] lastevent = null;     private float d = 0f;     private float newrot = 0f;     public static int countertext = 0;     dialog dialogtext;     relativelayout.layoutparams layoutparams;      arraylist<imageview> imagetext;       /** called when activity first created. */     @suppresswarnings("deprecation")     @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          _root = (viewgroup) findviewbyid(r.id.relativcapture);         layoutparams = new relativelayout.layoutparams(layoutparams.fill_parent, layoutparams.fill_parent);         imagetext = new arraylist<imageview>();          surfaceview = (surfaceview) findviewbyid(r.id.surfaceview);         surfaceholder = surfaceview.getholder();          surfaceholder.addcallback(this);          // deprecated setting, required on android versions prior 3.0         surfaceholder.settype(surfaceholder.surface_type_push_buffers);          jpegcallback = new picturecallback() {             public void onpicturetaken(byte[] data, camera camera) {                 fileoutputstream outstream = null;                 try {                     outstream = new fileoutputstream(string.format(                             "/sdcard/%d.jpg", system.currenttimemillis()));                     outstream.write(data);                     outstream.close();                     log.d("log", "onpicturetaken - wrote bytes: " + data.length);                 } catch (filenotfoundexception e) {                     e.printstacktrace();                 } catch (ioexception e) {                     e.printstacktrace();                 } {                 }                 toast.maketext(getapplicationcontext(), "picture saved", 2000)                         .show();                 refreshcamera();             }         };          raymenu raymenu = (raymenu) findviewbyid(r.id.ray_menu);         final int itemcount = item_drawables.length;         (int = 0; < itemcount; i++) {             imageview item = new imageview(this);             item.setimageresource(item_drawables[i]);              final int position = i;             raymenu.additem(item, new onclicklistener() {                  @override                 public void onclick(view v) {                      if(position == 0)                     {                         opendialog();                         toast.maketext(mainactivitysurfaceview.this,"create text ..... :" + position, toast.length_short).show();                     }                     else                     {                         toast.maketext(mainactivitysurfaceview.this,                                 "position:" + position, toast.length_short).show();                     }                 }             });// add menu item         }      }      public void captureimage(view v) throws ioexception {         // take picture         camera.takepicture(null, null, jpegcallback);         countertext=0;     }      @override     public void onbackpressed() {         // todo auto-generated method stub         countertext=0;         super.onbackpressed();     }      public void refreshcamera() {         if (surfaceholder.getsurface() == null) {             // preview surface not exist             return;         }          // stop preview before making changes         try {             camera.stoppreview();         } catch (exception e) {             // ignore: tried stop non-existent preview         }          // set preview size , make resize, rotate or         // reformatting changes here         // start preview new settings         try {             camera.setpreviewdisplay(surfaceholder);             camera.startpreview();         } catch (exception e) {          }     }      public void surfacechanged(surfaceholder holder, int format, int w, int h) {         // size known, set camera parameters , begin         // preview.         refreshcamera();     }      public void surfacecreated(surfaceholder holder) {         try {             // open camera             camera = camera.open();         } catch (runtimeexception e) {             // check exceptions             system.err.println(e);             return;         }         camera.parameters param;         param = camera.getparameters();           // modify parameter         param.setpreviewsize(352, 288);         camera.setparameters(param);         try {             // surface has been created, tell camera draw             // preview.             camera.setpreviewdisplay(surfaceholder);             camera.startpreview();         } catch (exception e) {             // check exceptions             system.err.println(e);             return;         }     }      public void surfacedestroyed(surfaceholder holder) {         // stop preview , release camera         camera.stoppreview();         camera.release();         camera = null;     }       public static bitmap drawtext(string text, int textwidth, int textsize) {         // text dimensions         textpaint textpaint = new textpaint(paint.anti_alias_flag | paint.linear_text_flag);         textpaint.setstyle(paint.style.fill);         textpaint.setcolor(color.white);         textpaint.settextsize(textsize);         staticlayout mtextlayout = new staticlayout(text, textpaint, textwidth,alignment.align_normal, 1.0f, 0.0f, false);          // create bitmap , canvas draw         bitmap b = bitmap.createbitmap(textwidth, mtextlayout.getheight(),config.argb_8888);         canvas c = new canvas(b);            // draw background         paint paint = new paint(paint.anti_alias_flag | paint.linear_text_flag);          paint.setcolor(color.transparent);         c.drawpaint(paint);          // draw text         c.save();         c.translate(0, 0);         mtextlayout.draw(c);         c.restore();          return b;     }       public void opendialog() {          dialogtext = new dialog(mainactivitysurfaceview.this);         dialogtext.setcontentview(r.layout.dialog);         dialogtext.settitle("custom dialog");         // todo auto-generated method stub          toast.maketext(getapplicationcontext(), "test" + countertext++, toast.length_short).show();      /*  for(final imageview image : imagetext)              _root.addview(image);             image.setlayoutparams(layoutparams);             image.setscaletype(scaletype.matrix);             image.setontouchlistener(this);             */            final edittext txtedit = (edittext) dialogtext.findviewbyid(r.id.edittext1);         button btnclick = (button) dialogtext.findviewbyid(r.id.btnclick);          btnclick.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                 string name = txtedit.gettext().tostring();                 int width = getwindowmanager().getdefaultdisplay().getwidth();                  //int size = (int) txtedit.gettextsize() ;                 bitmap bmp = drawtext(name, width, 100);                 imageview inage= new imageview(mainactivitysurfaceview.this);                  inage.setimagebitmap(bmp);                 ++countertext;                 dialogtext.dismiss();                 _root.addview(inage);                 inage.setlayoutparams(layoutparams);                 inage.setscaletype(scaletype.matrix);                 inage.setontouchlistener(mainactivitysurfaceview.this);              }         });         dialogtext.show();       }      @override     public boolean ontouch(view v, motionevent event) {           imageview view = (imageview) v;         switch (event.getaction() & motionevent.action_mask) {             case motionevent.action_down:                 savedmatrix.set(matrix);                 start.set(event.getx(), event.gety());                 mode = drag;                 lastevent = null;                 break;             case motionevent.action_pointer_down:                 olddist = spacing(event);                 if (olddist > 10f) {                     savedmatrix.set(matrix);                     midpoint(mid, event);                     mode = zoom;                 }                 lastevent = new float[4];                 lastevent[0] = event.getx(0);                 lastevent[1] = event.getx(1);                 lastevent[2] = event.gety(0);                 lastevent[3] = event.gety(1);                 d = rotation(event);                 break;             case motionevent.action_up:             case motionevent.action_pointer_up:                 mode = none;                 lastevent = null;                 break;             case motionevent.action_move:                 if (mode == drag) {                     matrix.set(savedmatrix);                     float dx = event.getx() - start.x;                     float dy = event.gety() - start.y;                     matrix.posttranslate(dx, dy);                 } else if (mode == zoom) {                     float newdist = spacing(event);                     if (newdist > 10f) {                         matrix.set(savedmatrix);                         float scale = (newdist / olddist);                         /* if(scale > 1.75f)                             system.out.println("====== scale : " + scale);                         else*/                             matrix.postscale(scale, scale, mid.x, mid.y);                     }                     if (lastevent != null && event.getpointercount() == 2) {                         newrot =  rotation(event);                         float r = newrot - d;                         float[] values = new float[9];                         matrix.getvalues(values);                         float tx = values[2];                         float ty = values[5];                         float sx = values[0];                         float xc = (view.getwidth() / 2) * sx;                         float yc = (view.getheight() / 2) * sx;                         matrix.postrotate(r, mid.x, mid.y);                     }                 }                 break;         }         ontouchevent(event);         view.setimagematrix(matrix);         return true;     }        private float rotation(motionevent event) {         // todo auto-generated method stub                 double delta_x = (event.getx(0) - event.getx(1));                 double delta_y = (event.gety(0) - event.gety(1));                 double radians = math.atan2(delta_y, delta_x);                 return (float) math.todegrees(radians);     }      private float spacing(motionevent event) {         float x = event.getx(0) - event.getx(1);         float y = event.gety(0) - event.gety(1);         return floatmath.sqrt(x * x + y * y);     }      private void midpoint(pointf point, motionevent event) {         float x = event.getx(0) + event.getx(1);         float y = event.gety(0) + event.gety(1);         point.set(x / 2, y / 2);     }      private void dumpevent(motionevent event) {         string names[] = { "down", "up", "move", "cancel", "outside",                 "pointer_down", "pointer_up", "7?", "8?", "9?" };         stringbuilder sb = new stringbuilder();         int action = event.getaction();         int actioncode = action & motionevent.action_mask;         sb.append("event action_").append(names[actioncode]);          if (actioncode == motionevent.action_pointer_down                 || actioncode == motionevent.action_pointer_up) {             sb.append("(pid ").append(                     action >> motionevent.action_pointer_id_shift);             sb.append(")");         }         sb.append("[");         (int = 0; < event.getpointercount(); i++) {             sb.append("#").append(i);             sb.append("(pid ").append(event.getpointerid(i));             sb.append(")=").append((int) event.getx(i));             sb.append(",").append((int) event.gety(i));             if (i + 1 < event.getpointercount())                 sb.append(";");         }         sb.append("]");         log.d("touch events ---------", sb.tostring());     }       @override     public void ondetachedfromwindow() {         // todo auto-generated method stub         system.out.println("===onde");         //bm.recycle();         system.gc();         super.ondetachedfromwindow();     }    } 

activity_main

    <relativelayout         android:id="@+id/relativsurface"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:layout_above="@+id/capture"         android:layout_alignparenttop="true" >          <surfaceview             android:id="@+id/surfaceview"             android:layout_width="match_parent"             android:layout_height="fill_parent"             android:layout_alignparenttop="true"             android:layout_below="@+id/ray_menu"             android:clickable="false" >         </surfaceview>          <com.capricorn.raymenu             android:background="@android:color/black"             android:id="@+id/ray_menu"             android:layout_width="fill_parent"             android:layout_height="60dp"             android:layout_alignparenttop="true"             android:paddingleft="5dp"             android:paddingright="5dp" />         <relativelayout             android:layout_width="fill_parent"             android:layout_height="wrap_content"             android:layout_alignparentbottom="true"             android:layout_below="@+id/ray_menu"             android:id="@+id/relativcapture"             android:layout_centerhorizontal="true" >          </relativelayout>      </relativelayout>      <linearlayout         android:id="@+id/capture"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_alignparentbottom="true"         android:clickable="true"         android:gravity="center"         android:onclick="captureimage"         android:orientation="horizontal" >          <textview             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:gravity="center"             android:padding="10dp"             android:text="capture"             android:textappearance="?android:attr/textappearancelarge" />     </linearlayout>  </relativelayout 

this dialog.xml

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="wrap_content"     android:layout_height="wrap_content" >      <textview         android:id="@+id/textview1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignparentleft="true"         android:layout_alignparenttop="true"         android:layout_marginleft="24dp"         android:layout_margintop="30dp"         android:text="enter text" />      <edittext         android:id="@+id/edittext1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignbaseline="@+id/textview1"         android:layout_alignbottom="@+id/textview1"         android:layout_marginleft="10dp"         android:layout_torightof="@+id/textview1"         android:ems="10" >          <requestfocus />     </edittext>      <button         android:id="@+id/btnclick"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignleft="@+id/edittext1"         android:layout_below="@+id/edittext1"         android:layout_margintop="15dp"         android:text="ok" />  </relativelayout> 

activity.xml

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="fill_parent"     android:layout_height="fill_parent" >      <edittext         android:id="@+id/edittext1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignparenttop="true"         android:layout_centerhorizontal="true"         android:layout_margintop="81dp"         android:background="@null"         android:ems="10" >      </edittext>  </relativelayout> 


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -