Touch event on Bitmap (coordinates and getPixel)

All your problems with Audio, Video and Images.

Touch event on Bitmap (coordinates and getPixel)

Postby captFuture » Fri Feb 26, 2010 10:33 am

Hi guys,
For controlling my RGB multicolor LEDlamp I try developing an app that reads color values from a RGB color wheel image and displays the values as string over serial/bluetooth/wifi to the LED controller.

The connection stuff is pretty much done bit now I'm struggling getting the "colorpicker" to work.
My basic setup is a layout with 2 edit text fields for displaying the color values in RGB and Hex and an ImageView where a BitmapDrawable (from the colorwheel.png) is created

The Touch Event is set up - i can read the x and y of the canvas but can't access and read it from the Bitmap (and the getPixel for color values does not work as well)

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.    android:orientation="vertical"
  7.  
  8.    android:layout_width="fill_parent"
  9.  
  10.    android:layout_height="fill_parent"
  11.  
  12.    android:background="#ffffff">
  13.  
  14. <TextView  
  15.  
  16.    android:layout_width="40px"
  17.  
  18.    android:layout_height="wrap_content"
  19.  
  20.    android:text="Hex:"
  21.  
  22.    android:textColor="#000000"
  23.  
  24.    android:layout_marginLeft="10px"/>
  25.  
  26. <EditText android:text="@+id/EditText01" android:layout_marginLeft="10px" android:id="@+id/EditText01" android:layout_height="wrap_content" android:layout_width="200px"></EditText>
  27.  
  28.    
  29.  
  30. <TextView  
  31.  
  32.    android:layout_width="40px"
  33.  
  34.    android:layout_height="wrap_content"
  35.  
  36.    android:text="RGB:"
  37.  
  38.    android:textColor="#000000"
  39.  
  40.    android:layout_marginLeft="10px"/>
  41.  
  42.  
  43.  
  44. <EditText android:text="@+id/EditText02"
  45.  
  46. android:id="@+id/EditText02"
  47.  
  48. android:layout_marginLeft="10px"
  49.  
  50. android:layout_height="wrap_content"  
  51.  
  52. android:layout_width="200px">
  53.  
  54. </EditText>
  55.  
  56.  
  57.  
  58. <ImageView android:id="@+id/ImageView01"
  59.  
  60. android:layout_width="wrap_content"
  61.  
  62. android:layout_height="wrap_content"
  63.  
  64. android:hapticFeedbackEnabled="true"
  65.  
  66. android:focusableInTouchMode="true"
  67.  
  68. android:scaleType="center"
  69.  
  70. android:clickable="true" android:layout_gravity="center">
  71.  
  72. </ImageView>
  73.  
  74. </LinearLayout>
  75.  
  76.  
Parsed in 0.006 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package cap.pick.color;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.graphics.Bitmap;
  9.  
  10. import android.graphics.Color;
  11.  
  12. import android.graphics.drawable.BitmapDrawable;
  13.  
  14. import android.os.Bundle;
  15.  
  16. import android.view.MotionEvent;
  17.  
  18. import android.widget.ImageView;
  19.  
  20. import android.widget.TextView;
  21.  
  22.  
  23.  
  24. public class MyColorPicker extends Activity {
  25.  
  26.        
  27.  
  28.     public Float xtouch;
  29.  
  30.     public Float ytouch;
  31.  
  32.     public TextView Hex;
  33.  
  34.     public TextView RGB;
  35.  
  36.     public ImageView ImageView01;
  37.  
  38.     public BitmapDrawable bd;
  39.  
  40.     public Bitmap bitmap;
  41.  
  42.        
  43.  
  44.     /** Called when the activity is first created. */
  45.  
  46.     @Override
  47.  
  48.     public void onCreate(Bundle savedInstanceState) {
  49.  
  50.         super.onCreate(savedInstanceState);
  51.  
  52.        
  53.  
  54.         setContentView(R.layout.main);
  55.  
  56.         Hex = (TextView) findViewById(R.id.EditText01);
  57.  
  58.         RGB = (TextView) findViewById(R.id.EditText02);  
  59.  
  60.         ImageView myImageView = null;
  61.  
  62.         myImageView = (ImageView) findViewById(R.id.ImageView01);
  63.  
  64.         BitmapDrawable bd = (BitmapDrawable) myImageView.getResources().getDrawable(R.drawable.wheel);
  65.  
  66.         Bitmap bitmap = bd.getBitmap();
  67.  
  68.         myImageView.setImageBitmap(bitmap);
  69.  
  70.     }
  71.  
  72.  
  73.  
  74.    
  75.  
  76.     public boolean dispatchTouchEvent(MotionEvent event){
  77.  
  78.  
  79.  
  80.     // this is canvas x/y - for testing the touch event and it works
  81.  
  82.           xtouch = event.getX();
  83.  
  84.           ytouch = event.getY();
  85.  
  86.           RGB.setText(xtouch.toString());
  87.  
  88.           Hex.setText(ytouch.toString());
  89.  
  90.          
  91.  
  92.           // this is bitmap get color on x/y  - it doesn't work (app crashes)
  93.  
  94.           int color = bitmap.getPixel((int)event.getX(), (int)event.getY());
  95.  
  96.           RGB.setText(Color.red(color)+""+Color.green(color)+""+Color.blue(color));
  97.  
  98.                  
  99.  
  100.           return true;
  101.  
  102.         }
  103.  
  104. }
  105.  
  106.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


I'm pretty stuck here - please help :)
captFuture
Freshman
Freshman
 
Posts: 3
Joined: Fri Feb 26, 2010 10:10 am

Top

Postby pskink » Fri Feb 26, 2010 1:11 pm

i've done similar color picker that operates on hsv model with optional alpha channel

i tried color wheel as well but decided tha picking from it would be inaccurate

see:
http://www.anddev.org/announce_color_pi ... 10771.html
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby captFuture » Fri Feb 26, 2010 2:09 pm

Thanks for your link - already did a "slider" based control, but this is too much "overhead" for an easy LEDcontrol and therefore i chose use a colorwheel now.

Cheers
captFuture
Freshman
Freshman
 
Posts: 3
Joined: Fri Feb 26, 2010 10:10 am

Re: Touch event on Bitmap (coordinates and getPixel)

Postby pskink » Fri Feb 26, 2010 2:31 pm

captFuture wrote:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package cap.pick.color;
  2.  
  3. import android.app.Activity;
  4. import android.graphics.Bitmap;
  5. import android.graphics.Color;
  6. import android.graphics.drawable.BitmapDrawable;
  7. import android.os.Bundle;
  8. import android.view.MotionEvent;
  9. import android.widget.ImageView;
  10. import android.widget.TextView;
  11.  
  12. public class MyColorPicker extends Activity {
  13.        
  14.     public Float xtouch;
  15.     public Float ytouch;
  16.     public TextView Hex;
  17.     public TextView RGB;
  18.     public ImageView ImageView01;
  19.     public BitmapDrawable bd;
  20.     public Bitmap bitmap;
  21.        
  22.     /** Called when the activity is first created. */
  23.     @Override
  24.     public void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.        
  27.         setContentView(R.layout.main);
  28.         Hex = (TextView) findViewById(R.id.EditText01);
  29.         RGB = (TextView) findViewById(R.id.EditText02);  
  30.         ImageView myImageView = null;
  31.         myImageView = (ImageView) findViewById(R.id.ImageView01);
  32.         BitmapDrawable bd = (BitmapDrawable) myImageView.getResources().getDrawable(R.drawable.wheel);
  33.         Bitmap bitmap = bd.getBitmap();
  34.         myImageView.setImageBitmap(bitmap);
  35.     }
  36.  
  37.    
  38.     public boolean dispatchTouchEvent(MotionEvent event){
  39.  
  40.     // this is canvas x/y - for testing the touch event and it works
  41.           xtouch = event.getX();
  42.           ytouch = event.getY();
  43.           RGB.setText(xtouch.toString());
  44.           Hex.setText(ytouch.toString());
  45.          
  46.           // this is bitmap get color on x/y  - it doesn't work (app crashes)
  47.           int color = bitmap.getPixel((int)event.getX(), (int)event.getY());
  48.           RGB.setText(Color.red(color)+""+Color.green(color)+""+Color.blue(color));
  49.                  
  50.           return true;
  51.         }
  52. }
  53.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4



this is because dispatchTouchEvent gets events where which position (0,0) is left/top corner of the root view of your activity not left/top of your image view

use myImageView.setOnTouchListener
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby captFuture » Fri Feb 26, 2010 3:31 pm

My newest version at least works... but with wrong color values - how do I correctly set this onTouchListener?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package cap.pick.color;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.graphics.Bitmap;
  9.  
  10. import android.graphics.Color;
  11.  
  12. import android.graphics.drawable.BitmapDrawable;
  13.  
  14. import android.os.Bundle;
  15.  
  16. import android.view.MotionEvent;
  17.  
  18. import android.widget.Button;
  19.  
  20. import android.widget.ImageView;
  21.  
  22. import android.widget.TextView;
  23.  
  24.  
  25.  
  26. public class MyColorPicker extends Activity {
  27.  
  28.     public TextView RGB;
  29.  
  30.     public ImageView ImageView01;
  31.  
  32.     public Button BTN;
  33.  
  34.     public BitmapDrawable bd;
  35.  
  36.     public Bitmap bitmap;
  37.  
  38.    
  39.  
  40.     /** Called when the activity is first created. */
  41.  
  42.     @Override
  43.  
  44.     public void onCreate(Bundle savedInstanceState) {
  45.  
  46.         super.onCreate(savedInstanceState);
  47.  
  48.         setContentView(R.layout.main);
  49.  
  50.  
  51.  
  52.         RGB = (TextView) findViewById(R.id.EditText02);  
  53.  
  54.         RGB.setText("please choose...");
  55.  
  56.        
  57.  
  58.         BTN = (Button) findViewById(R.id.Button01);
  59.  
  60.         BTN.setText("");
  61.  
  62.         BTN.setBackgroundColor(0xffffffff);
  63.  
  64.        
  65.  
  66.         ImageView myImageView = null;
  67.  
  68.         myImageView = (ImageView) findViewById(R.id.ImageView01);
  69.  
  70.         BitmapDrawable bd = (BitmapDrawable) myImageView.getResources().getDrawable(R.drawable.wheel);
  71.  
  72.         Bitmap bitmap = bd.getBitmap();
  73.  
  74.         myImageView.setImageBitmap(bitmap);
  75.  
  76.     }
  77.  
  78.  
  79.  
  80.   public boolean dispatchTouchEvent(MotionEvent event){
  81.  
  82.           ImageView myImageView = (ImageView) findViewById(R.id.ImageView01);
  83.  
  84.           BitmapDrawable bd = (BitmapDrawable) myImageView.getResources().getDrawable(R.drawable.wheel);
  85.  
  86.           Bitmap bitmap = bd.getBitmap();
  87.  
  88.           try{
  89.  
  90.                   int color = bitmap.getPixel((int)event.getX(),(int)event.getX());
  91.  
  92.                   RGB.setText(" R:"+Color.red(color)+" G:"+Color.green(color)+" B:"+Color.blue(color));
  93.  
  94.                   BTN.setBackgroundColor(color);
  95.  
  96.           } catch (Exception e){};
  97.  
  98.       return true;
  99.  
  100.         }
  101.  
  102.  
  103.  
  104.  
  105.  
  106. }
  107.  
  108.  
Parsed in 0.042 seconds, using GeSHi 1.0.8.4
captFuture
Freshman
Freshman
 
Posts: 3
Joined: Fri Feb 26, 2010 10:10 am

Postby pskink » Fri Feb 26, 2010 4:21 pm

captFuture wrote:My newest version at least works... but with wrong color values - how do I correctly set this onTouchListener?


myImageView.setOnTouchListener(this);

your Activity should implement OnTouchListener by overriding onTouch method.

all you did in dispatchTouchEvent move to onTouch

thats it.
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Top

captFuture

Postby -squid- » Thu Mar 25, 2010 1:57 am

Did you get this working with your lamp captFuture? !?
-squid-
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Mar 25, 2010 12:49 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 23 guests