Bitmap.getPixel总是返回黑 [英] Bitmap.getPixel always returning black

查看:671
本文介绍了Bitmap.getPixel总是返回黑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建其中涉及越来越的屏幕部分的颜色的应用程序。
要做到这一点,我现在用的是Bitmap.getPixel方法来检索屏幕的指​​定像素,比我将其转换为RGB格式,使其更容易对我来说,code与后来的。问题是,不管是什么,当我使用与getPixel方法在屏幕上,它总是返回相同的RGB值,R:0 G:0 B:0,或黑色,即使有一个灰色按钮覆盖整个屏幕!这里是code

 包proof.of.concept;进口android.app.Activity;
进口android.graphics.Bitmap;
进口android.graphics.Color;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.Display;
进口android.view.View;
导入android.widget *。
公共类ColorCheckerProofOfConcept延伸活动{
    私有静态最后弦乐TAG =ColorChckerProofOfConcept ::;
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        最后Button按钮=(按钮)findViewById(R.id.button1);     button.setOnClickListener(新View.OnClickListener(){
         公共无效的onClick(视图v){
             显示显示= getWindowManager()getDefaultDisplay()。
                INT宽度= display.getWidth();
                INT高度= display.getHeight();
                   Log.d(TAG,宽度和高度检索为:+宽+,+高);
                位图B = Bitmap.createBitmap(宽度,高度,Bitmap.Config RGB_565);
                串hexValue;                 INT测试;
              测试= b.getPixel(240,350);             hexValue = Integer.toHexString(测试);
             Log.d(TAG,在100像素,200成功地与retreived价值:!+测试);
             Log.d(TAG,和一个十六进制值:+ hexValue);
             INT蓝= Color.blue(试验);
             INT红色= Color.red(试验);
             INT绿色= Color.green(试验);
//这是一个修改               Log.d(TAG!RGB颜色R:+红+G:+绿色+B:+蓝色);
         }
     });
   }
}


解决方案

您从您的位图拉没有什么是您的屏幕上。您创建了一个黑色的位图屏幕的大小,但不是你的屏幕。您code适用于从位图拉。你们是不是要创建屏幕的位图?

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);
         }
     });
   }
}

解决方案

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?

这篇关于Bitmap.getPixel总是返回黑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆