Skip to content Skip to sidebar Skip to footer

Retrieve Exact Rgb Value From Touch Event In Camera Preview

I've been working on an Android app that simply needs to retrieve and display the coordinates and RGB values of a touch event on the camera preview. I'm a beginner at this programm

Solution 1:

the second example should work, the problem is that x and y are not initialized, so in method onCameraFrame, where you initialize rgb = mRgba.get(x,y), after this rgb is still null, since x and y are not initialized. Hence, your nullPointerException when you try to print it.

What I would suggest is that you just initialize x and y to -1 in the beginning:

int x=-1, y=-1;

and then in touch handler method you update x and y like so:

public boolean onTouchEvent(MotionEvent event) {
    x = (int)event.getX();
    y = (int)event.getY();
    return super.onTouchEvent(event);
}

and in the onCameraFrame method you do something like this:

publicMatonCameraFrame(CvCameraViewFrame inputFrame) {
  mRgba = inputFrame.rgba();
  if(x != -1 && y != -1) { //if this is true, you've touched something
    rgb = mRgba.get(x,y);
    touchView.setText("Touch coordinates--> " + "x: " + String.valueOf(x) 
            + " y: " + String.valueOf(y) + " \n" + "RGB values--> " + "Red: " + rgb[0]
            + " Green: " + rgb[1] + " Blue: " + rgb[2]);
    x = -1; 
    y = -1;
  }

return mRgba;
}

Hope this helps

Post a Comment for "Retrieve Exact Rgb Value From Touch Event In Camera Preview"