Bitmap.getPixel always returning black

我是研究僧i 提交于 2019-12-11 00:59:59

问题


I'm creating an app which involves getting the color of part of the screen. To do this I am using the Bitmap.getPixel method to retrieve a specified pixel of the screen, than I am converting it to RGB format to make it easier for me to code with later. The issue is no matter what is on the screen when i use the getPixel method, it always returns the same RGB values, R:0 G:0 B:0, or Black, even when there is a gray button covering up the entire screen! Here is the code

package proof.of.concept;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.*;
public class ColorCheckerProofOfConcept extends Activity {
    private static final String TAG = "ColorChckerProofOfConcept:: ";  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Button button = (Button) findViewById(R.id.button1);

     button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             Display display = getWindowManager().getDefaultDisplay();
                int width = display.getWidth();
                int height = display.getHeight();
                   Log.d(TAG, "Width and Height Retrieved As: " + width + ", " + height);
                Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config. RGB_565);
                String hexValue;

                 int test;
              test = b.getPixel(240, 350);

             hexValue = Integer.toHexString(test);
             Log.d(TAG, "pixel at 100, 200 succesfully retreived! with value of: " + test);
             Log.d(TAG, "and an Hex value of: " + hexValue);
             int blue = Color.blue(test);
             int red = Color.red(test);
             int green = Color.green(test);
//this is a modification

               Log.d(TAG, "RGB COLOR! R:" + red + " G:" + green + " B:" + blue);
         }
     });
   }
}

回答1:


You are pulling from your Bitmap not what is on your screen. You created a black bitmap the size of your screen but not of your screen. Your code works for pulling from that bitmap. Are you trying to create a bitmap of your screen?




回答2:


If anyone else is looking for what you're supposed to do...

//just add this is

    Canvas c = new Canvas(b);
    View view = ((ViewGroup)findViewById(android.R.id.content)).getChildAt(0);
    view.draw(c);

//right after you create your bitmap, that should print the screen onto the bitmap
//from there, you can use Bitmap.getPixel(X,Y)


来源:https://stackoverflow.com/questions/9245071/bitmap-getpixel-always-returning-black

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!