android 应用问题的相机图像 getpixel 颜色

camera image getpixel colour for android app issue

我是 JAVA 的新手,这是我的第一个 Whosebug post 所以我会尝试具体一点!

好吧,我正在尝试创建一个 android 应用程序,用户可以在其中使用手机拍照 camera.the 照片被返回并放置在图像视图中,用户可以按下按钮告诉你图像是什么颜色。到目前为止它工作正常但是我出于测试目的在图像视图中放置了一个绿色框的图片,即使在你拍照后绿色框被新照片替换我仍然获得绿色的 RGB 值尽管正在替换内容!

----我的代码----

private ImageView img;
private Bitmap bmp;
private Bitmap operation;
Button button;

ImageView imgFavorite;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imgFavorite = (ImageView)findViewById(R.id.imageView1);
    imgFavorite.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            open();
        }
    });

    img = (ImageView)findViewById(R.id.imageView1);
    button = (Button) findViewById(R.id.button);
    BitmapDrawable abmp = (BitmapDrawable)img.getDrawable();
    bmp = abmp.getBitmap();

    button.setOnClickListener(new View.OnClickListener() {
        @Override`enter code here`
        public void onClick(View v) {
            pix();
        }
    });
}

public void open(){
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, 0);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    Bitmap bp = (Bitmap) data.getExtras().get("data");
    imgFavorite.setImageBitmap(bp);
}

public void pix(){
    operation= Bitmap.createBitmap(bmp.getWidth(),
            bmp.getHeight(),bmp.getConfig());

    int height = bmp.getHeight();
    int width = bmp.getWidth();
    int p = bmp.getPixel(height / 2, width / 2);

    int r = Color.red(p);
    int g = Color.green(p);
    int b = Color.blue(p);

    Toast.makeText(this, String.valueOf(r) + String.valueOf(g) + String.valueOf(b), Toast.LENGTH_LONG).show();
}











@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

onActivityResult() 中,您正在设置 ImageView 来显示新照片,但您没有设置 bmp 变量,这是您用来读取 RGB 值的变量.所以只需将此行添加到 onActivityResult():

bmp = bp;