2016-02-24 1 views
5

पर पृष्ठभूमि में पिक्सेल रंग प्राप्त करें मुझे एक ऐसी सेवा बनाने की आवश्यकता है जो एक पिक्सेल के रंग के लिए एंड्रॉइड स्क्रीन के समन्वय की जांच करे।एंड्रॉइड

ईजी।

Bitmap bitmap = ((BitmapDrawable)getBackground()).getBitmap(); 
int pixel = bitmap.getPixel(x,y); 

if(pixel == Color.MAGENTA){ 
    //Stop Service 
} 

हालांकि बजाय बस अपना एप्लिकेशन, मैं इसे एक पृष्ठभूमि सेवा है जो रंग यहाँ तक कि जब मैं ऐप्लिकेशन स्विच के लिए जाँच रहता है में होना चाहिए।

मान लें कि डिवाइस रूट है, क्या कोई सुपरसुर कमांड है जो मुझे चाहिए?

+0

यह बुरी तरह अक्षम और बोझिल लगता है की आवश्यकता है। अन्य विकल्पों का पता क्यों नहीं लगाओ? – JoxTraex

+0

आपकी सेवा में प्रसारण रिसीवर का उपयोग करें और अपनी गतिविधि से प्रसारण भेजें, और अपनी सेवा में प्रसारण की सदस्यता लें – Bhargav

+0

यहां क्या उद्देश्य है? आप क्या जांचने की कोशिश कर रहे हैं? आप जानना चाहते हैं कि उदाहरण के लिए जब किसी मैपेंटा पिक्सेल को एक्स पर प्रदर्शित किया जाता है, तो किसी भी ऐप से स्क्रीन पर वाई स्थिति (जैसे फेसबुक)? उत्तर सभी आपके ऐप के भीतर ही प्रस्तुत की गई चीज़ों को देख रहे हैं, इसलिए आपको –

उत्तर

1

जो लोग कुछ करने के लिए है कि मैं कर रहा हूँ की जरूरत के लिए,

मैं स्क्रीन पर कब्जा और स्क्रीनशॉट पर पिक्सेल के लिए जाँच करने के लिए प्रणाली के आदेश/प्रणाली/bin/screencap -p इस्तेमाल किया।

इस विधि जड़ अनुमति

private void getPixelColor(int xcoord, int ycoord) 
{ 
    try { 
     Process sh = Runtime.getRuntime().exec("su", null,null); 

     OutputStream os = sh.getOutputStream(); 
     os.write(("/system/bin/screencap -p " + "/sdcard/colorPickerTemp.png").getBytes("ASCII")); 
     os.flush(); 

     os.close(); 
     sh.waitFor(); 

     Bitmap screen = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + File.separator + "colorPickerTemp.png"); 
     int pixel = screen.getPixel(xcoord ,ycoord + statusHeight); 
     Log.d("pixel color", "Pixel Color: + " + Integer.toHexString(pixel) + " at x:" + xcoord + " y:" + ycoord); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 
-2

जैसा कि कहा गया है। सामान्य रूप से अक्षम ... शायद आपके आवेदन को लागू करने के लिए एक बेहतर तरीका है।

public boolean checkColor() { 
     View rootView = findViewById(android.R.id.content).getRootView(); 
     rootView.setDrawingCacheEnabled(true); 
     Bitmap bitmap = rootView.getDrawingCache(); 
     rootView.setDrawingCacheEnabled(false); 
     int pixel = bitmap.getPixel(x,y); 
     if(pixel == Color.MAGENTA){ 
      return true; 
     } 
     return false; 
    } 
3
public class TouchView extends ImageView { 

Bitmap bitmap; 
double bmWidth, bmHeight; 

public TouchView(Context context) { 
    super(context); 
    // TODO Auto-generated constructor stub 
    init(); 
} 

public TouchView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    // TODO Auto-generated constructor stub 
    init(); 
} 

public TouchView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    // TODO Auto-generated constructor stub 
    init(); 
} 

private void init(){ 

    bitmap = ((BitmapDrawable)getBackground()).getBitmap(); 
    bmWidth = (double)bitmap.getWidth(); 
    bmHeight = (double)bitmap.getHeight(); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    // TODO Auto-generated method stub 
    setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), 
     MeasureSpec.getSize(heightMeasureSpec)); 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    // TODO Auto-generated method stub 


    switch(event.getAction()){ 
    case MotionEvent.ACTION_DOWN: 
    case MotionEvent.ACTION_MOVE: 
    float x = event.getX(); 
    float y = event.getY(); 

    int color = getColor(x, y); 
     ((AndroidDetechTouchActivity)getContext()).updateMsg("[email protected]" + x + " : " + y, color); 

    break; 
    case MotionEvent.ACTION_UP: 
    ((AndroidDetechTouchActivity)getContext()).updateMsg("", 0); 
    break; 
    } 

    return true; 
} 

private int getColor(float x, float y){ 

    if (x < 0 || y < 0 || x > (float)getWidth() || y > (float)getHeight()){ 
    return 0; //Invalid, return 0 
    }else{ 
    //Convert touched x, y on View to on Bitmap 
    int xBm = (int)(x * (bmWidth/(double)getWidth())); 
     int yBm = (int)(y * (bmHeight/(double)getHeight())); 

    return bitmap.getPixel(xBm, yBm); 
    } 

} 

} 

संशोधित updateMsg() मुख्य गतिविधि, AndroidDetechTouchActivity.java, की विधि रंग शामिल हैं और TextView के textcolor अद्यतन करने के लिए।

public class AndroidDetechTouchActivity extends Activity { 

TextView msg; 
TouchView touchView; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     msg = (TextView)findViewById(R.id.msg); 
     touchView = (TouchView)findViewById(R.id.touchview); 

    } 

    public void updateMsg(String tMsg, int color){ 
    msg.setTextColor(color); 
    msg.setText(tMsg); 
    } 

} 
+0

हाय, स्पष्ट और धन्यवाद के लिए धन्यवाद, हालांकि यह अन्य ऐप्स का पिक्सेल रंग भी प्राप्त करता है? –