如何从android中的imageview获取RGB值

时间:2014-11-05 06:02:36

标签: android android-fragments

我要做的是什么:我正在尝试从图像中获取RGB值


发生了什么:我在

中收到空指针异常
final Bitmap bitmap = ((BitmapDrawable)imgUsrClrId.getDrawable()).getBitmap();

问题:这种情况发生在fragments here正在为活动工作,我想我无法正确获取imageview fragments 我如何解决这个问题!!


MyClass.java

public class MyClass extends Fragment {


    //View object variables
    TextView tvAllSymptomsId,tvGenderId,txtColorCodeId;
    Button btnRotateId;
    ImageView imgUsrClrId;


    public MyClass() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.symptom_checker, container,false);

        //Find the views
        tvAllSymptomsId=(TextView) rootView.findViewById(R.id.tvAllSymptomsId);
        tvGenderId=(TextView) rootView.findViewById(R.id.tvGenderId);
        btnRotateId=(Button) rootView.findViewById(R.id.btnRotateId);
        imgUsrClrId=(ImageView) rootView.findViewById(R.id.imgUsrClrId);
        txtColorCodeId=(TextView) rootView.findViewById(R.id.txtColorCodeId);

        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();

        //Initially set the image as female facing front
        imgUsrClrId.setBackgroundDrawable(getResources().getDrawable(R.drawable.female_front));

        final Bitmap bitmap = ((BitmapDrawable)imgUsrClrId.getDrawable()).getBitmap();
        imgUsrClrId.setOnTouchListener(new OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event){
            int x = (int)event.getX();
            int y = (int)event.getY();
            int pixel = bitmap.getPixel(x,y);

            //then do what you want with the pixel data, e.g
            int redValue = Color.red(pixel);
            int blueValue = Color.blue(pixel);
            int greenValue = Color.green(pixel);  

            txtColorCodeId.setText(redValue+greenValue+blueValue);
            return false;
            }
       });
    }
}

XML代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imgUsrClrId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="@drawable/female_front"
        android:layout_centerVertical="true" />

    <TextView
        android:id="@+id/tvGenderId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imgUsrClrId"
        android:layout_toLeftOf="@+id/imgUsrClrId"
        android:text="Gender" />

    <TextView
        android:id="@+id/tvAllSymptomsId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imgUsrClrId"
        android:layout_alignParentRight="true"
        android:layout_marginRight="14dp"
        android:text="All Symptoms" />

    <Button
        android:id="@+id/btnRotateId"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imgUsrClrId"
        android:layout_alignRight="@+id/tvAllSymptomsId"
        android:background="@drawable/rotate" />

    <TextView
        android:id="@+id/txtColorCodeId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="TextView" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

将此代码移至onCreateView()方法。

imgUsrClrId.setBackgroundDrawable(getResources().getDrawable(R.drawable.female_front));

    final Bitmap bitmap = ((BitmapDrawable)imgUsrClrId.getDrawable()).getBitmap();
    imgUsrClrId.setOnTouchListener(new OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event){
        int x = (int)event.getX();
        int y = (int)event.getY();
        int pixel = bitmap.getPixel(x,y);

        //then do what you want with the pixel data, e.g
        int redValue = Color.red(pixel);
        int blueValue = Color.blue(pixel);
        int greenValue = Color.green(pixel);  

        txtColorCodeId.setText(redValue+greenValue+blueValue);
        return false;
        }
   });

最初,您的imgUsrClrId为空。在onStart()方法中Fragment将是可见的,因此它首先获得imgUsrClrId null的值。

答案 1 :(得分:0)

问题是您在错误的位置编写代码,您将在onActivityCreated方法而不是onStart()

中获取ImageView对象
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);

    /* Write code to get reference of ImageView and Get value of Bitmap RGB here */

}

更新:

这意味着ImageView Instance不为null,但drawable是

您正在设置背景可绘制,您需要像下面那样访问它

Drawable background = view.getBackground();
if (background instanceof BitmapDrawable)
{
   Bitmap bitmap = ((BitmapDrawable)background).getBitmap();
}