从相机预览中的触摸事件中检索精确的RGB值

时间:2015-07-20 14:00:19

标签: android opencv camera opencv4android

我一直在开发一款Android应用,只需要在相机预览中检索并显示触摸事件的坐标和RGB值。我是这种编程语言的初学者,我只想尝试这个但是应用程序在触摸事件期间不断崩溃。这是我在Android中尝试的代码:

    /* When copying this code set Permissions: CAMERA
     * Do this by adding to AndroidManifest.xml:
     * <uses-permission
    android:name="android.permission.CAMERA" />
     */

    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.Color;
    import android.hardware.Camera;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Display;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.MotionEvent;
    import android.widget.FrameLayout;
    import android.widget.TextView;

    @SuppressWarnings("deprecation") 

    public class MainActivity extends Activity {
private static final String TAG = "HDMapp_MainActivity";    //ERROR Log tag
private Camera mCamera = null;
private CameraDisplay mCameraView = null;
static TextView touchView;
private Bitmap bmp;

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

    //try-catch block to try accessing the Android camera
    try {
        mCamera = Camera.open();
    } catch(Exception e) {
        Log.d(TAG, "Failed to get camera" + e.getMessage());
    }

    if (mCamera != null) {
        mCameraView = new CameraDisplay(this, mCamera); //create SurfaceView to show camera data
        FrameLayout camera_view = (FrameLayout)findViewById(R.id.camera_view);
        camera_view.addView(mCameraView); //add the SurfaceView to the layout
    }

    touchView = (TextView)findViewById(R.id.touchView);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

//detects touches on screen
@Override
public boolean onTouchEvent(MotionEvent event) {
    int x = (int)event.getX();
    int y = (int)event.getY();

    Display display = getWindowManager().getDefaultDisplay();
    int width = display.getWidth();
    int height = display.getHeight();

    int pixelVal = bmp.getPixel(heightC,widthC);
    int r = (pixelVal>>16) & 0xFF;
    int g = Color.green(pixelVal);
    int b = Color.blue(pixelVal);

    touchView.setText("Touch coordinates--> " + "x: " +
            String.valueOf(event.getX()) + " y: " + String.valueOf(event.getY()) + 
            " \n" + r + " \n" + g + " \n" + b);

    return super.onTouchEvent(event);
}
    }

我收到NullPointerException错误:

  

E / MessageQueue-JNI(14476):java.lang.NullPointerException:尝试   调用虚方法'int android.graphics.Bitmap.getPixel(int,int)'   在空对象引用上

我也尝试使用 OpenCV4Android ,我仍然得到相同的结果:

    import org.opencv.android.BaseLoaderCallback;
    import org.opencv.android.CameraBridgeViewBase;
    import org.opencv.android.OpenCVLoader;
    import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
    import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
    import org.opencv.android.LoaderCallbackInterface;
    import org.opencv.core.CvType;
    import org.opencv.core.Mat;

    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Display;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.MotionEvent;
    import android.view.SurfaceView;
    import android.view.WindowManager;
    import android.widget.TextView;

    public class MainActivity extends Activity implements CvCameraViewListener2 {
private CameraBridgeViewBase mOpenCvCameraView;
private Mat mRgba;
TextView touchView;
int x, y;
double [] rgb;

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
        case LoaderCallbackInterface.SUCCESS:
        {
            Log.i("BaseLoaderCallback", "OpenCV loaded successfully");
            mOpenCvCameraView.enableView();
        } break;
        default:
        {
            super.onManagerConnected(status);
        } break;
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i("OnCreate", "called onCreate");
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setContentView(R.layout.activity_main);

    mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.surface_view);
    mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
    mOpenCvCameraView.setCvCameraViewListener(this);

    //touchView = (TextView)findViewById(R.id.touchView);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onPause() {
    super.onPause();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}

@Override
public void onResume()
{
    super.onResume();
    if (!OpenCVLoader.initDebug()) {
        Log.d("onResume", "Internal OpenCV library not found. Using OpenCV Manager for initialization");
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
    } else {
        Log.d("onResume", "OpenCV library found inside package. Using it!");
        mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
    }
}

public void onDestroy() {
    super.onDestroy();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}

public void onCameraViewStarted(int width, int height) {
    mRgba = new Mat(height, width, CvType.CV_8UC4); 
}

public void onCameraViewStopped() {
    mRgba.release();    
}

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    // TODO Auto-generated method stub
    mRgba = inputFrame.rgba();
    rgb = mRgba.get(x,y);

    return mRgba;
}   

//detects touches on screen
    @SuppressWarnings("deprecation")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        x = (int)event.getX();
        y = (int)event.getY();

        //Display display = getWindowManager().getDefaultDisplay();
        //int width = display.getWidth();
        //int height = display.getHeight();

        //double [] rgb = mRgba.get(width,height);

        touchView.setText("Touch coordinates--> " + "x: " + String.valueOf(x) 
                + " y: " + String.valueOf(y) + " \n" + "RGB values--> " + "Red: " + rgb[0]
                + " Green: " + rgb[1] + " Blue: " + rgb[2]);

        return super.onTouchEvent(event);
    }
    }

错误再次出现:

  

E / MessageQueue-JNI(15884):java.lang.NullPointerException:尝试   调用虚方法'void   android.widget.TextView.setText(java.lang.CharSequence)'为null   对象参考

这两个应用程序都显示了相机预览,但当我触摸Nexus 7的屏幕时,该应用程序停止工作。有人可以告诉我我做错了什么并且可能纠正我吗?

1 个答案:

答案 0 :(得分:1)

第二个示例应该有效,问题是x和y没有初始化,所以在方法onCameraFrame中初始化rgb = mRgba.get(x,y),在此rgb之后仍然{{1}自nullx未初始化以来。因此,当您尝试打印时,y

我建议你只是在开头将nullPointerExceptionx初始化为y

-1

然后在触摸处理程序方法中更新x和y,如下所示:

int x=-1, y=-1;

并在 onCameraFrame 方法中执行以下操作:

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

希望这有帮助