Drawing over /Editing an Image in the Android Emulator

Put your problem here if it does not fit any of the other categories.

Drawing over /Editing an Image in the Android Emulator

Postby vishalmk » Fri Feb 22, 2008 6:37 am

Hi Friends,

I have an Image in my screen (emulator) and I want to draw a square over some area of the image (actually record information for some area of the image ) and store the coordinates (top-left and bottom-right) of the square, so that when the image is opened next time and a mouse event happens over it, the mouse icon changes into a clickable one - over the area of the image recorded earlier.

And also when next time a mouse is dragged over the recorded area it shows a subtitle over the area of the image on the screen for example some name with-which I stored it last time.

Then I want to click that text and open a new activity..

Do you have any suggestions? Would appreciate that.

Thanks,
Vishal
vishalmk
Freshman
Freshman
 
Posts: 6
Joined: Fri Feb 22, 2008 6:21 am

Top

Postby vishalmk » Fri Feb 22, 2008 8:19 am

Any ideas what is the following error I am getting: I am getting this when I use the MotionEvent inside TagPhoto activity (method below). I mean when I click on the image to draw I get this error and the window crashes and returns to its parent (activity screen)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. INFO/WindowManager(508): WIN DEATH: Window{40211030 test.androidintelliimage/test.androidintelliimage.tag.TagPhoto}
  2.  
  3. INFO/ActivityManager(508): APP DEATH: test.androidintelliimage
  4.  
  5. DEBUG/ActivityManager(508): Restarting: HistoryRecord{40095f00 {test.androidintelliimage/test.androidintelliimage.animation.Transition3d}}
  6.  
  7. INFO/WindowManager(508): WIN DEATH: Window{402534a8 test.androidintelliimage/test.androidintelliimage.AndoidIntelliImage}
  8.  
  9. INFO/WindowManager(508): WIN DEATH: Window{40291488 test.androidintelliimage/test.androidintelliimage.animation.Transition3d}
  10.  
  11. INFO/WindowManager(508): WINDOW DIED during motion dispatch: Window{40211030 test.androidintelliimage/test.androidintelliimage.tag.TagPhoto}
  12.  
  13. WARN/WindowManager(508): Requested window android.os.BinderProxy@40210ec8 does not exist
  14.  
  15. WARN/WindowManager(508): java.lang.RuntimeException
  16.  
  17. WARN/WindowManager(508):     at android.server.WindowManagerService.windowForClientLocked(WindowManagerService.java:3881)
  18.  
  19. WARN/WindowManager(508):     at android.server.WindowManagerService.windowForClientLocked(WindowManagerService.java:3873)
  20.  
  21. WARN/WindowManager(508):     at android.server.WindowManagerService.removeWindow(WindowManagerService.java:584)
  22.  
  23. WARN/WindowManager(508):     at android.server.WindowManagerService.dispatchPointer(WindowManagerService.java:1641)
  24.  
  25. WARN/WindowManager(508):     at android.server.WindowManagerService.access$300(WindowManagerService.java:80)
  26.  
  27. WARN/WindowManager(508):     at android.server.WindowManagerService$InputDispatcherThread.process(WindowManagerService.java:2514)
  28.  
  29. WARN/WindowManager(508):     at android.server.WindowManagerService$InputDispatcherThread.run(WindowManagerService.java:2437)
  30.  
  31. INFO/ActivityManager(508): Starting app process test.androidintelliimage to host activity {test.androidintelliimage/test.androidintelliimage.animation.Transition3d}: pid=1080
  32.  
  33. INFO/jdwp(1080): prepping for JDWP over ADB
  34.  
  35. INFO/jdwp(1080): PID sent as '0438' to ADB
  36.  
  37. INFO/jdwp(1080): trying to receive file descriptor from ADB
Parsed in 0.067 seconds, using GeSHi 1.0.8.4

Following is the piece of code I am using
-------------------------------------------------------------
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public boolean onTouchEvent(MotionEvent event) {
  2.  
  3.                 int action = event.getAction();
  4.  
  5.                 mCurDown = action == MotionEvent.ACTION_DOWN ;
  6.  
  7.                 mCurUp = action == MotionEvent.ACTION_UP;
  8.  
  9.                 float x1,y1,x2,y2;
  10.  
  11.                
  12.  
  13.                 x1=y1=x2=y2=0F;
  14.  
  15.                 newCanvas = new Canvas();
  16.  
  17.                
  18.  
  19.                 if(mCurDown){
  20.  
  21.                         x1 = event.getX();
  22.  
  23.                         y1 = event.getY();
  24.  
  25.                 }
  26.  
  27.                
  28.  
  29.                 if(mCurUp){
  30.  
  31.                         x1 = event.getX();
  32.  
  33.                         y1 = event.getY();
  34.  
  35.                 }
  36.  
  37.                 Log.i("x1",String.valueOf(x1));
  38.  
  39.                 Log.i("y1",String.valueOf(y1));
  40.  
  41.                 Log.i("s2",String.valueOf(x2));
  42.  
  43.                 Log.i("y2",String.valueOf(y2));
  44.  
  45.                
  46.  
  47.                 Log.i("Planing Canvas Drawing ",newCanvas.getClass().getName());
  48.  
  49.                 Log.i("Draw Canvas...","");
  50.  
  51.                 mImageView.setDrawingCacheEnabled(true);
  52.  
  53.                 Bitmap bitmap = mImageView.getDrawingCache();
  54.  
  55.                 //newCanvas.setDevice(bitmap);
  56.  
  57.                 newCanvas.drawRect(x1, y1, x2, y2, mPaint);
  58.  
  59.                 mImageView.draw(newCanvas);              
  60.  
  61.                 return true;
  62.  
  63.         }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
vishalmk
Freshman
Freshman
 
Posts: 6
Joined: Fri Feb 22, 2008 6:21 am

Postby plusminus » Fri Feb 22, 2008 1:02 pm

Hello vishalmk,

you error seems to be a native error. Probably with drawing to canvas.
Try uncommenting every line that does sth. with a canvas and check if it runs then.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby vishalmk » Fri Feb 22, 2008 2:32 pm

Thanks for replying..

And true the problem is with canvas - but then how do I draw over an image in my screen and the store information regarding that area.. I actually want to tag an area on the image

Regards,
Vishal
vishalmk
Freshman
Freshman
 
Posts: 6
Joined: Fri Feb 22, 2008 6:21 am

Postby res » Fri Feb 22, 2008 5:29 pm

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. newCanvas.drawRect(x1, y1, x2, y2, mPaint);
  3.  
  4. mImageView.draw(newCanvas);  
  5.  
  6.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Have you setup your canvas object so you can access it outside the native draw method? I would suggest keeping your drawing to the draw method, simply update the co-ordinates in your touch event and maybe set a boolean to true so your image is drawn only after a specific touch event occurred.

edit: I just re-read your code and notice you are setting up a new canvas object. I would be careful trying to create a new canvas outside the onDraw method because the canvas you create and the canvas called by onDraw may be different so you might get some weird symptoms.
res
Senior Developer
Senior Developer
 
Posts: 146
Joined: Tue Nov 20, 2007 8:14 pm
Location: United States

Postby res » Fri Feb 22, 2008 5:41 pm

This is what I would do

Declare the variables for class use
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. float x1 = 0;
  3.  
  4. float x2 = 0;
  5.  
  6. float y1 = 0;
  7.  
  8. float y2 = 0;
  9.  
  10.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public boolean onTouchEvent(MotionEvent event) {
  3.  
  4.           int action = event.getAction();
  5.  
  6.           mCurDown = action == MotionEvent.ACTION_DOWN ;
  7.  
  8.           mCurUp = action == MotionEvent.ACTION_UP;
  9.  
  10.          
  11.  
  12.           if(mCurDown){
  13.  
  14.                x1 = event.getX();
  15.  
  16.                y1 = event.getY();
  17.  
  18.           }
  19.  
  20.          
  21.  
  22.           if(mCurUp){
  23.  
  24.                x1 = event.getX();
  25.  
  26.                y1 = event.getY();
  27.  
  28.           }
  29.  
  30.           Log.i("x1",String.valueOf(x1));
  31.  
  32.           Log.i("y1",String.valueOf(y1));
  33.  
  34.           Log.i("s2",String.valueOf(x2));
  35.  
  36.           Log.i("y2",String.valueOf(y2));
  37.  
  38.          
  39.  
  40.           Log.i("Planing Canvas Drawing ",newCanvas.getClass().getName());
  41.  
  42.           Log.i("Draw Canvas...","");
  43.  
  44.  
  45.  
  46.           return true;
  47.  
  48.         }
  49.  
  50.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


I presume you are using a View so this would be in onDraw()
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. protected void onDraw(Canvas canvas)
  3.  
  4. {
  5.  
  6.     ..... do whatever other drawing you require
  7.  
  8.  
  9.  
  10.     mImageView.setDrawingCacheEnabled(true);
  11.  
  12.     Bitmap bitmap = mImageView.getDrawingCache();
  13.  
  14.    
  15.  
  16.     if (x1 > 0 || x2 > 0 || y1 > 0 || y2 > 0)
  17.  
  18.         canvas.drawRect(x1, y1, x2, y2, mPaint);
  19.  
  20.    
  21.  
  22.     mImageView.draw(canvas);    
  23.  
  24. }
  25.  
  26.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4



Use onTouchEvent and onKeyDown to update variables but leave all the drawing to be done in onDraw and any methods it may call.
res
Senior Developer
Senior Developer
 
Posts: 146
Joined: Tue Nov 20, 2007 8:14 pm
Location: United States

Top

Postby plusminus » Fri Feb 22, 2008 9:38 pm

Yes res, that should work. :)
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Drawing over /Editing an Image in the Android Emulator

Postby vishalmk » Mon Feb 25, 2008 7:28 am

I appreciate your suggestions.Thanks!
I wish to elaborate more on this.

Following is the class i modified my class to.. But there are two things that happen now:

1) My Image is gone and is replaced by a blank black screen. (If I remove the ondraw(canvas) method from this class - I am able to see the image on the screen).
2)Doing this I am able to get the XY coordiates on mouse down and up click(can see them in my logs)- but not able to see anything(Rectangle in my case) getting drawn on the screen (might be because of the blank screen)..

I am in a learning mode of this but getting somewhat lost here.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MyView extends ImageView {
  2.  
  3.        
  4.  
  5.      public MyView(Context c) {
  6.  
  7.             super(c);
  8.  
  9.             mPaint = new Paint();
  10.  
  11.             mPaint.setAntiAlias(true);
  12.  
  13.             mPaint.setARGB(255, 255, 255, 255);
  14.  
  15.         }
  16.  
  17.                
  18.  
  19.         private final Paint mPaint;
  20.  
  21.         private boolean mCurDown, mCurUp;
  22.  
  23.         float x1=0F,y1=0F,x2=0F,y2=0F;
  24.  
  25.    
  26.  
  27.       public boolean onTouchEvent(MotionEvent event) {
  28.  
  29.                 int action = event.getAction();
  30.  
  31.                 mCurDown = action == MotionEvent.ACTION_DOWN ;
  32.  
  33.                 mCurUp = action == MotionEvent.ACTION_UP;
  34.  
  35.        
  36.  
  37.                 if(mCurDown){
  38.  
  39.                         if(!(x1 > 0) && !(y1 > 0)){
  40.  
  41.                                 x1 = event.getX();
  42.  
  43.                                 y1 = event.getY();
  44.  
  45.                         }
  46.  
  47.                 }
  48.  
  49.                
  50.  
  51.                 if(mCurUp){
  52.  
  53.                         if(!(x2 > 0) && !(y2 > 0)){
  54.  
  55.                                 x2 = event.getX();
  56.  
  57.                                 y2 = event.getY();
  58.  
  59.                         }
  60.  
  61.                 }
  62.  
  63.                 Log.i("x1",String.valueOf(x1));
  64.  
  65.                 Log.i("y1",String.valueOf(y1));
  66.  
  67.                 Log.i("x2",String.valueOf(x2));
  68.  
  69.                 Log.i("y2",String.valueOf(y2));
  70.  
  71.                
  72.  
  73.                 Log.i("Draw Canvas...","Draw Canvas...");
  74.  
  75.         return true;
  76.  
  77.         }
  78.  
  79.                
  80.  
  81.         protected void onDraw(Canvas canvas)
  82.  
  83.         {
  84.  
  85.             mImageView.setImageResource(resID);
  86.  
  87.             mImageView.setDrawingCacheEnabled(true);
  88.  
  89.             Bitmap bitmap = mImageView.getDrawingCache();
  90.  
  91.             Log.i("Bitmap = ",String.valueOf(bitmap));
  92.  
  93.             if (bitmap != null) {
  94.  
  95.                     if (x1 > 0 || x2 > 0 || y1 > 0 || y2 > 0)
  96.  
  97.                         canvas.drawRect(x1, y1, x2, y2, mPaint);
  98.  
  99.                     mImageView.draw(canvas);
  100.  
  101.             }
  102.  
  103.         }      
  104.  
  105.    }  
  106.  
  107.  
Parsed in 0.043 seconds, using GeSHi 1.0.8.4

Please pass on any more knowledge/information you can share on this. Thank you - Vishal
vishalmk
Freshman
Freshman
 
Posts: 6
Joined: Fri Feb 22, 2008 6:21 am

Postby plusminus » Mon Feb 25, 2008 10:11 am

Hello vishalmk,

what is the purpose of the ImageView :?:
You can create a BitMap directly from the Resources:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.      protected void onDraw(Canvas canvas){
  2.           if (x1 > 0 || x2 > 0 || y1 > 0 || y2 > 0){
  3.                // What is this supposed to do ? Clear the background ???
  4.                canvas.drawRect(x1, y1, x2, y2, mPaint);
  5.  
  6.                // Load the bitmap in Constructor for performance reasons!
  7.                Bitmap bm = BitmapFactory.decodeResource(c.getResources(), resID);
  8.                canvas.drawBitmap(bm, left, top, mPaint); // Maybe choose another paint here
  9.           }
  10.      }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

to obtain the Resource, you need to save the Context-Reference that is passed to your constructor.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby vishalmk » Mon Feb 25, 2008 11:31 am

I want to tag some area in an image/photo and store the coordinates, so that when I open a photograph/image next time, I can click in that area and start a new activity...

I am using ImageView to show image in the screen using something like this.
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
mImageView = new MyView(this);
setContentView(mImageView);
//mImageView.setImageResource(resID);
}
vishalmk
Freshman
Freshman
 
Posts: 6
Joined: Fri Feb 22, 2008 6:21 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 14 guests