Bitmap.getPixel 始终返回黑色
我正在创建一个应用程序,其中涉及获取屏幕部分的颜色。 为此,我使用 Bitmap.getPixel 方法来检索屏幕的指定像素,然后将其转换为 RGB 格式,以便以后更轻松地进行编码。问题是,当我使用 getPixel 方法时,无论屏幕上是什么,它总是返回相同的 RGB 值,R:0 G:0 B:0 或黑色,即使有一个灰色按钮覆盖整个屏幕!这是代码
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);
}
});
}
}
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);
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果其他人正在寻找你应该做的事情......
If anyone else is looking for what you're supposed to do...
您是从位图中提取的,而不是屏幕上的内容。您创建了一个黑色位图,其大小与屏幕大小相同,但与屏幕大小无关。您的代码适用于从该位图中提取。您是否正在尝试创建屏幕位图?
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?