簡體   English   中英

如何從rgb值獲取紅色值:android

[英]How can I get red value from rgb value : android

  import java.io.File;


public class AndroidCamera extends Activity implements SurfaceHolder.Callback{

Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;;
    private Bitmap bitmap;
    TextView colorRGB;


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

   Button testButton = (Button) findViewById(R.id.testButton); 
   txtData = (EditText) findViewById(R.id.editText1);


//j*******************************************************************************       
    bitmap=BitmapFactory.decodeFile("/sdcard/folder/tes.jpg");
    ImageView imageView=(ImageView)findViewById(R.id.imageView1);
    imageView.setImageBitmap(bitmap);

//j*******************************************************************************       

    testButton.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View v) 
        {
            int touchedRGB = bitmap.getPixel(400, 200);

            int Red1 = Color.red(touchedRGB);




            txtData.setText(Integer.toHexString(Red1));

            txtData.setTextColor(Red1);

        }});

}   

我編輯了帖子並添加了必要的代碼,以使其更清晰。 請找到更新的,並提供解決方案的建議。

對不起,這篇文章被誤刪了。 我無法從圖像中檢索紅色值。 我試過這段代碼。 此代碼是我收到的建議中的更新代碼。

int Red1 = Color.red(touchedRGB);

txtData.setTextColor(Red1);

像素的紅色分量不是您可以使用setTextColor()設置的顏色,它需要ARGB值。 因此,將R值解釋為ARGB將使紅色分量值成為藍色分量值,零alpha即完全透明,因此不可見。

int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);

應該夠了

如果你的意思是要求你有你的顏色值(RGB格式),並且你想要檢索R值那么你可以用這種方式得到它,

int r = (your_color_value >> 16) & 0xFF;

與獲得G和B值類似,

int g = (your_color_value >> 8) & 0xFF;
int b = (your_color_value >> 0) & 0xFF;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM