gpt4 book ai didi

android - Bitmap.getPixel 总是返回黑色

转载 作者:行者123 更新时间:2023-11-29 02:07:27 24 4
gpt4 key购买 nike

我正在创建一个涉及获取部分屏幕颜色的应用程序。为此,我使用 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);
}
});
}
}

最佳答案

如果其他人正在寻找您应该做的事情...

//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)

关于android - Bitmap.getPixel 总是返回黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9245071/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com